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,) static final 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 final Object
clone()
Prevents cloning of pseudo-enums.final boolean
Checks for reference equality (==).final T
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.final int
hashCode()
Returnsordinal()
, i.e.final String
name()
Returns the unique name of this constant.final int
final int
ordinal()
Returns the unique ID of this constant.toString()
Returnsname()
.static <T extends PseudoEnum<T>>
Tfinal 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:
-
toString
Returnsname()
. -
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()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 of whether they define an anonymous subclass.- Returns:
- This constant's pseudo-enum class.
- See Also:
-
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:
-
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:
-
values
public static <T extends PseudoEnum<T>> List<T> values(Class<T> type) throws IllegalArgumentException Returns all constants of the given class registered so far, ordered by theirid
(i.e. type.values()[type.ordinal()] == type is true for any constant type).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
(type) != type (i.e. if the given class is anonymous).- See Also:
-
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 invalid input: '<' 0 or >=numConstants()
- See Also:
-
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:
-
valueOf
- Parameters:
type
- 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:
-