KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transport > packet > Enum


1 /*
2  * File: Enum.java
3  * Created: Dec 18, 2003
4  * Author: bruce
5  *
6  * Copyright 2003,2004 Bruce Lowery
7  */

8 package transport.packet;
9
10 import java.io.Serializable JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  *
16  */

17 public class Enum implements Serializable JavaDoc
18 {
19     private static final Map JavaDoc byName = new HashMap JavaDoc();
20     private static final Map JavaDoc byValue = new HashMap JavaDoc();
21     
22     private String JavaDoc name;
23     private Integer JavaDoc value;
24     
25     public Enum(String JavaDoc n, int v)
26     {
27         super();
28         name = n;
29         value = new Integer JavaDoc(v);
30
31         if (byName.containsKey(name))
32         {
33             throw new DuplicateEnumException(name);
34         }
35         if (byValue.containsKey(value))
36         {
37             throw new DuplicateEnumException(value.toString());
38         }
39         byName.put(name,this);
40         byValue.put(value,this);
41     }
42     
43     public String JavaDoc getName()
44     {
45         return name;
46     }
47     
48     public int getValue()
49     {
50         return value.intValue();
51     }
52     
53     public static Enum JavaDoc getByValue(int i)
54     {
55         return (Enum JavaDoc) byValue.get(new Integer JavaDoc(i));
56     }
57     
58     public static Enum JavaDoc getByName(String JavaDoc n)
59     {
60         return (Enum JavaDoc) byName.get(n);
61     }
62     
63     public boolean equals(Object JavaDoc o)
64     {
65         if (! (o instanceof Enum JavaDoc))
66         {
67             return false;
68         }
69         Enum JavaDoc e = (Enum JavaDoc) o;
70         return e.name.equals(name) && e.value.equals(value);
71     }
72     
73     public int hashCode()
74     {
75         return (name + value.toString()).hashCode();
76     }
77 }
78
Popular Tags