001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: Enum.java,v 1.8 2005/02/16 11:28:14 jesper Exp $
023: package net.infonode.util;
024:
025: import java.io.*;
026: import java.util.HashMap;
027:
028: /**
029: * Base class for enum classes.
030: * Each enum value contains a name and an integer identifier.
031: *
032: * @author Jesper Nordenberg
033: * @version $Revision: 1.8 $ $Date: 2005/02/16 11:28:14 $
034: */
035: public class Enum implements Serializable, Writable {
036: private static final long serialVersionUID = 1;
037: private static final HashMap VALUE_MAP = new HashMap();
038:
039: private int value;
040: private transient String name;
041:
042: protected Enum(int value, String name) {
043: this .value = value;
044: this .name = name;
045:
046: HashMap values = (HashMap) VALUE_MAP.get(getClass());
047:
048: if (values == null) {
049: values = new HashMap();
050: VALUE_MAP.put(getClass(), values);
051: }
052:
053: values.put(new Integer(value), this );
054: }
055:
056: /**
057: * Returns the integer identifier for this enum value.
058: *
059: * @return the integer identifier for this enum value
060: */
061: public int getValue() {
062: return value;
063: }
064:
065: /**
066: * Return the name of this enum value.
067: *
068: * @return the name of this enum value
069: */
070: public String getName() {
071: return name;
072: }
073:
074: public String toString() {
075: return name;
076: }
077:
078: public void write(ObjectOutputStream out) throws IOException {
079: writeObject(out);
080: }
081:
082: protected static Object getObject(Class cl, int value)
083: throws IOException {
084: HashMap map = (HashMap) VALUE_MAP.get(cl);
085:
086: if (map == null)
087: throw new IOException("Invalid enum class '" + cl + "'!");
088:
089: Object object = map.get(new Integer(value));
090:
091: if (object == null)
092: throw new IOException("Invalid enum value '" + value + "'!");
093:
094: return object;
095: }
096:
097: private void writeObject(ObjectOutputStream out) throws IOException {
098: out.writeShort(value);
099: }
100:
101: private void readObject(ObjectInputStream in) throws IOException {
102: value = in.readShort();
103: }
104:
105: protected static Object decode(Class cl, ObjectInputStream in)
106: throws IOException {
107: return getObject(cl, in.readShort());
108: }
109:
110: protected Object readResolve() throws ObjectStreamException {
111: try {
112: return getObject(getClass(), getValue());
113: } catch (IOException e) {
114: return this;
115: }
116: }
117: }
|