2012-05-11 17:43:27 -06:00
|
|
|
/* Copyright (c) 2008-2011, Avian Contributors
|
2008-02-19 11:06:52 -07:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-09-26 11:27:09 -06:00
|
|
|
package java.lang;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
2007-11-14 09:32:36 -07:00
|
|
|
public abstract class Enum<E extends Enum<E>> implements Comparable<E> {
|
2007-09-26 11:27:09 -06:00
|
|
|
private final String name;
|
2011-11-19 19:22:34 -07:00
|
|
|
protected final int ordinal;
|
2007-09-26 11:27:09 -06:00
|
|
|
|
|
|
|
public Enum(String name, int ordinal) {
|
|
|
|
this.name = name;
|
|
|
|
this.ordinal = ordinal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(E other) {
|
2009-08-21 16:06:12 -06:00
|
|
|
if (getDeclaringClass() != other.getDeclaringClass()) {
|
|
|
|
throw new ClassCastException();
|
|
|
|
}
|
|
|
|
|
2007-09-26 11:27:09 -06:00
|
|
|
return ordinal - other.ordinal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
|
2009-08-13 08:57:58 -06:00
|
|
|
if (name == null) throw new NullPointerException();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Method method = enumType.getMethod("values");
|
2010-09-01 10:10:11 -06:00
|
|
|
Enum values[] = (Enum[]) method.invoke(null);
|
2009-08-13 08:57:58 -06:00
|
|
|
for (Enum value: values) {
|
|
|
|
if (name.equals(value.name)) {
|
|
|
|
return (T) value;
|
2007-09-26 11:27:09 -06:00
|
|
|
}
|
|
|
|
}
|
2009-08-13 08:57:58 -06:00
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new RuntimeException(ex);
|
2007-09-26 11:27:09 -06:00
|
|
|
}
|
2009-08-13 08:57:58 -06:00
|
|
|
|
|
|
|
throw new IllegalArgumentException(name);
|
2007-09-26 11:27:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public int ordinal() {
|
|
|
|
return ordinal;
|
|
|
|
}
|
2007-09-26 12:59:18 -06:00
|
|
|
|
2008-08-12 16:21:39 -06:00
|
|
|
public final String name() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2007-09-26 12:59:18 -06:00
|
|
|
public String toString() {
|
|
|
|
return name;
|
|
|
|
}
|
2009-06-02 17:14:38 -06:00
|
|
|
|
|
|
|
public Class<E> getDeclaringClass() {
|
|
|
|
Class c = getClass();
|
|
|
|
while (c.getSuperclass() != Enum.class) {
|
|
|
|
c = c.getSuperclass();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2007-09-26 11:27:09 -06:00
|
|
|
}
|