KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > IntEnum


1 /**
2  * $RCSfile: IntEnum.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2004/10/21 07:28:12 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import java.util.*;
15
16 /**
17  * <p>A type safe enumeration object that is keyed by an Int
18  * value for switch statements and storage in DBs.</p>
19  * <p/>
20  * <p>Used for indicating distinct states in a generic manner
21  * where each enum should have an associated int value. The
22  * given int should be unique for each enum value as hashCode
23  * and equals depends solely on the int value given. Most
24  * child classes should extend IntEnum and create static instances.</p>
25  *
26  * @author Iain Shigeoka
27  */

28 public class IntEnum extends Enum JavaDoc {
29
30     private int value;
31     protected static Hashtable enumTypes = new Hashtable();
32
33     protected IntEnum(String JavaDoc name, int val) {
34         super(name);
35         this.value = val;
36     }
37
38     /**
39      * Returns the int value associated with the enum.
40      *
41      * @return the int value of the enum.
42      */

43     public int getValue() {
44         return value;
45     }
46
47     public boolean equals(Object JavaDoc object) {
48         if (this == object) {
49             return true;
50         }
51         else if ((this.getClass().isInstance(object)) && value == (((IntEnum)object).value)) {
52             return true;
53         }
54         else {
55             return false;
56         }
57     }
58
59     /**
60      * <p>Checks in an enum for use in the getEnumFromInt() method.</p>
61      *
62      * @param enumeration The enum to be registered
63      */

64     protected static void register(IntEnum enumeration) {
65         Map enums = (Map)enumTypes.get(enumeration.getClass());
66         if (enums == null) {
67             enums = new HashMap<Integer JavaDoc,Object JavaDoc>();
68             enumTypes.put(enumeration.getClass(), enums);
69         }
70         enums.put(enumeration.getValue(), enumeration);
71     }
72
73     /**
74      * <p>Obtain the enum associated with the given value.</p>
75      * <p>Values must be registered earlier using the register() method.</p>
76      *
77      * @param value the value to lookup the enum for
78      * @return The associated enum or null if no matching enum exists
79      */

80     protected static IntEnum getEnumFromInt(Class JavaDoc enumClass, int value) {
81         Map enums = (Map)enumTypes.get(enumClass);
82         if (enums != null) {
83             return (IntEnum)enums.get(value);
84         }
85         return null;
86     }
87
88     public int hashCode() {
89         return value;
90     }
91
92     public String JavaDoc toString() {
93         return Integer.toString(value) + " " + super.toString();
94     }
95 }
Popular Tags