corda/classpath/java/lang/Enum.java
Joel Dice 87b02eb949 update copyright years
Previously, I used a shell script to extract modification date ranges
from the Git history, but that was complicated and unreliable, so now
every file just gets the same year range in its copyright header.  If
someone needs to know when a specific file was modified and by whom,
they can look at the Git history themselves; no need to include it
redundantly in the header.
2013-07-02 20:52:38 -06:00

73 lines
1.8 KiB
Java

/* Copyright (c) 2008-2013, Avian Contributors
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. */
package java.lang;
import java.lang.reflect.Method;
public abstract class Enum<E extends Enum<E>> implements Comparable<E> {
private final String name;
protected final int ordinal;
public Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
public int compareTo(E other) {
if (getDeclaringClass() != other.getDeclaringClass()) {
throw new ClassCastException();
}
return ordinal - other.ordinal;
}
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
if (name == null) throw new NullPointerException("name");
if (!enumType.isEnum())
throw new IllegalArgumentException(enumType.getCanonicalName() + " is not an enum.");
try {
Method method = enumType.getMethod("values");
Enum values[] = (Enum[]) method.invoke(null);
for (Enum value: values) {
if (name.equals(value.name)) {
return (T) value;
}
}
} catch (Exception ex) {
// Cannot happen
throw new Error(ex);
}
throw new IllegalArgumentException(enumType.getCanonicalName() + "." + name + " is not an enum constant.");
}
public int ordinal() {
return ordinal;
}
public final String name() {
return name;
}
public String toString() {
return name;
}
public Class<E> getDeclaringClass() {
Class c = getClass();
while (c.getSuperclass() != Enum.class) {
c = c.getSuperclass();
}
return c;
}
}