KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.Serializable JavaDoc;
20 import java.util.Comparator JavaDoc;
21
22 import org.springframework.util.comparator.CompoundComparator;
23 import org.springframework.util.comparator.NullSafeComparator;
24
25 /**
26  * A interface for objects that represent a labled enumeration.
27  * Each such enum instance has the following characteristics:
28  *
29  * <ul>
30  * <li>A type that identifies the enum's class.
31  * For example: <code>com.mycompany.util.FileFormat</code>.
32  *
33  * <li>A code that uniquely identifies the enum within the context of its type.
34  * For example: "CSV". Different classes of codes are possible
35  * (Character, Integer, String).
36  *
37  * <li>A descriptive label. For example: "the CSV File Format".
38  * </ul>
39  *
40  * @author Keith Donald
41  * @since 1.2.2
42  */

43 public interface LabeledEnum extends Comparable JavaDoc, Serializable JavaDoc {
44
45     /**
46      * Return this enumeration's type.
47      */

48     Class JavaDoc getType();
49
50     /**
51      * Return this enumeration's code.
52      * <p>Each code should be unique within enumeration's of the same type.
53      */

54     Comparable JavaDoc getCode();
55
56     /**
57      * Return a descriptive, optional label.
58      */

59     String JavaDoc getLabel();
60
61
62     // Constants for standard enum ordering (Comparator implementations)
63

64     /**
65      * Shared Comparator instance that sorts enumerations by <code>CODE_ORDER</code>.
66      */

67     Comparator JavaDoc CODE_ORDER = new Comparator JavaDoc() {
68         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
69             Comparable JavaDoc c1 = ((LabeledEnum) o1).getCode();
70             Comparable JavaDoc c2 = ((LabeledEnum) o2).getCode();
71             return c1.compareTo(c2);
72         }
73     };
74
75     /**
76      * Shared Comparator instance that sorts enumerations by <code>LABEL_ORDER</code>.
77      */

78     Comparator JavaDoc LABEL_ORDER = new Comparator JavaDoc() {
79         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
80             LabeledEnum e1 = (LabeledEnum) o1;
81             LabeledEnum e2 = (LabeledEnum) o2;
82             Comparator JavaDoc comp = new NullSafeComparator(String.CASE_INSENSITIVE_ORDER, true);
83             return comp.compare(e1.getLabel(), e2.getLabel());
84         }
85     };
86
87     /**
88      * Shared Comparator instance that sorts enumerations by <code>LABEL_ORDER</code>,
89      * then <code>CODE_ORDER</code>.
90      */

91     Comparator JavaDoc DEFAULT_ORDER =
92             new CompoundComparator(new Comparator JavaDoc[] { LABEL_ORDER, CODE_ORDER });
93
94 }
95
Popular Tags