T - is the generic type of the property.public class TypedProperty<T> extends Object implements PojoPropertyPath, AttributeReadTitle<String>
segment. In case the property is not directly accessed but via traversing multiple
properties of an object-web, then getParentPath() will return the path to the parent object
containing the actual property identified by getSegment(). The entire path including the
segment is accessible via getPojoPath(). The actual type of the property may
be returned by getPropertyType() (if provided at construction time) but is typically omitted.
public interface MyObject {
TypedProperty<String> PROPERTY_NAME = new TypedProperty<>(String.class, "name");
TypedProperty<Integer> PROPERTY_AGE = new TypedProperty<>(Integer.class, "age");
TypedProperty<String> PROPERTY_ADDRESS_CITY = new TypedProperty<>(String.class, "address", MyAddress.TYPED_PROPERTY_CITY);
String getName();
// may also return int instead of Integer...
Integer getAge();
MyAddress getAddress();
}
Now assuming that you have a generic signature like this:
public interface Query {
<T> addCondition(TypedProperty<T> property, Operator operator, T value);
}
Then you can combine these two things as following:
Query myQuery = newQuery(); myQuery.addCondition(MyObject.TYPED_PROPERTY_AGE, Operator.GREATER_THAN, Integer.valueOf(42));Now in case you have to change the type of the property "age" for whatever reason to some other type, then you change the type of the getter in parallel with the according
TypedProperty constant. In the
same moment the compiler will reject the above code to create the query and you will be able to fix all
places where generic access to the "age" property takes place. When using simple string references for
property access (addCondition(String, Operator, Object)) instead you would have to pray that
you did NOT miss anything in the code or you get errors in production at runtime.| Modifier and Type | Field and Description |
|---|---|
private String |
parentPath |
private String |
pojoPath |
private Class<T> |
propertyType |
private String |
segment |
private String |
title |
SEPARATOR| Constructor and Description |
|---|
TypedProperty(Class<T> propertyType,
String segment)
The constructor.
|
TypedProperty(Class<T> propertyType,
String segment,
String parentPath)
The constructor.
|
TypedProperty(String segment)
The constructor.
|
TypedProperty(String title,
Class<T> propertyType,
String segment)
The constructor.
|
TypedProperty(String title,
Class<T> propertyType,
String segment,
String parentPath)
The constructor.
|
TypedProperty(String segment,
String parentPath)
The constructor.
|
TypedProperty(TypedProperty<T> property,
String path)
The constructor.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
appendPath(StringBuilder buffer,
String... segments)
|
static String |
createPath(String... segments)
This method creates a
pojo property path specified by the given
segments. |
String |
getParentPath()
This method gets the parent-path of this path.
|
String |
getPojoPath()
This method gets the actual
Pojo path represented by this object. |
Class<T> |
getPropertyType()
This method gets the type of this property.
|
String |
getSegment()
This method gets the last segment of this current
Pojo path. |
String |
getTitle()
This title is used to display the property name to end-users.
|
String |
toString() |
private final String segment
getSegment()private final String pojoPath
getPojoPath()private final Class<T> propertyType
getPropertyType()private final String parentPath
getParentPath()private final String title
getTitle()public TypedProperty(String segment)
public TypedProperty(String segment, String parentPath)
segment - - see getSegment().parentPath - - see getParentPath() and createPath(String...).public TypedProperty(Class<T> propertyType, String segment)
propertyType - - see getPropertyType().segment - is the segment and also the entire pojo path.public TypedProperty(String title, Class<T> propertyType, String segment)
title - - see getTitle().propertyType - - see getPropertyType().segment - is the segment and also the entire pojo path.public TypedProperty(TypedProperty<T> property, String path)
property - is the existing TypedProperty to create as nested property.path - is the parent path to the given property. See also
createPath(String...).public TypedProperty(Class<T> propertyType, String segment, String parentPath)
propertyType - - see getPropertyType().segment - - see getSegment().parentPath - - see getParentPath() and createPath(String...).public TypedProperty(String title, Class<T> propertyType, String segment, String parentPath)
title - - see getTitle().propertyType - - see getPropertyType().segment - - see getSegment().parentPath - - see getParentPath() and createPath(String...).public String getSegment()
Pojo path. E.g. if
this path represents "foo.bar.property" then this method would return
"property".getSegment in interface PojoPropertyPathpublic String getTitle()
segment. For NLS the title
will be used as key for resource bundles (outside of this class) and should therefore be stable and not
contain special characters.
This method gets the title of this object. Object.toString() of the result if NOT nullgetTitle in interface AttributeReadTitle<String>null if not set. The string-representation of
the result (if NOT null) needs to be suitable for end-users.public String getPojoPath()
Pojo path represented by this object. As an
example this method may return "foo.bar.property".getPojoPath in interface PojoPropertyPathPojoPropertyPath.public String getParentPath()
"foo.bar.property" then this method would return
"foo.bar".getParentPath in interface PojoPropertyPathnull if this is the root-segment.public Class<T> getPropertyType()
TypedProperty and requires the property type to be set this must
be explicitly documented. By default the type will be omitted, to lower the barrier of defining a
TypedProperty for each property of an entity.null if NOT available.public static String createPath(String... segments)
pojo property path specified by the given
segments.segments - is the array of properties to concat with dot (".").String.public static void appendPath(StringBuilder buffer, String... segments)
buffer - is the StringBuilder to append to.segments - are the path segments for the property.Copyright © 2001–2015 mmm-Team. All rights reserved.