Class BaseUnion

  • All Implemented Interfaces:
    Field, Serializable, Union

    public class BaseUnion
    extends BaseField
    implements Union
    Base class for implementing an union. It is also a complete implementation.
    Author:
    mse
    • Constructor Detail

      • BaseUnion

        public BaseUnion()
        Constructor for a variant union (aka any type).
      • BaseUnion

        public BaseUnion​(String[] fieldNames,
                         Field[] fields)
        Constructor for an union field.
        Parameters:
        fieldNames - The field names for the subfields
        fields - The array of nodes definitions for the nodes of the structure.
      • BaseUnion

        public BaseUnion​(String id,
                         String[] fieldNames,
                         Field[] fields)
        Constructor for an union field.
        Parameters:
        id - The identification string for the union.
        fieldNames - The field names for the subfields
        fields - The union fields (members).
        Throws:
        IllegalArgumentException - if id is null or empty.
    • Method Detail

      • getID

        public String getID()
        Description copied from interface: Field
        Get the identification string.
        Specified by:
        getID in interface Field
        Returns:
        the identification string, can be empty
      • getField

        public Field getField​(String name)
        Description copied from interface: Union
        Get the Field for the specified fieldName
        Specified by:
        getField in interface Union
        Parameters:
        name - the name of the field
        Returns:
        the Field that describes the field
      • getFieldIndex

        public int getFieldIndex​(String name)
        Description copied from interface: Union
        Get the index of the specified field.
        Specified by:
        getFieldIndex in interface Union
        Parameters:
        name - the name of the field
        Returns:
        the index or -1 if fieldName is not a field in the union
      • getField

        public Field getField​(int fieldIndex)
        Description copied from interface: Union
        Get a sub fields by index.
        Specified by:
        getField in interface Union
        Parameters:
        fieldIndex - the field index
        Returns:
        the field at specified index
      • getField

        public <T extends Field> T getField​(Class<T> c,
                                            String fieldName)
        Description copied from interface: Union
        Get the Field for the specified fieldName
        Specified by:
        getField in interface Union
        Type Parameters:
        T - the expected type of the Field of the requested field
        Parameters:
        c - class object modeling the class T of expected type of the requested field
        fieldName - the fieldName
        Returns:
        the Field or null if the field does not exist, or the field is not of c type
      • getField

        public <T extends Field> T getField​(Class<T> c,
                                            int fieldOffset)
        Description copied from interface: Union
        Get a subfield by index.
        Specified by:
        getField in interface Union
        Type Parameters:
        T - the expected type of the Field of the requested field
        Parameters:
        c - class object modeling the class T of expected type of the requested field
        fieldOffset - the index
        Returns:
        the Field or null if the index is invalid, or the field is not of c type
      • getFieldNames

        public String[] getFieldNames()
        Description copied from interface: Union
        Get the array of the subfield names
        Specified by:
        getFieldNames in interface Union
        Returns:
        the array of the subfield names
      • getFieldName

        public String getFieldName​(int fieldIndex)
        Description copied from interface: Union
        Get the name of the subfield by index.
        Specified by:
        getFieldName in interface Union
        Parameters:
        fieldIndex - the field index
        Returns:
        the name of the field
      • getFields

        public Field[] getFields()
        Description copied from interface: Union
        Get a list of union fields (members).
        Specified by:
        getFields in interface Union
        Returns:
        an array of fields (can be empty), non-null
      • isVariant

        public boolean isVariant()
        Description copied from interface: Union
        Check if this union is variant union (aka any type).
        Specified by:
        isVariant in interface Union
        Returns:
        true if this union is variant union, otherwise false
      • toString

        public void toString​(StringBuilder buf,
                             int indentLevel)
        Description copied from interface: Field
        Convert to a string
        Specified by:
        toString in interface Field
        Specified by:
        toString in class BaseField
        Parameters:
        buf - buffer for the result
        indentLevel - the indentation level
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object