java.lang.Object
org.jhotdraw8.fxcollection.typesafekey.AbstractKey<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
Key<T>, MapAccessor<T>
Direct Known Subclasses:
NonNullObjectKey, NullableObjectKey, TransientKey

public abstract class AbstractKey<T> extends Object implements Key<T>
A name which provides typesafe access to a map entry.

A Key has a name, a type and a default value.

The following code example shows how to set and get a value from a map.

 
 String value = "Werner";
 Key<String> stringKey = new Key("name",String.class,null);
 Map<Key<?>,Object> map = new HashMap<>();
 stringKey.put(map, value);
 
 

Note that Key is not a value type. Thus using two distinct instances of a Key will result in two distinct entries in the hash map, even if both keys have the same name.

Author:
Werner Randelshofer
  • Constructor Details

    • AbstractKey

      public AbstractKey(String name, Type clazz)
      Creates a new instance with the specified name, type token class, default value null, and allowing null values.
      Parameters:
      name - The name of the key.
      clazz - The type of the value.
    • AbstractKey

      public AbstractKey(String name, TypeToken<T> clazz, T initialValue)
    • AbstractKey

      public AbstractKey(String name, Type clazz, @Nullable T initialValue)
      Creates a new instance with the specified name, type token class, default value, and allowing or disallowing null values.
      Parameters:
      name - The name of the key.
      clazz - The type of the value.
      initialValue - The default value.
    • AbstractKey

      public AbstractKey(String name, Type clazz, boolean isNullable, @Nullable T initialValue)
      Creates a new instance with the specified name, type token class, default value, and allowing or disallowing null values.
      Parameters:
      name - The name of the key.
      clazz - The type of the value.
      isNullable - Whether the value may be set to null
      initialValue - The default value.
    • AbstractKey

      public AbstractKey(String name, Type clazz, @Nullable Class<?>[] typeParameters, boolean isNullable, @Nullable T initialValue)
    • AbstractKey

      public AbstractKey(@Nullable String name, Type clazz, boolean isNullable, boolean isTransient, @Nullable T initialValue)
  • Method Details

    • getName

      public String getName()
      Returns the name string.
      Specified by:
      getName in interface MapAccessor<T>
      Returns:
      name string.
    • getValueType

      public Type getValueType()
      Description copied from interface: MapAccessor
      Returns the value type of this map accessor.

      If the value type has type parameters, make sure to create it using TypeToken.

      Specified by:
      getValueType in interface MapAccessor<T>
    • getDefaultValue

      public @Nullable T getDefaultValue()
      Description copied from interface: MapAccessor
      Returns the default value of this map accessor.

      The default value of an attribute or property is the value used when that attribute or property is not specified.

      Specified by:
      getDefaultValue in interface MapAccessor<T>
      Returns:
      the default value
    • isNullable

      public boolean isNullable()
      Description copied from interface: Key
      Whether the value may be set to null.
      Specified by:
      isNullable in interface Key<T>
      Returns:
      true if nullable
    • isTransient

      public boolean isTransient()
      Description copied from interface: MapAccessor
      Whether the value needs to be made persistent.
      Specified by:
      isTransient in interface MapAccessor<T>
      Returns:
      true if transient
    • toString

      public String toString()
      Returns the name string.
      Overrides:
      toString in class Object