KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > enums > StaticLabeledEnum


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.enums;
18
19 /**
20  * Base class for static type-safe labeled enum instances.
21  *
22  * Usage example:
23  *
24  * <pre>
25  * public class FlowSessionStatus extends StaticLabeledEnum {
26  *
27  * // public static final instances!
28  * public static FlowSessionStatus CREATED = new FlowSessionStatus(0, &quot;Created&quot;);
29  * public static FlowSessionStatus ACTIVE = new FlowSessionStatus(1, &quot;Active&quot;);
30  * public static FlowSessionStatus PAUSED = new FlowSessionStatus(2, &quot;Paused&quot;);
31  * public static FlowSessionStatus SUSPENDED = new FlowSessionStatus(3, &quot;Suspended&quot;);
32  * public static FlowSessionStatus ENDED = new FlowSessionStatus(4, &quot;Ended&quot;);
33  *
34  * // private constructor!
35  * private FlowSessionStatus(int code, String label) {
36  * super(code, label);
37  * }
38  *
39  * // custom behavior
40  * }</pre>
41  *
42  * @author Keith Donald
43  * @since 1.2.6
44  */

45 public abstract class StaticLabeledEnum extends AbstractLabeledEnum {
46
47     /**
48      * The unique code of the enum.
49      */

50     private final Short JavaDoc code;
51
52     /**
53      * A descriptive label for the enum.
54      */

55     private final transient String JavaDoc label;
56
57
58     /**
59      * Create a new StaticLabeledEnum instance.
60      * @param code the short code
61      * @param label the label (can be <code>null</code>)
62      */

63     protected StaticLabeledEnum(int code, String JavaDoc label) {
64         this.code = new Short JavaDoc((short) code);
65         if (label != null) {
66             this.label = label;
67         }
68         else {
69             this.label = this.code.toString();
70         }
71     }
72
73     public Comparable JavaDoc getCode() {
74         return code;
75     }
76
77     public String JavaDoc getLabel() {
78         return label;
79     }
80
81     /**
82      * Return the code of this LabeledEnum instance as a short.
83      */

84     public short shortValue() {
85         return ((Number JavaDoc) getCode()).shortValue();
86     }
87
88
89     //---------------------------------------------------------------------
90
// Serialization support
91
//---------------------------------------------------------------------
92

93     /**
94      * Return the resolved type safe static enum instance.
95      */

96     protected Object JavaDoc readResolve() {
97         return StaticLabeledEnumResolver.instance().getLabeledEnumByCode(getType(), getCode());
98     }
99
100 }
101
Popular Tags