https://www.novixys.com/blog/java-enum-examples/
https://stackoverflow.com/questions/28332924/case-insensitive-matching-of-a-string-to-a-java-enum
https://deepeshdarshan.wordpress.com/2012/11/14/java-enum-and-abstract-methods/
https://dzone.com/articles/java-holiday-calendar-2016-day-19-speed-up-your-en
http://stackoverflow.com/questions/26357132/generic-enum-valueof-method-enum-class-in-parameter-and-return-enum-item
http://tutorials.jenkov.com/java/enums.html
<T extends Comparable<? super T>> void sort( List<T> list ) { ... }
Comparable<? super T> is the set of instantiations of Comparable on T and all of its superclasses. A Comparable<T> suffices and, at the other end, so does a Comparable<Object>. What this means in English is that the elements must be comparable to their own type or some supertype of their own type. This is sufficient to ensure that the elements can all be compared to one another, but not as restrictive as saying that they must all implement the compareTo() method themselves. Some of the elements may inherit the Comparable interface from a parent class that knows how to compare only to a supertype of T, and that is exactly what is allowed here.
The type A may not subclass Enum explicitly.
public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
throw new InvalidObjectException("can't deserialize enum");
}
private void readObjectNoData() throws ObjectStreamException {
throw new InvalidObjectException("can't deserialize enum");
}
This class is the common superclass of all enumerated types. It is not itself an enum type, however, and a Java compiler does not allow other classes to extend it. Subclasses of Enum may be only created with enum declarations. Enum is a generic type, and the type variable E represents the concrete enumerated type that actually extends Enum. This type variable exists so that Enum can implement Comparable<E>.
Most users of enumerated constants will use toString( ) instead of name( ). The implementation of toString( ) defined by Enum returns the same value as name( ). The toString( ) method is not final, however, and it can be overridden in enum declarations.
Enum doesn't support clone
protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public final boolean equals(Object other) {
return this==other;
}
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)
https://stackoverflow.com/questions/28332924/case-insensitive-matching-of-a-string-to-a-java-enum
To do a case-insensitive matching, one can write a custom method inside the
Day
enum, e.g.public static Day lookup(String day) {
for (Day d : Day.values()) {
if (d.name().equalsIgnoreCase(day)) {
return type;
}
}
return null;
}
https://deepeshdarshan.wordpress.com/2012/11/14/java-enum-and-abstract-methods/
An enum is a different type of class. The Java Language Specification says “There are two kinds of class declarations: normal class declarations and enumdeclarations. The body of an enum type may contain enum constants. An enum constant defines an instance of the enum type”.
An enum type is a subclass of java.Lang.Enum. An enum cannot extend anything else, but still it can implement other interfaces. Public or protected modifiers can only be used with a top-level enum declaration, but all access modifiers can be used with nested enum declarations. Also an enum type provides final methods like name, compareTo, hashCode etc. We can also use as well as override toString() method in an enum.
https://dzone.com/articles/java-holiday-calendar-2016-day-19-speed-up-your-en
Then it turns out Java will generate an equivalent to the following under the hood:
This means each time we are calling values(), we are creating a copy of the internal VALUES array. The reason for this is that an array cannot be protected from being overwritten by user code. Thus, a new copy must be provided for each call to guarantee state integrity. The JVM can mitigate the problem under certain circumstances, but if we want to be absolutely sure no objects are created, we must implement our own equivalent to values(). This can be done like this:
Note that the internal VALUE_LIST is unmodifiable and that this is important, or else we cannot expose it directly via the valuesAsList() method.
Now we can use our modified Enum like this with no performance overhead:
http://stackoverflow.com/questions/5262096/how-do-i-get-the-value-of-an-enum-if-i-dont-know-the-class-at-compile-timehttp://stackoverflow.com/questions/26357132/generic-enum-valueof-method-enum-class-in-parameter-and-return-enum-item
Why implement your own, when you can use Java's own implementation for this?
YourEnum yourEnum = Enum.valueOf(YourEnum.class, value);
Your method then becomes:
private static <E extends Enum<E>> E getEnum(Class<E> enumClass, String name){
try {
return Enum.valueOf(enumClass, name);
} catch(IllegalArgumentException e) {
return null;
}
}
and you can call it with:
getEnum(Enum1.class, "A")
http://tutorials.jenkov.com/java/enums.html
public enum Level { HIGH, MEDIUM, LOW }
You can obtain an array of all the possible values of a Java enum type by calling its static
values()
method. All enum types get a static values()
method automatically by the Java compiler. Here is an example of iterating all values of an enum:for (Level level : Level.values()) { System.out.println(level); }
Running this Java code would print out all the enum values. Here is the output:
HIGH MEDIUM LOW
Java enums extend the
java.lang.Enum
class implicitly, so your enum types cannot extend another class.
If a Java enum contains fields and methods, the definition of fields and methods must always come afterthe list of constants in the enum. Additionally, the list of enum constants must be terminated by a semicolon;
You can add fields to a Java enum. Thus, each constant enum value gets these fields. The field values must be supplied to the constructor of the enum when defining the constants. Here is an example:
public enum Level { HIGH (3), //calls constructor with value 3 MEDIUM(2), //calls constructor with value 2 LOW (1) //calls constructor with value 1 ; // semicolon needed when fields / methods follow private final int levelCode; private Level(int levelCode) { this.levelCode = levelCode; } }http://www.hankcs.com/program/java/enum-java-examples-of-dynamic-modification.html
<T extends Comparable<? super T>> void sort( List<T> list ) { ... }
Comparable<? super T> is the set of instantiations of Comparable on T and all of its superclasses. A Comparable<T> suffices and, at the other end, so does a Comparable<Object>. What this means in English is that the elements must be comparable to their own type or some supertype of their own type. This is sufficient to ensure that the elements can all be compared to one another, but not as restrictive as saying that they must all implement the compareTo() method themselves. Some of the elements may inherit the Comparable interface from a parent class that knows how to compare only to a supertype of T, and that is exactly what is allowed here.
The type A may not subclass Enum explicitly.
public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
throw new InvalidObjectException("can't deserialize enum");
}
private void readObjectNoData() throws ObjectStreamException {
throw new InvalidObjectException("can't deserialize enum");
}
This class is the common superclass of all enumerated types. It is not itself an enum type, however, and a Java compiler does not allow other classes to extend it. Subclasses of Enum may be only created with enum declarations. Enum is a generic type, and the type variable E represents the concrete enumerated type that actually extends Enum. This type variable exists so that Enum can implement Comparable<E>.
Most users of enumerated constants will use toString( ) instead of name( ). The implementation of toString( ) defined by Enum returns the same value as name( ). The toString( ) method is not final, however, and it can be overridden in enum declarations.
Enum doesn't support clone
protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public final boolean equals(Object other) {
return this==other;
}
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)