Skip to content

Commit

Permalink
Move StructuredData id to its own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoers committed Oct 22, 2009
1 parent cb849ce commit 1209c20
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 129 deletions.
121 changes: 2 additions & 119 deletions slf4j-api/src/main/java/org/slf4j/StructuredData.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface StructuredData extends Serializable {
*
* @return The structured data id.
*/
StructuredData.Id getId();
StructuredDataId getId();

/**
* A message String that is associated with the data.
Expand Down Expand Up @@ -91,122 +91,5 @@ public interface StructuredData extends Serializable {
* @param maps Additional data to include.
* @return The formatted String.
*/
String asString(String format, StructuredData.Id id, Map[] maps);

/**
* The StructuredData identifier
*/
public class Id implements Serializable {
private static final long serialVersionUID = 9031746276396249990L;

public static Id TIME_QUALITY = new Id("timeQuality", null,
new String[]{"tzKnown", "isSynced", "syncAccuracy"});
public static Id ORIGIN = new Id("origin", null,
new String[]{"ip", "enterpriseId", "software", "swVersion"});
public static Id META = new Id("meta", null,
new String[]{"sequenceId", "sysUpTime", "language"});

public static final int RESERVED = -1;

private final String name;
private final int enterpriseNumber;
private final String[] required;
private final String[] optional;

protected Id(String name, String[] required, String[] optional) {
int index = -1;
if (name != null) {
if (name.length() > 32) {
throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + name);
}
index = name.indexOf("@");
}

if (index > 0) {
this.name = name.substring(0, index);
this.enterpriseNumber = Integer.parseInt(name.substring(index+1));
} else {
this.name = name;
this.enterpriseNumber = RESERVED;
}
this.required = required;
this.optional = optional;
}

/**
* A Constructor that helps conformance to RFC 5424.
*
* @param name The name portion of the id.
* @param enterpriseNumber The enterprise number.
* @param required The list of keys that are required for this id.
* @param optional The list of keys that are optional for this id.
*/
public Id(String name, int enterpriseNumber, String[] required, String[] optional) {
if (name == null) {
throw new IllegalArgumentException("No structured id name was supplied");
}
if (enterpriseNumber <= 0) {
throw new IllegalArgumentException("No enterprise number was supplied");
}
this.name = name;
this.enterpriseNumber = enterpriseNumber;
String id = enterpriseNumber < 0 ? name : name + "@" + enterpriseNumber;
if (id.length() > 32) {
throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + id);
}
this.required = required;
this.optional = optional;
}

public Id makeId(StructuredData.Id id) {
if (id == null) {
return this;
}
return makeId(id.getName(), id.getEnterpriseNumber());
}

public Id makeId(String defaultId, int enterpriseNumber) {
String id;
String[] req;
String[] opt;
if (enterpriseNumber <= 0) {
return this;
}
if (this.name != null) {
id = this.name;
req = this.required;
opt = this.optional;
} else {
id = defaultId;
req = null;
opt = null;
}

return new Id(id, enterpriseNumber, req, opt);
}

public String[] getRequired() {
return required;
}

public String[] getOptional() {
return optional;
}

public String getName() {
return name;
}

public int getEnterpriseNumber() {
return enterpriseNumber;
}

public boolean isReserved() {
return enterpriseNumber <= 0;
}

public String toString() {
return isReserved() ? name : name + "@" + enterpriseNumber;
}
}
String asString(String format, StructuredDataId id, Map[] maps);
}
120 changes: 120 additions & 0 deletions slf4j-api/src/main/java/org/slf4j/StructuredDataId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.slf4j;

import java.io.Serializable;

/**
* The StructuredData identifier
*/
public class StructuredDataId implements Serializable {
private static final long serialVersionUID = 9031746276396249990L;

public static StructuredDataId TIME_QUALITY = new StructuredDataId("timeQuality", null,
new String[]{"tzKnown", "isSynced", "syncAccuracy"});
public static StructuredDataId ORIGIN = new StructuredDataId("origin", null,
new String[]{"ip", "enterpriseId", "software", "swVersion"});
public static StructuredDataId META = new StructuredDataId("meta", null,
new String[]{"sequenceId", "sysUpTime", "language"});

public static final int RESERVED = -1;

private final String name;
private final int enterpriseNumber;
private final String[] required;
private final String[] optional;

protected StructuredDataId(String name, String[] required, String[] optional) {
int index = -1;
if (name != null) {
if (name.length() > 32) {
throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + name);
}
index = name.indexOf("@");
}

if (index > 0) {
this.name = name.substring(0, index);
this.enterpriseNumber = Integer.parseInt(name.substring(index + 1));
} else {
this.name = name;
this.enterpriseNumber = RESERVED;
}
this.required = required;
this.optional = optional;
}

/**
* A Constructor that helps conformance to RFC 5424.
*
* @param name The name portion of the id.
* @param enterpriseNumber The enterprise number.
* @param required The list of keys that are required for this id.
* @param optional The list of keys that are optional for this id.
*/
public StructuredDataId(String name, int enterpriseNumber, String[] required, String[] optional) {
if (name == null) {
throw new IllegalArgumentException("No structured id name was supplied");
}
if (enterpriseNumber <= 0) {
throw new IllegalArgumentException("No enterprise number was supplied");
}
this.name = name;
this.enterpriseNumber = enterpriseNumber;
String id = enterpriseNumber < 0 ? name : name + "@" + enterpriseNumber;
if (id.length() > 32) {
throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + id);
}
this.required = required;
this.optional = optional;
}

public StructuredDataId makeId(StructuredDataId id) {
if (id == null) {
return this;
}
return makeId(id.getName(), id.getEnterpriseNumber());
}

public StructuredDataId makeId(String defaultId, int enterpriseNumber) {
String id;
String[] req;
String[] opt;
if (enterpriseNumber <= 0) {
return this;
}
if (this.name != null) {
id = this.name;
req = this.required;
opt = this.optional;
} else {
id = defaultId;
req = null;
opt = null;
}

return new StructuredDataId(id, enterpriseNumber, req, opt);
}

public String[] getRequired() {
return required;
}

public String[] getOptional() {
return optional;
}

public String getName() {
return name;
}

public int getEnterpriseNumber() {
return enterpriseNumber;
}

public boolean isReserved() {
return enterpriseNumber <= 0;
}

public String toString() {
return isReserved() ? name : name + "@" + enterpriseNumber;
}
}
16 changes: 8 additions & 8 deletions slf4j-api/src/main/java/org/slf4j/StructuredDataImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public class StructuredDataImpl implements StructuredData {

private Map data = new HashMap();

private StructuredData.Id id;
private StructuredDataId id;

private String message;

private String type;

public StructuredDataImpl(final String id, final String msg, final String type) {
this.id = new Id(id, null, null);
this.id = new StructuredDataId(id, null, null);
this.message = msg;
this.type = type;
}

public StructuredDataImpl(final StructuredData.Id id, final String msg, final String type) {
public StructuredDataImpl(final StructuredDataId id, final String msg, final String type) {
this.id = id;
this.message = msg;
this.type = type;
Expand All @@ -36,15 +36,15 @@ protected StructuredDataImpl() {

}

public StructuredData.Id getId() {
public StructuredDataId getId() {
return id;
}

protected void setId(String id) {
this.id = new StructuredData.Id(id, null, null);
this.id = new StructuredDataId(id, null, null);
}

protected void setId(StructuredData.Id id) {
protected void setId(StructuredDataId id) {
this.id = id;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ public String asString(String format) {
* @param maps Additional data to include.
* @return The formatted String.
*/
public final String asString(String format, StructuredData.Id structuredDataId, Map[] maps) {
public final String asString(String format, StructuredDataId structuredDataId, Map[] maps) {
StringBuffer sb = new StringBuffer();
boolean full = FULL.equals(format);
if (full) {
Expand All @@ -106,7 +106,7 @@ public final String asString(String format, StructuredData.Id structuredDataId,
}
sb.append(getType()).append(" ");
}
StructuredData.Id id = getId();
StructuredDataId id = getId();
if (id != null) {
id = id.makeId(structuredDataId);
} else {
Expand Down
4 changes: 2 additions & 2 deletions slf4j-ext/src/main/java/org/slf4j/ext/EventData.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package org.slf4j.ext;

import org.slf4j.StructuredDataImpl;
import org.slf4j.StructuredData;
import org.slf4j.StructuredDataId;

import java.io.Serializable;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -386,7 +386,7 @@ public void putAll(Map<? extends String, ?> map) {
public Object remove(Object key) {
Object oldValue = get(key);
if (EVENT_ID.equals(key)) {
setId((StructuredData.Id) null);
setId((StructuredDataId) null);
} else if (EVENT_MESSAGE.equals(key)) {
setMessage(null);
} else if (EVENT_TYPE.equals(key)) {
Expand Down

0 comments on commit 1209c20

Please sign in to comment.