Class PrimitiveTypeProperties

java.lang.Object
org.apache.sis.internal.jaxb.PrimitiveTypeProperties

public final class PrimitiveTypeProperties extends Object
A workaround for attaching properties (nilreason, href, etc.) to primitive type wrappers. The normal approach in SIS is to implement the NilObject interface. However, we cannot do so when the object is a final Java class like Boolean, Integer, Double or String. This class provides a workaround using specific instances of those wrappers.
Since:
0.4
Version:
0.4
See Also:
  • Field Details

    • SENTINEL_VALUES

      private static final Map<Object,Object> SENTINEL_VALUES
      The map where to store specific instances. Keys are instances of the primitive wrappers considered as nil. Values are the NilReason why the primitive is missing, or any other property we may want to attach.

      Identity comparisons

      We really need an identity hash map; using the Object.equals(Object) method is not allowed here. This is because "nil values" are real values. For example if the type is Integer, then the nil value is an Integer instance having the value 0. We don't want to consider every 0 integer value as nil, but only the specific Integer instance used as sentinel value for nil.

      Weak references

      We cannot use weak value references, because we don't want the NilReason (the map value) to be lost while the sentinel value (the map key) is still in use. We could use weak references for the keys, but JDK 7 does not provides any map implementation which is both an IdentityHashMap and a WeakHashMap. For now we do not use weak references. This means that if a user creates a custom NilReason by a call to NilReason.valueOf(String), and if he uses that nil reason for a primitive type, then that custom NilReason instance and its sentinel values will never be garbage-collected. We presume that such cases will be rare enough for not being an issue in practice.

      Synchronization

      All accesses to this map shall be synchronized on the map object.
  • Constructor Details

    • PrimitiveTypeProperties

      private PrimitiveTypeProperties()
      Do not allow instantiation of this class.
  • Method Details

    • isValidKey

      private static boolean isValidKey(Object primitive)
      Returns true if the given type is a valid key. This PrimitiveTypeProperties class is a workaround to be used only for final classes on which we have no control. Non-final classes shall implement NilObject instead.
    • associate

      public static void associate(Object primitive, Object property)
      Associates the given property to the given primitive. The primitive argument shall be a specific instance created by the new keyword, not a shared instance link Boolean.FALSE or the values returned by Integer.valueOf(int).
      Parameters:
      primitive - the Boolean, Integer, Double or String specific instance.
      property - the NilReason or other property to associate to the given instance.
    • property

      public static Object property(Object primitive)
      Returns the property of the given primitive type, or null if none.
      Parameters:
      primitive - the Boolean, Integer, Double or String specific instance.
      Returns:
      the property associated to the given instance, or null if none.