Package com.gengoai

Class HierarchicalEnumValue<T extends HierarchicalEnumValue<T>>

  • All Implemented Interfaces:
    Tag, Serializable, Cloneable, Comparable<T>

    public abstract class HierarchicalEnumValue<T extends HierarchicalEnumValue<T>>
    extends EnumValue<T>

    A enum like object that can have elements created at runtime as needed and which have a parent associated with them. As with EnumValues, elements are singleton objects and can have their equality safely checked using the == operator. Their implementation of Tag.isInstance(Tag) returns true if the element is equal to or a descendant of the tag being compared against. Elements can have their parents assigned at later time as long up until a non-null parent has been set.

    The python script in the mango tools directory (tools/enumGen.py) bootstraps the creation of basic HierarchicalEnumValue. As with enum values the names associated with EnumValues are normalized to be uppercase and have all whitespace replaced by underscores with consecutive whitespace becoming a single underscore.

    Examples of common usage patterns for HierarchicalEnumValue types generated using tools/enumGen.py are as follows:

     
        //Enum values can be retrieved or created using the create method.
        MyEnum animal = MyEnum.create("animal");
        MyEnum dog = MyEnum.create("dog", animal);
        MyEnum pug = MyEnum.create("pug", dog);
    
        MyEnum thing = MyEnum.create("thing");
    
        //Now we want to set the parent of animal to thing. This is ok, because we did not set the parent yet, and
        // do not have one defined via a configuration property.
        MyEnum.create("animal", thing);
    
        //Will evaluate isInstance using the hierarchy
        boolean isAnimal = pug.isInstance(animal);
    
        //A leaf element is one that doesn't have children
        boolean isLeaf = pug.isLeaf();
    
        //A root element is one that doesn't have a parent.
        //Note: this can change since parents can be updated.
        boolean isRoot = thing.isRoot();
    
        //Can get the children of an element using the getChildren method
        List<MyEnum> typesOfAnimals = animal.getChildren();
    
        //Can emulate Java enum using the valueOf method
        MyEnum cat = MyEnum.valueOf("cat");
    
        //Can retrieve all instances in an unmodifiable set using the values method
        Set<MyEnum> allThings = MyEnum.values();
    
        //Will result in [dog, animal, thing]
        List<MyEnum> ancestors = pug.getAncestors();
     *
     

    If your HierarchicalEnumValue stores other information and want to ensure that declared instances are loaded in memory you can use Mango's Preloader to load during application startup.

    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Constructor Detail

      • HierarchicalEnumValue

        protected HierarchicalEnumValue​(String name)
        Instantiates a new enum value.
        Parameters:
        name - the name of the enum value
    • Method Detail

      • children

        public List<T> children()
        Gets the child enum values for this value
        Returns:
        the list of child enum values
      • depth

        public int depth()
        The depth of the enum value in the hierarchy
        Returns:
        the depth
      • isInstance

        public final boolean isInstance​(Tag value)
        Description copied from interface: Tag
        Determines if this tag is an instance of a given tag.
        Specified by:
        isInstance in interface Tag
        Overrides:
        isInstance in class EnumValue<T extends HierarchicalEnumValue<T>>
        Parameters:
        value - The given tag to check if this one is an instance of
        Returns:
        True if this tag is an instance of the given tag
      • isLeaf

        public boolean isLeaf()
        Checks if this enum value is a leaf
        Returns:
        True if a leaf, False otherwise
      • isRoot

        public boolean isRoot()
        Checks if this enum value is a root
        Returns:
        True if a root, False otherwise
      • label

        public String label()
        Description copied from interface: Tag
        Gets the label associated with the tag. In most cases the label is the same as the name, but when tags are defined using a HierarchicalEnumValue the label is the leaf node name without the full path.
        Returns:
        the label of the tag
      • parent

        public T parent()
        Description copied from interface: Tag
        Gets the parent of this tag
        Returns:
        the parent or null if it has no parent
      • path

        public String[] path()
        Generates an array of string representing the path of this enum value in the hierarchy
        Returns:
        the path