Class 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.getPropertyId() is the property ID and you can access the value using 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 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 ID
        type - property type
        Throws:
        IllegalArgumentException - if name or type is null
      • PropertyDef

        public PropertyDef​(String name,
                           Class<T> type,
                           T defaultValue)
        Primary constructor.
        Parameters:
        name - property name; also serves as the property ID
        type - property type
        defaultValue - default value for the property; may be null
        Throws:
        IllegalArgumentException - if name or type is 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 as getName().
        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 simple ObjectProperty using the given value.
        Parameters:
        value - property value
        readOnly - whether property should be read-only
        Returns:
        new property
      • createProperty

        public ObjectProperty<T> createProperty​(T value)
        Create a read/write ObjectProperty using the given value.

        Equivalent to:

          createProperty()(value, false);
          
        Parameters:
        value - property value
        Returns:
        new property
      • get

        public Property<T> get​(Item item)
        Get the property that this instance represents from the given Item.
        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 given Container.
        Parameters:
        container - the container containing the items
        itemId - 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 property is null, otherwise property
        Throws:
        ClassCastException - if property has a different type than this definition
      • addTo

        public boolean addTo​(Container container)
        Add a property represented by this instance to the given Container.
        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 given Item.
        Parameters:
        item - the item to add the property to
        property - 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 given Table.
        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 given Item.
        Parameters:
        item - item with property
        Returns:
        property value, or null if not found
        Throws:
        ClassCastException - if the property in item has the wrong type
      • isSortable

        public boolean isSortable()
        Determine whether this instance supports sorting property values.

        The implementation in PropertyDef returns true if this instance's type implements Comparable.

        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 PropertyDef sorts null values first, then delegates to Comparable if the values implement it, or else throws UnsupportedOperationException.

        Parameters:
        value1 - first value, possibly null
        value2 - second value, possibly null
        Returns:
        negative, zero, or positive based on comparing value1 to value2
        Throws:
        UnsupportedOperationException - if this instance does not support sorting property values
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object