Class Factory

  • Direct Known Subclasses:
    ArrayFactory, CollectionFactory, MapFactory, PrimitiveFactory

    abstract class Factory
    extends java.lang.Object
    The Factory object provides a base class for factories used to produce field values from XML elements. The goal of this type of factory is to make use of the Strategy object to determine the type of the field value. The strategy class must be assignable to the field class type, that is, it must extend it or implement it if it represents an interface. If the strategy returns a null Value then the subclass implementation determines the type used to populate the object field value.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Context context
      This is the context object used for the serialization process.
      protected java.lang.Class override
      This is the class override to used when instantiating objects.
      protected Support support
      This is used to translate all of the primitive type strings.
      protected Type type
      This is the field type that the class must be assignable to.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Factory​(Context context, Type type)
      Constructor for the Factory object.
      protected Factory​(Context context, Type type, java.lang.Class override)
      Constructor for the Factory object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Value getConversion​(InputNode node)
      This performs the conversion from the element node to a type.
      java.lang.Object getInstance()
      This is used to create a default instance of the field type.
      protected Value getOverride​(InputNode node)
      This is used to get a possible override from the provided node.
      private Type getPrimitive​(Type type, java.lang.Class expect)
      This is used to convert the Type provided as an overridden type.
      java.lang.Class getType()
      This is used to extract the type this factory is using.
      static boolean isCompatible​(java.lang.Class expect, java.lang.Class type)
      This is used to determine whether the provided base class can be assigned from the issued type.
      static boolean isInstantiable​(java.lang.Class type)
      This is used to determine whether the type given is instantiable, that is, this determines if an instance of that type can be created.
      boolean setOverride​(Type type, java.lang.Object value, OutputNode node)
      This method is used to set the override class within an element.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • context

        protected Context context
        This is the context object used for the serialization process.
      • support

        protected Support support
        This is used to translate all of the primitive type strings.
      • override

        protected java.lang.Class override
        This is the class override to used when instantiating objects.
      • type

        protected Type type
        This is the field type that the class must be assignable to.
    • Constructor Detail

      • Factory

        protected Factory​(Context context,
                          Type type)
        Constructor for the Factory object. This is given the class type for the field that this factory will determine the actual type for. The actual type must be assignable to the field type to insure that any instance can be set.
        Parameters:
        context - the contextual object used by the persister
        type - this is the property representing the field
      • Factory

        protected Factory​(Context context,
                          Type type,
                          java.lang.Class override)
        Constructor for the Factory object. This is given the class type for the field that this factory will determine the actual type for. The actual type must be assignable to the field type to insure that any instance can be set.
        Parameters:
        context - the contextual object used by the persister
        type - this is the property representing the field
        override - this is the override used for this factory
    • Method Detail

      • getType

        public java.lang.Class getType()
        This is used to extract the type this factory is using. Each factory represents a specific class, which it instantiates if required. This method provides the represented class.
        Returns:
        this returns the class represented by the factory
      • getInstance

        public java.lang.Object getInstance()
                                     throws java.lang.Exception
        This is used to create a default instance of the field type. It is up to the subclass to determine how to best instantiate an object of the field type that best suits. This is used when the empty value is required or to create the default type instance.
        Returns:
        a type which is used to instantiate the collection
        Throws:
        java.lang.Exception
      • getOverride

        protected Value getOverride​(InputNode node)
                             throws java.lang.Exception
        This is used to get a possible override from the provided node. If the node provided is an element then this checks for a specific class override using the Strategy object. If the strategy cannot resolve a class then this will return null. If the resolved Value is not assignable to the field then this will thrown an exception.
        Parameters:
        node - this is the node used to search for the override
        Returns:
        this returns null if no override type can be found
        Throws:
        java.lang.Exception - if the override type is not compatible
      • setOverride

        public boolean setOverride​(Type type,
                                   java.lang.Object value,
                                   OutputNode node)
                            throws java.lang.Exception
        This method is used to set the override class within an element. This delegates to the Strategy implementation, which depending on the implementation may add an attribute of a child element to describe the type of the object provided to this.
        Parameters:
        type - this is the class of the field type being serialized
        node - the XML element that is to be given the details
        Throws:
        java.lang.Exception - thrown if an error occurs within the strategy
      • getPrimitive

        private Type getPrimitive​(Type type,
                                  java.lang.Class expect)
                           throws java.lang.Exception
        This is used to convert the Type provided as an overridden type. Overriding the type in this way ensures that if a primitive type, represented as a boxed object, is given to a strategy then the strategy will see a match in the types used.
        Parameters:
        type - this is the field or method that is a primitive
        expect - this is the boxed object type to be converted
        Returns:
        this returns a type representing the boxed type
        Throws:
        java.lang.Exception
      • getConversion

        public Value getConversion​(InputNode node)
                            throws java.lang.Exception
        This performs the conversion from the element node to a type. This is where the Strategy object is consulted and asked for a class that will represent the provided XML element. This will, depending on the strategy implementation, make use of attributes and/or elements to determine the type for the field.
        Parameters:
        node - this is the element used to extract the override
        Returns:
        this returns null if no override type can be found
        Throws:
        java.lang.Exception - thrown if the override class cannot be loaded
      • isCompatible

        public static boolean isCompatible​(java.lang.Class expect,
                                           java.lang.Class type)
        This is used to determine whether the provided base class can be assigned from the issued type. For an override to be compatible with the field type an instance of the override type must be assignable to the field value.
        Parameters:
        expect - this is the field value present the the object
        type - this is the specialized type that will be assigned
        Returns:
        true if the field type can be assigned the type value
      • isInstantiable

        public static boolean isInstantiable​(java.lang.Class type)
        This is used to determine whether the type given is instantiable, that is, this determines if an instance of that type can be created. If the type is an interface or an abstract class then this will return false.
        Parameters:
        type - this is the type to check the modifiers of
        Returns:
        false if the type is an interface or an abstract class