Class OptionsByType<T>

  • Type Parameters:
    T - the base type of the options in the collection
    All Implemented Interfaces:
    com.tangosol.io.ExternalizableLite, com.tangosol.io.pof.PortableObject, Serializable
    Direct Known Subclasses:
    OptionsByType.EmptyOptionsByType

    public class OptionsByType<T>
    extends Object
    implements com.tangosol.io.ExternalizableLite, com.tangosol.io.pof.PortableObject
    A mutable collection of zero or more values, typically called an options, internally arranged as a map, keyed by the concrete type of each option in the collection.
    Since:
    21.12
    Author:
    bo
    See Also:
    Serialized Form
    • Field Detail

      • m_mapOptionsByType

        protected LinkedHashMap<Class<? extends T>,​T> m_mapOptionsByType
        The map of the options, keyed by their concrete class.
      • m_clzOfOption

        protected Class<T> m_clzOfOption
        The Class of the option in the collection.
    • Constructor Detail

      • OptionsByType

        public OptionsByType()
        Constructor for ExternalizableLite support.
    • Method Detail

      • get

        public <U extends T> U get​(Class<U> clzOfOption)
        Obtains the option for a specified concrete type from the collection.

        Should the option not exist in the collection, an attempt is made to determine a suitable default based on the use of the OptionsByType.Default annotation in the specified class, firstly by looking for and evaluating the annotated "public static U getter()" method, failing that, looking for and evaluating the annotated "public static U value = ...;" field, failing that, looking for an evaluating the annotated public no args constructor and finally, failing that, looking for an annotated field on an enum (assuming the class is an enum). Failing these approaches, null is returned.

        Type Parameters:
        U - the type of value
        Parameters:
        clzOfOption - the concrete type of option to obtain
        Returns:
        the option of the specified type or if undefined, the suitable default value (or null if one can't be determined)
      • get

        public <U extends T> U get​(Class<U> clzOfOption,
                                   U defaultOption)
        Obtains the option of a specified concrete type from the collection.

        Should the type of option not exist, the specified default is returned.

        Type Parameters:
        U - the type of value
        Parameters:
        clzOfOption - the type of option to obtain
        defaultOption - the option to return if the specified type is not defined
        Returns:
        the option of the specified type or the default if it's not defined
      • contains

        public <O extends T> boolean contains​(Class<O> clzOfOption)
        Determines if an option of the specified concrete type is in the collection.
        Type Parameters:
        O - the type of option
        Parameters:
        clzOfOption - the class of option
        Returns:
        true if the class of option is in the OptionsByType false otherwise
      • contains

        public boolean contains​(T option)
        Determines if the specified option (and type) is in the OptionsByType.
        Parameters:
        option - the option
        Returns:
        true if the options is defined, false otherwise
      • asArray

        public T[] asArray()
        Obtains the current collection of options as an array.
        Returns:
        an array of options
      • from

        @SafeVarargs
        public static <T> OptionsByType<T> from​(Class<T> classOfOption,
                                                T[] baseOptions,
                                                T... options)
        Constructs an OptionsByType collection given an array of options.
        Type Parameters:
        T - the type of options
        Parameters:
        classOfOption - the Class of the base type of the options in the collection
        baseOptions - the base options
        options - the array of options
        Returns:
        an OptionsByType collection
      • add

        public OptionsByType<T> add​(T option)
        Adds an option to the collection, replacing an existing option of the same concrete type if one exists.
        Parameters:
        option - the option to add
        Returns:
        the OptionsByType to permit fluent-style method calls
      • addAll

        public OptionsByType<T> addAll​(T[] options)
        Adds an array of options to the collection, replacing existing options of the same concrete type where they exist.
        Parameters:
        options - the options to add
        Returns:
        the OptionsByType to permit fluent-style method calls
      • addAll

        public OptionsByType<T> addAll​(OptionsByType<? extends T> optionsByType)
        Adds all current options into the specified OptionsByType to this collection, replacing existing options of the same concrete type where they exist.
        Parameters:
        optionsByType - the OptionsByType to add
        Returns:
        the OptionsByType to permit fluent-style method calls
      • getClassOf

        protected Class<T> getClassOf​(T option)
        Obtains the concrete option type.
        Parameters:
        option - the option
        Returns:
        the concrete Class that directly extends / implements the value interface or null if the value is null
      • getClassOf

        protected <O extends TClass<O> getClassOf​(Class<?> clzOfOption)
        Obtains the concrete type that directly implements / extends the m_clzOfOption option Class.
        Type Parameters:
        O - the option concrete type
        Parameters:
        clzOfOption - the class that somehow implements or extends m_clzOfOption
        Returns:
        the concrete Class that directly extends / implements the m_clzOfOption class or null if the specified Class doesn't implement or extend the m_clzOfOption class
      • getDefaultFor

        protected <U extends T> U getDefaultFor​(Class<U> clzOfOption)
        Attempts to determine a "default" value for a given class.

        Aan attempt is made to determine a suitable default based on the use of the OptionsByType.Default annotation in the specified class, firstly by looking for and evaluating the annotated "public static U getter()" method, failing that, looking for and evaluating the annotated "public static U value = ...;" field, failing that, looking for an evaluating the annotated public no args constructor and finally, failing that, looking for an annotated field on an enum (assuming the class is an enum). Failing these approaches, null is returned.

        Type Parameters:
        U - the type of value
        Parameters:
        clzOfOption - the class
        Returns:
        a default value or null if a default can't be determined
      • readExternal

        public void readExternal​(DataInput in)
                          throws IOException
        Specified by:
        readExternal in interface com.tangosol.io.ExternalizableLite
        Throws:
        IOException
      • writeExternal

        public void writeExternal​(DataOutput out)
                           throws IOException
        Specified by:
        writeExternal in interface com.tangosol.io.ExternalizableLite
        Throws:
        IOException
      • readExternal

        public void readExternal​(com.tangosol.io.pof.PofReader in)
                          throws IOException
        Specified by:
        readExternal in interface com.tangosol.io.pof.PortableObject
        Throws:
        IOException
      • writeExternal

        public void writeExternal​(com.tangosol.io.pof.PofWriter out)
                           throws IOException
        Specified by:
        writeExternal in interface com.tangosol.io.pof.PortableObject
        Throws:
        IOException