Package org.dellroad.stuff.vaadin7
Class PropertyDef<T>
- java.lang.Object
-
- org.dellroad.stuff.vaadin7.PropertyDef<T>
-
- Type Parameters:
T- property's value type
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
StringValuePropertyDef
public class PropertyDef<T> extends Object implements Serializable
Defines a Vaadin property, having a name, which is also the property ID, and its type. This class provides a mechanism for the explicit naming and identification of Vaadin properties.For a given instance,
def.is the property ID and you can access the value usinggetPropertyId()def..read(item)Example:
PropertyDef<Integer> def = new PropertyDef<Integer>("age", Integer.class, -1); def.addTo(container); def.addTo(item, property); ... int age = def.read(item); ... Property prop = this.get(container, itemId);Instances are serializable if the default value is.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static Comparator<PropertyDef<?>>SORT_BY_NAMEComparator that sorts instances by name.
-
Constructor Summary
Constructors Constructor Description PropertyDef(String name, Class<T> type)Convenience contructor.PropertyDef(String name, Class<T> type, T defaultValue)Primary constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddTo(Container container)Add a property represented by this instance to the givenContainer.booleanaddTo(Item item, Property<T> property)Add a property represented by this instance to the givenItem.booleanaddTo(Table table)Add a property represented by this instance to the givenTable.Property<T>cast(Property<?> property)Verify that the given property has the same Java type as this property definition.ObjectProperty<T>createProperty()Create a read/writeObjectPropertyusing the default value.ObjectProperty<T>createProperty(T value)Create a read/writeObjectPropertyusing the given value.ObjectProperty<T>createProperty(T value, boolean readOnly)Create a simpleObjectPropertyusing the given value.booleanequals(Object obj)Property<T>get(Container container, Object itemId)Get the property that this instance represents from the givenContainer.Property<T>get(Item item)Get the property that this instance represents from the givenItem.TgetDefaultValue()Get the default value for this property.StringgetName()Get the name of this property.StringgetPropertyId()Get the ID of this property.Class<T>getType()Get the type of the property value that this instance represents.inthashCode()booleanisSortable()Determine whether this instance supports sorting property values.Tread(Item item)Read the property that this instance represents from the givenItem.intsort(T value1, T value2)Sort two values of this property.StringtoString()
-
-
-
Field Detail
-
SORT_BY_NAME
public static final Comparator<PropertyDef<?>> SORT_BY_NAME
Comparator that sorts instances by name.
-
-
Constructor Detail
-
PropertyDef
public PropertyDef(String name, Class<T> type)
Convenience contructor.Equivalent to:
PropertyDef(name, type, null);
- Parameters:
name- property name; also serves as the property IDtype- property type- Throws:
IllegalArgumentException- ifnameortypeis null
-
PropertyDef
public PropertyDef(String name, Class<T> type, T defaultValue)
Primary constructor.- Parameters:
name- property name; also serves as the property IDtype- property typedefaultValue- default value for the property; may be null- Throws:
IllegalArgumentException- ifnameortypeis null
-
-
Method Detail
-
getName
public String getName()
Get the name of this property. This is also used as the property ID.- Returns:
- property name, never null
-
getPropertyId
public String getPropertyId()
Get the ID of this property. Returns the same thing asgetName().- Returns:
- property name, never null
-
getType
public Class<T> getType()
Get the type of the property value that this instance represents.- Returns:
- property type, never null
-
getDefaultValue
public T getDefaultValue()
Get the default value for this property.- Returns:
- property default value
-
createProperty
public ObjectProperty<T> createProperty(T value, boolean readOnly)
Create a simpleObjectPropertyusing the given value.- Parameters:
value- property valuereadOnly- whether property should be read-only- Returns:
- new property
-
createProperty
public ObjectProperty<T> createProperty(T value)
Create a read/writeObjectPropertyusing the given value.Equivalent to:
createProperty()(value, false);- Parameters:
value- property value- Returns:
- new property
-
createProperty
public ObjectProperty<T> createProperty()
Create a read/writeObjectPropertyusing the default value.Equivalent to:
createProperty()(def.getDefaultValue());- Returns:
- new property
-
get
public Property<T> get(Item item)
Get the property that this instance represents from the givenItem.- Parameters:
item- item with property- Returns:
- property, or null if not found
- Throws:
ClassCastException- if the property found has a different type than this instance
-
get
public Property<T> get(Container container, Object itemId)
Get the property that this instance represents from the givenContainer.- Parameters:
container- the container containing the itemsitemId- the ID of the item containing the property- Returns:
- property, or null if not found
- Throws:
ClassCastException- if the property found has a different type than this instance
-
cast
public Property<T> cast(Property<?> property)
Verify that the given property has the same Java type as this property definition.This essentially verifies that
property.getType() == this.getType().- Parameters:
property- the property to verify; may be null- Returns:
- null if
propertyis null, otherwiseproperty - Throws:
ClassCastException- ifpropertyhas a different type than this definition
-
addTo
public boolean addTo(Container container)
Add a property represented by this instance to the givenContainer.- Parameters:
container- the container to add the property to- Returns:
- true if the operation succeeded, false if not
- Throws:
UnsupportedOperationException- if the operation is not supported
-
addTo
public boolean addTo(Item item, Property<T> property)
Add a property represented by this instance to the givenItem.- Parameters:
item- the item to add the property toproperty- the property to be added to the item and identified by this instance's name- Returns:
- true if the operation succeeded, false if not
- Throws:
UnsupportedOperationException- if the operation is not supported
-
addTo
public boolean addTo(Table table)
Add a property represented by this instance to the givenTable.- Parameters:
table- the table to add the property to- Returns:
- true if the operation succeeded, false if not
- Throws:
UnsupportedOperationException- if the operation is not supported
-
read
public T read(Item item)
Read the property that this instance represents from the givenItem.- Parameters:
item- item with property- Returns:
- property value, or null if not found
- Throws:
ClassCastException- if the property initemhas the wrong type
-
isSortable
public boolean isSortable()
Determine whether this instance supports sorting property values.The implementation in
PropertyDefreturns true if this instance's type implementsComparable.- Returns:
- true if this instance supports sorting property values
- See Also:
sort()
-
sort
public int sort(T value1, T value2)
Sort two values of this property. Optional operation.The implementation in
PropertyDefsorts null values first, then delegates toComparableif the values implement it, or else throwsUnsupportedOperationException.- Parameters:
value1- first value, possibly nullvalue2- second value, possibly null- Returns:
- negative, zero, or positive based on comparing
value1tovalue2 - Throws:
UnsupportedOperationException- if this instance does not support sorting property values
-
-