T
- value type@PublicApi public final class ValueFormat<T> extends Object
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.
Modifier and Type | Field and Description |
---|---|
static ValueFormat<Object> |
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.
|
static ValueFormat<Boolean> |
BOOLEAN
BOOLEAN value contains simple boolean value.
|
static ValueFormat<Long> |
DURATION
DURATION format contain the number of milliseconds between two points in time.
|
static ValueFormat<String> |
HTML
HTML values can be shown on a web page.
|
static ValueFormat<String> |
ID
ID is a special format for values that represent entities.
|
static ValueFormat<List> |
JSON_ARRAY
JSON_ARRAY values are Java lists, ready to be converted to a JSON array.
|
static ValueFormat<Map> |
JSON_OBJECT
JSON_OBJECT values are Java maps, ready to be converted to a JSON object.
|
static ValueFormat<Number> |
NUMBER
NUMBER values are numeric and usually are either
Long or Double . |
static ValueFormat<Comparable> |
ORDER
ORDER values are special values that can be used to sort by this attributes.
|
static ValueFormat<String> |
TEXT
TEXT values are plain text.
|
static ValueFormat<Long> |
TIME
TIME values contain Epoch time in milliseconds.
|
Constructor and Description |
---|
ValueFormat(String formatId,
Class<T> valueClass)
Creates a new value format.
|
Modifier and Type | Method and Description |
---|---|
AttributeSpec<T> |
cast(AttributeSpec<?> spec)
Performs type checking and cast of an arbitrary attribute specification to the given format.
|
boolean |
equals(Object o) |
String |
getFormatId()
Returns the unique ID of the value format.
|
static ValueFormat<?> |
getStandardFormat(String formatId)
Returns a standard format (declared in this class) given its format ID.
|
Class<T> |
getValueClass()
Returns the Java class of the values in this format.
|
int |
hashCode() |
String |
toString() |
public static final ValueFormat<String> HTML
public static final ValueFormat<String> TEXT
public static final ValueFormat<String> ID
public static final ValueFormat<Number> NUMBER
Long
or Double
. When interpreting NUMBER values,
try to avoid casts and use Number.longValue()
and Number.doubleValue()
instead.public static final ValueFormat<Boolean> BOOLEAN
public static final ValueFormat<Long> TIME
public static final ValueFormat<Long> DURATION
public static final ValueFormat<Map> JSON_OBJECT
public static final ValueFormat<List> JSON_ARRAY
public static final ValueFormat<Object> ANY
public static final ValueFormat<Comparable> 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 TotalOrder
or ComparableTuple
.
@NotNull public String getFormatId()
@NotNull public Class<T> getValueClass()
@NotNull public AttributeSpec<T> cast(@NotNull AttributeSpec<?> spec)
Performs type checking and cast of an arbitrary attribute specification to the given format.
This method should be used only to cast AttributeSpec
type parameter to the desired format.
It does not convert AttributeSpec
, so if the format is different, an exception will be thrown.
spec
- attribute spec with unknown typeT
ClassCastException
- in case attribute specification is of different formatAttributeSpec.as(ValueFormat)
@Nullable public static ValueFormat<?> getStandardFormat(String formatId)
formatId
- format IDnull
if there's no such standard formatCopyright © 2024 Tempo Software. All Rights Reserved.