Class ValueFormat<T>
- Type Parameters:
T- value type
ValueFormat is used as a part of AttributeSpec to define in what format the value should be
returned.
There are a number of well-known formats, defined in this class. However, custom formats can be created and used on the server side by Structure extensions. Only the standard formats can be sent over via REST API.
It is important to note the difference between value format and value type. The format defines value type but
also the semantics of the value. For example, both HTML and TEXT formats provide String
values, but HTML values can actually be rendered as-is on a web page (without HTML escaping) and they are unsuitable
to be shown to the user as plain text.
Different value types must have different IDs. When creating a new value type, it's recommended to have your package prefix as a part of the value type ID.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ValueFormat<Object>ANY format is suitable when the requesting code can accept object of any type and there's no knowledge at development time what format of the attribute is going to be available.static final ValueFormat<Boolean>BOOLEAN value contains simple boolean value.static final ValueFormat<Long>DURATION format contain the number of milliseconds between two points in time.static final ValueFormat<String>HTML values can be shown on a web page.static final ValueFormat<String>ID is a special format for values that represent entities.static final ValueFormat<List>JSON_ARRAY values are Java lists, ready to be converted to a JSON array.static final ValueFormat<Map>JSON_OBJECT values are Java maps, ready to be converted to a JSON object.static final ValueFormat<Number>NUMBER values are numeric and usually are eitherLongorDouble.static final ValueFormat<Comparable>ORDER values are special values that can be used to sort by this attributes.static final ValueFormat<String>TEXT values are plain text.static final ValueFormat<Long>TIME values contain Epoch time in milliseconds. -
Constructor Summary
ConstructorsConstructorDescriptionValueFormat(String formatId, Class<T> valueClass) Creates a new value format. -
Method Summary
Modifier and TypeMethodDescriptioncast(AttributeSpec<?> spec) Performs type checking and cast of an arbitrary attribute specification to the given format.booleanReturns the unique ID of the value format.static ValueFormat<?>getStandardFormat(String formatId) Returns a standard format (declared in this class) given its format ID.Returns the Java class of the values in this format.inthashCode()toString()
-
Field Details
-
HTML
HTML values can be shown on a web page. Most attributes can provide HTML values. -
TEXT
TEXT values are plain text. If shown on a web page, they must be HTML-escaped. -
ID
ID is a special format for values that represent entities. Some attributes may provide ID format in case the client needs only the ID of the value. If the ID is numeric, it is converted to String. -
NUMBER
NUMBER values are numeric and usually are eitherLongorDouble. When interpreting NUMBER values, try to avoid casts and useNumber.longValue()andNumber.doubleValue()instead. -
BOOLEAN
BOOLEAN value contains simple boolean value. -
TIME
TIME values contain Epoch time in milliseconds. -
DURATION
DURATION format contain the number of milliseconds between two points in time. -
JSON_OBJECT
JSON_OBJECT values are Java maps, ready to be converted to a JSON object. The maps must contain only String keys and only JSON-compatible values: numbers, strings, booleans, maps or lists. -
JSON_ARRAY
JSON_ARRAY values are Java lists, ready to be converted to a JSON array. The lists must contain only JSON-compatible values: numbers, strings, booleans, maps or lists. -
ANY
ANY format is suitable when the requesting code can accept object of any type and there's no knowledge at development time what format of the attribute is going to be available. This format is used for nested attribute specs that can provide results of any format. The Java type may be any class. -
ORDER
ORDER values are special values that can be used to sort by this attributes. The Java type of the value may be any class, as long as a) they are
Comparable, and b) the ORDER values for the same item type will be the same Java type.Values are compared using
TotalOrderorComparableTuple.
-
-
Constructor Details
-
ValueFormat
Creates a new value format.- Parameters:
formatId- unique format ID (use your package prefix to guarantee uniqueness). Must not contain colon (":") or be unreasonably long.valueClass- Java type of the values
-
-
Method Details
-
getFormatId
Returns the unique ID of the value format. -
getValueClass
Returns the Java class of the values in this format. -
cast
Performs type checking and cast of an arbitrary attribute specification to the given format. This method should be used only to cast
AttributeSpectype parameter to the desired format. It does not convertAttributeSpec, so if the format is different, an exception will be thrown.- Parameters:
spec- attribute spec with unknown type- Returns:
- attribute spec with type
T - Throws:
ClassCastException- in case attribute specification is of different format- See Also:
-
equals
-
hashCode
public int hashCode() -
toString
-
getStandardFormat
Returns a standard format (declared in this class) given its format ID.- Parameters:
formatId- format ID- Returns:
- the standard format or
nullif there's no such standard format
-