Package ch.njol.yggdrasil
Class PseudoEnum<T extends PseudoEnum<T>>
java.lang.Object
ch.njol.yggdrasil.PseudoEnum<T>
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected Object
clone()
Prevents cloning of pseudo-enums.boolean
Checks for reference equality (==).getConstant(int id)
Returns the constant with the given ID.Returns this constant's pseudo-enum class, i.e.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.int
hashCode()
Returnsordinal()
, i.e.name()
Returns the unique name of this constant.int
int
ordinal()
Returns the unique ID of this constant.toString()
Returnsname()
.static <T extends PseudoEnum<T>>
@Nullable T@Nullable T
values()
Returns all constants registered so far, ordered by theirid
(i.e.static <T extends PseudoEnum<T>>
List<T>Returns all constants of the given class registered so far, ordered by theirid
(i.e.
-
Constructor Details
-
PseudoEnum
- Parameters:
name
- The unique name of this constant.- Throws:
IllegalArgumentException
- If the given name is already in use.
-
-
Method Details
-
name
Returns the unique name of this constant.- Returns:
- The unique name of this constant.
- See Also:
Enum.name()
-
toString
Returnsname()
.- Overrides:
toString
in classObject
- Returns:
name()
- See Also:
Enum.toString()
-
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:
Enum.ordinal()
-
hashCode
public final int hashCode()Returnsordinal()
, i.e. distinct hash codes for distinct constants. -
equals
Checks for reference equality (==). -
clone
Prevents cloning of pseudo-enums. If you want to make your enums cloneable, create a (name, constantToClone) constructor.- Overrides:
clone
in classObject
- Returns:
- newer returns normally
- Throws:
CloneNotSupportedException
- always
-
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:
Enum.getDeclaringClass()
-
getDeclaringClass
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:
Enum.getDeclaringClass()
-
values
Returns all constants registered so far, ordered by theirid
(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:
Enum.valueOf(Class, String)
-
values
Returns all constants of the given class registered so far, ordered by theirid
(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
- IfgetDeclaringClass
(c) != c (i.e. if the given class is anonymous).- See Also:
Enum.valueOf(Class, String)
-
getConstant
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:
valueOf(String)
-
numConstants
public final int numConstants()- Returns:
- How many constants are currently registered
-
valueOf
- 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:
Enum.valueOf(Class, String)
-
valueOf
- Parameters:
c
- The class of the constant to findname
- 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:
Enum.valueOf(Class, String)
-