Class Enumerator

  • Direct Known Subclasses:
    Access, JGrep.Action, Parser.ClassDeclarationContext, Parser.InterfaceDeclarationContext

    public abstract class Enumerator
    extends java.lang.Object
    A class that represents an enumerated value. Its main features are its toString() and fromString(String, Class) method, which map names to values and vice versa.

    To use this class, derive from it and define one or more public static final fields, as follows:

     public final class Suit extends Enumerator {
    
         // Exactly N instances of "Suit" exist to represent the N possible values.
         public static final Suit CLUBS    = new Suit("clubs");
         public static final Suit DIAMONDS = new Suit("diamonds");
         public static final Suit HEARTS   = new Suit("hearts");
         public static final Suit SPADES   = new Suit("spades");
    
         // Optional, if you want to use EumeratorSet arithmetics.
         public static final EnumeratorSet NONE = new EnumeratorSet(Suit.class      ).setName("none");
         public static final EnumeratorSet ALL  = new EnumeratorSet(Suit.class, true).setName("all");
    
         // These MUST be declared exactly like this:
         private Suit(String name) { super(name); }
         public static Suit fromString(String name) throws EnumeratorFormatException {
             return (Suit) Enumerator.fromString(name, Suit.class);
         }
     }
     
    See Also:
    Effective Java, Item 21
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.Class,​java.util.Map<java.lang.String,​Enumerator>> INSTANCES  
      private java.lang.String name  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Enumerator​(java.lang.String name)
      Initialize the enumerator to the given value.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object that)
      Equality is reference identity.
      protected static Enumerator fromString​(java.lang.String name, java.lang.Class enumeratorClass)
      Initialize an Enumerator from a string.
      (package private) static java.util.Map<java.lang.String,​Enumerator> getInstances​(java.lang.Class enumeratorClass)
      Returns a mapping of name to Enumerator for the given enumeratorClass.
      int hashCode()
      Enforce Object's notion of Object.hashCode().
      java.lang.String toString()
      Returns the name passed to Enumerator(String).
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • name

        private final java.lang.String name
      • INSTANCES

        private static final java.util.Map<java.lang.Class,​java.util.Map<java.lang.String,​Enumerator>> INSTANCES
    • Constructor Detail

      • Enumerator

        protected Enumerator​(java.lang.String name)
        Initialize the enumerator to the given value.
    • Method Detail

      • equals

        public final boolean equals​(java.lang.Object that)
        Equality is reference identity.
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public final int hashCode()
        Enforce Object's notion of Object.hashCode().
        Overrides:
        hashCode in class java.lang.Object
      • getInstances

        static java.util.Map<java.lang.String,​Enumerator> getInstances​(java.lang.Class enumeratorClass)
        Returns a mapping of name to Enumerator for the given enumeratorClass.
      • fromString

        protected static final Enumerator fromString​(java.lang.String name,
                                                     java.lang.Class enumeratorClass)
                                              throws EnumeratorFormatException
        Initialize an Enumerator from a string.

        The given string is converted into a value by looking at all instances of the given type created so far.

        Derived classes should invoke this method as follows:

         public class Suit extends Enumerator {
             ...
             public static Suit fromString(String name) throws EnumeratorFormatException {
                 return (Suit) Enumerator.fromString(name, Suit.class);
             }
         }
        Throws:
        EnumeratorFormatException - if the string cannot be identified
      • toString

        public java.lang.String toString()
        Returns the name passed to Enumerator(String).
        Overrides:
        toString in class java.lang.Object