Class PseudoEnum<T extends PseudoEnum<T>>

java.lang.Object
ch.njol.yggdrasil.PseudoEnum<T>

@ThreadSafe public abstract class PseudoEnum<T extends PseudoEnum<T>> extends Object
A class that acts as a "pseudo-enum", i.e. a class which only has immutable, (public,) final static instances, which can be identified by their unique name. The instances don't even have to be defined in their class, as they are registered in the constructor.

Please note that you cannot define a constant's id used for saving by annotating it with @YggdrasilID, as the field(s) of the constant may not be known, and furthermore a constant can be assigned to any number of fields.

This class defines methods similar to those in Enum with minor differences, e.g. values() returns a List instead of an array.

  • Constructor Details

  • Method Details

    • name

      public final String name()
      Returns the unique name of this constant.
      Returns:
      The unique name of this constant.
      See Also:
    • toString

      public String toString()
      Returns name().
      Overrides:
      toString in class Object
      Returns:
      name()
      See Also:
    • ordinal

      public final int ordinal()
      Returns the unique ID of this constant. This will not be used by Yggdrasil and can thus change freely across version, in particular reordering and inserting constants is permitted.
      Returns:
      The unique ID of this constant.
      See Also:
    • hashCode

      public final int hashCode()
      Returns ordinal(), i.e. distinct hash codes for distinct constants.
      Overrides:
      hashCode in class Object
    • equals

      public final boolean equals(@Nullable Object obj)
      Checks for reference equality (==).
      Overrides:
      equals in class Object
    • clone

      protected final Object clone() throws CloneNotSupportedException
      Prevents cloning of pseudo-enums. If you want to make your enums cloneable, create a (name, constantToClone) constructor.
      Overrides:
      clone in class Object
      Returns:
      newer returns normally
      Throws:
      CloneNotSupportedException - always
    • getDeclaringClass

      public final Class<T> getDeclaringClass()
      Returns this constant's pseudo-enum class, i.e. the first non-anonymous superclass of this constant. This class is the same for all constants inheriting from a common class independently from whether they define an anonymous subclass.
      Returns:
      This constant's pseudo-enum class.
      See Also:
    • getDeclaringClass

      public static <T extends PseudoEnum<T>> Class<? super T> getDeclaringClass(Class<T> type)
      Returns the common base class for constants of the given type, i.e. the first non-anonymous superclass of type.
      Returns:
      The pseudo-enum class of the given class.
      See Also:
    • values

      public final List<T> values()
      Returns all constants registered so far, ordered by their id (i.e. c.values()[c.ordinal()] == c is true for any constant c).

      The returned list is a copy of the internal list at the time this method was called.

      Please note that you

      Returns:
      All constants registered so far.
      See Also:
    • values

      public static <T extends PseudoEnum<T>> List<T> values(Class<T> c) throws IllegalArgumentException
      Returns all constants of the given class registered so far, ordered by their id (i.e. c.values()[c.ordinal()] == c is true for any constant c).

      The returned list is a copy of the internal list at the time this method was called.

      Returns:
      All constants registered so far.
      Throws:
      IllegalArgumentException - If getDeclaringClass(c) != c (i.e. if the given class is anonymous).
      See Also:
    • getConstant

      public final T getConstant(int id) throws IndexOutOfBoundsException
      Returns the constant with the given ID.
      Parameters:
      id - The constant's ID
      Returns:
      The constant with the given ID.
      Throws:
      IndexOutOfBoundsException - if ID is < 0 or >= numConstants()
      See Also:
    • numConstants

      public final int numConstants()
      Returns:
      How many constants are currently registered
    • valueOf

      public final @Nullable T valueOf(String name)
      Parameters:
      name - The name of the constant to find
      Returns:
      The constant with the given name, or null if no constant with that exact name was found.
      See Also:
    • valueOf

      public static <T extends PseudoEnum<T>> @Nullable T valueOf(Class<T> c, String name)
      Parameters:
      c - The class of the constant to find
      name - The name of the constant to find
      Returns:
      The constant with the given name, or null if no constant with that exact name was found in the given class.
      See Also: