연습장

잡동사니

package com.libeka.osei;

public final class Type implements Comparable {

	public static final Type BOOLEAN = new Type("BOOLEAN");

	public static final Type INTEGER = new Type("INTEGER");

	public static final Type LONG = new Type("LONG");

	public static final Type FLOAT = new Type("FLOAT");

	public static final Type DOUBLE = new Type("DOUBLE");

	public static final Type STRING = new Type("STRING");

	public static final Type DATE = new Type("DATE");

	private static final Type[] values = new Type[] { BOOLEAN, INTEGER, LONG,
			FLOAT, DOUBLE, STRING, DATE };

	public static Type valueOf(String name) {
		for (int i = 0; i < values.length; i++) {
			if (values[i].name.equals(name))
				return values[i];
		}

		StringBuffer message = new StringBuffer();
		message.append("No enum const class ");
		message.append(Type.class.getName()).append(".").append(name);
		throw new IllegalArgumentException(message.toString());
	}

	public static Type[] values() {
		return values;
	}

	private String name;

	private Type(String name) {
		this.name = name;
	}

	public int compareTo(Object o) {
		Type type = (Type) o;
		return new Integer(this.ordinal())
				.compareTo(new Integer(type.ordinal()));
	}

	/**
	 * Returns the name of this enum constant, exactly as declared in its enum
	 * declaration. Most programmers should use the {@link #toString()}
	 * method in preference to this one, as the toString method may return a
	 * more user-friendly name. This method is designed primarily for use in
	 * specialized situations where correctness depends on getting the exact
	 * name, which will not vary from release to release.
	 *
	 * @return the name of this enum constant
	 *
	 * @see <a
	 *      href="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#name()">http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#name()</a>
	 */
	public String name() {
		return name;
	}

	/**
	 * Returns the ordinal of this enumeration constant (its position in its
	 * enum declaration, where the initial constant is assigned an ordinal of
	 * zero). Most programmers will have no use for this method. It is designed
	 * for use by sophisticated enum-based data structures, such as EnumSet and
	 * EnumMap.
	 *
	 * @return the ordinal of this enumeration constant
	 *
	 * @see <a
	 *      href="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#ordinal()">http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#ordinal()</a>
	 */
	public int ordinal() {
		for (int i = 0; i < values.length; i++)
			if (values[i].equals(this))
				return i;
		throw new RuntimeException();
	}

	/**
	 * Returns the name of this enum constant, as contained in the declaration.
	 * This method may be overridden, though it typically isn't necessary or
	 * desirable. An enum type should override this method when a more
	 * "programmer-friendly" string form exists.
	 *
	 * @return the name of this enum constant
	 *
	 * @see <a
	 *      href="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#toString()">http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#toString()</a>
	 */
	public String toString() {
		return name;
	}

	/**
	 * Returns true if the specified object is equal to this enum constant.
	 *
	 * @param obj the object to be compared for equality with this object.
	 *
	 * @return true if the specified object is equal to this enum constant.
	 *
	 * @see <a
	 *      href="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#equals(java.lang.Object)">http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#equals(java.lang.Object)</a>
	 */
	public boolean equals(Object obj) {
		if (!(obj instanceof Type))
			return false;
		Type o = (Type) obj;
		return name.equals(o.name);
	}

	public int hashCode() {
		return name.hashCode();
	}

	protected Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}

	/**
	 * Returns the Class object corresponding to this enum constant's enum type.
	 * Two enum constants e1 and e2 are of the same enum type if and only if
	 * e1.getDeclaringClass() == e2.getDeclaringClass(). (The value returned by
	 * this method may differ from the one returned by the
	 * {@link Object#getClass()} method for enum constants with
	 * constant-specific class bodies.)
	 *
	 * @return the Class object corresponding to this enum constant's enum type
	 *
	 * @see <a
	 *      href="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#getDeclaringClass()">http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#getDeclaringClass()</a>
	 */
	public Class getDeclaringClass() {
		return Type.class;
	}

}
개인 도구