Posted on 2006-04-26 22:09
iceboundrock 閱讀(912)
評論(0) 編輯 收藏 所屬分類:
J2SE隨筆
?1
import
?java.util.HashMap;
?2
?3
/**?*/
/**
?4
?*?枚舉類型的基類
?5
?*?
@author
?Richard?Lee
?6
?*
?7
?
*/
?8
public
?
abstract
?
class
?EnumBase?
{
?9
10
????
//////////////////////////////////////////
/
11
????
//
成員變量
12
????
private
?
final
?String?name;
13
????
private
?
int
?value?
=
?
-
1
;
14
15
????
16
????
//////////////////////////////////////////
/
17
????
//
類靜態變量
18
????
private
?
static
?
final
?HashMap?valueCollection?
=
?
new
?HashMap();
19
????
private
?
static
?
final
?HashMap?nameCollection?
=
?
new
?HashMap();
20
????
21
????
//////////////////////////////////////////
/
22
????
//
類靜態方法
23
????
public
?
static
?EnumBase?valueOf(Class?enumType?,?
int
?value)
{
24
????????
return
?(EnumBase)valueCollection.get(getEnumValueKey(enumType,?value));
25
????}
26
????
27
????
public
?
static
?EnumBase?parse(Class?enumType?,?String?s)
{
28
????????
return
?(EnumBase)nameCollection.get(getEnumNameKey(enumType?,?s));
29
????}
30
????
31
????
private
?
static
?String?getEnumValueKey(Class?enumClass,?
int
?value)?
{
32
????????
return
?enumClass.getName()?
+
?
"
,
"
?
+
?value;
33
????}
34
35
????
private
?
static
?String?getEnumNameKey(Class?enumType?,?String?name)
{
36
????????
return
?enumType?
+
?
"
,
"
?
+
?name;
37
????}
38
????
39
????
//////////////////////////////////////////
/
40
????
//
構造函數
41
????
protected
?EnumBase(String?name)
{
42
????????checkName(name);
43
????????
this
.name?
=
?name;
44
????????nameCollection.put(getEnumNameKey(name)?,?
this
);
45
????}
46
????
47
????
protected
?EnumBase(String?name?,?
int
?value)
{
48
????????
this
(name);
49
????????
50
????????checkValue(value);
51
????????
this
.value?
=
?value;
52
????????valueCollection.put(getEnumValueKey(value)?,?
this
);
53
????}
54
55
????
56
????
//////////////////////////////////////////
/
57
????
//
成員函數
58
????
private
?String?getEnumNameKey(String?name)?
{
59
????????
return
?getEnumNameKey(
this
.getClass()?,?name);
60
????}
61
62
????
private
?
void
?checkName(String?name)?
{
63
????????
if
(
null
?
==
?name?
||
?
""
.equals(name))
{
64
????????????
throw
?
new
?IllegalArgumentException(
"
Element?name?can?not?be?null?or?empty.
"
);
65
????????}
66
????????
if
(nameCollection.containsKey(getEnumNameKey(name)))
{
67
????????????
throw
?
new
?IllegalStateException(
"
The?name?of?enum?element?already?exist.
"
);
68
????????}
69
????}
70
????
71
????
private
?
void
?checkValue(
int
?value)?
{
72
????????
if
(value?
<
?
0
)
{
73
????????????
throw
?
new
?IllegalArgumentException(
"
Element?value?must?not?less?than?0.
"
);
74
????????}
75
????????
if
(valueCollection.containsKey(getEnumValueKey(value)))
{
76
????????????
throw
?
new
?IllegalStateException(
"
This?value?of?enum?element?already?exist.
"
);
77
????????}
78
????}
79
80
????
private
?String?getEnumValueKey(
int
?value)?
{
81
????????
return
?getEnumValueKey(
this
.getClass()?,?value);
82
????}
83
84
????
public
?String?toString()?
{
85
????????
return
?name;
86
????}
87
????
88
????
public
?
int
?getValue()?
{
89
????????
if
(value?
>
?
-
1
)
{
90
????????????
return
?value;
91
????????}
else
{
92
????????????
throw
?
new
?IllegalStateException(
"
This?enum?class?has?no?value
"
);
93
????????}
94
????}
95
}
96
使用范例:
public
?
class
?VaildEnum?
extends
?EnumBase?
{


?
protected
?VaildEnum(String?name?,?
int
?value)?
{
??
super
(name?,?value);
?}
?
?
public
?
static
?
final
?VaildEnum?Rule?
=
?
new
?VaildEnum(
"
Rule
"
?,?
1
);
?
public
?
static
?
final
?VaildEnum?OU?
=
?
new
?VaildEnum(
"
OU
"
?,?
2
);
}
該枚舉基類對于j2sdk中常用的整型常量或者字符串型常量的優點:1.? 類型安全。2.性能提高(比字符串常量型)。3.在IDE中使用時會有提示,無需翻看SDK文檔。
缺點:1.編寫略微麻煩。
如果發現代碼中的bugs,歡迎您評論,非常感謝。