📄 enum.java
字号:
// Copyright <applicate>, 2002. All Rights Reserved.
// This software is the proprietary information of www.applicate.de.
// Use is subject to license terms.
package de.applicate.util.lang;
import java.io.Serializable;
import java.util.HashMap;
/**
* This base classe enables an objectorientated implementation for enumerations (C++ enum).
* <p>
* Example:<br>
* <pre>
* public class WeekdayEnum extends Enum {
* public final static WeekdayEnum MONDAY = new WeekdayEnum(0);
* public final static WeekdayEnum TUESDAY = new WeekdayEnum(1);
* public final static WeekdayEnum WEDNESDAY = new WeekdayEnum(2);
* public final static WeekdayEnum THURSDAY = new WeekdayEnum(3);
* public final static WeekdayEnum FRIDAY = new WeekdayEnum(4);
* public final static WeekdayEnum SATURDAY = new WeekdayEnum(5);
* public final static WeekdayEnum SUNDAY = new WeekdayEnum(6);
*
* private WeekdayEnum(int _nWeekday) {
* super(_nWeekday);
* }
*
* public boolean isMonday(WeekdayEnum _nWeekday) {
* if (_nWeekday == WeekdayEnum.MONDAY) {
* return true;
* } else {
* return false;
* }
* }
* </pre>
*
* <p>
* {@link #equals} and {@link #compareTo} are declared final.<br>
* This means that fields in derived classes should contain just
* additional information which do not effect these methods.<br>
* hashCode() is not overridden because the default implementation
* is good (in this case) and fast.
*
* @author Holger Schulz
* @created 10. August 2001
* @version $Author: mwulff $ checked in $Revision: 1.1 $ at $Date: 2003/01/16 13:04:46 $
*/
public abstract class Enum implements Comparable, Serializable, Cloneable, WithID {
/**
* Key: "33de.applicate.util.abc.EnumXYZ" for an instance of class "de.applicate.util.abc.EnumXYZ" with ID=33
* Value: the existing instance of "de.applicate.util.abc.EnumXYZ" with ID=33
*/
protected final static HashMap s_hmIDtoEnum = new HashMap();
protected final int m_nID;
//------------------------------------------------------------------------
// Constructors
//------------------------------------------------------------------------
/**
* @param _nID User defined ID.<br>
* <b>Attention:</b>Pay attention not to define the same ID twice! This cause an exception!
*/
protected Enum(final int _nID) {
m_nID = _nID;
String sIDandClassName = getIDandClassName();
Object oEnumOld = s_hmIDtoEnum.put(sIDandClassName, this);
if (null != oEnumOld) {
// restore original
s_hmIDtoEnum.put(sIDandClassName, oEnumOld);
throw new IllegalArgumentException("ID " + _nID + " already exists for class " + this.getClass().getName());
}
}
//------------------------------------------------------------------------
// implemented methods from ID
//------------------------------------------------------------------------
public final int getID() {
return m_nID;
}
public final boolean equalIDs(final WithID _id) {
return m_nID == _id.getID();
}
//------------------------------------------------------------------------
// implemented methods from Comparable
//------------------------------------------------------------------------
/**
* Just the IDs are compared. So this method is inconsistent with {@link #equals}
*
* @param _o Parameter name is self-explanatory!
* @return <code>m_nID - ((WithID)_o).getID();</code>
*/
public final int compareTo(final Object _o) {
return m_nID - ((WithID)_o).getID();
}
//------------------------------------------------------------------------
// overrided methods from Object
//------------------------------------------------------------------------
/**
* @return Returns the ID.
*/
public String toString() {
return Integer.toString(m_nID);
}
/**
* An enum is equal if the instances are of the same class (= default behaviour).
*
* @param _o Parameter name is self-explanatory!
* @return -
*/
public final boolean equals(final Object _o) {
return this == _o;
}
//------------------------------------------------------------------------
// implemented methods for Cloneable
//------------------------------------------------------------------------
/**
* @return the original instance
*/
public final Object clone() {
return this;
}
//------------------------------------------------------------------------
// implemented methods for Serializable
//------------------------------------------------------------------------
/**
* Called during deserialization.
*
* @return the existing instance of the enum.<br>So you can use '==' like 'equals' even if you use a deserialized Enum.
* @see <a href="http://www.javaworld.com/javaworld/javatips/javatip122/">JavaWorld Java Tip 122: Beware of Java typesafe enumerations</a>
*/
protected final Object readResolve() {
// must be protected. If private the method is not called during deserialization.
return s_hmIDtoEnum.get(getIDandClassName());
}
//------------------------------------------------------------------------
// help methods
//------------------------------------------------------------------------
protected String getIDandClassName() {
return m_nID + this.getClass().getName();
}
//------------------------------------------------------------------------
// static functions
//------------------------------------------------------------------------
/**
* <pre>
* EnumTest enumTest = new EnumTest(1);
* EnumTest enumTest2 = (EnumTest)Enum.getEnum(EnumTest.class, 1);
* System.out.println("enumTest == enumTest2: " + (enumTest == enumTest2)); // => true
* </pre>
*
* @param _class
* @param _nID
* @return The Enum instance of the specific class with the given ID.
*/
public static Enum getEnum(final Class _class, final int _nID) {
return (Enum)s_hmIDtoEnum.get(_nID + _class.getName());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -