KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > enum > NestLinked


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 package org.apache.commons.lang.enum;
17
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21
22 /**
23  * Color enumeration which demonstrates how to define the constants in a
24  * different class to the Enum. The extra <code>static{}</code> block is
25  * needed to ensure that the enum constants are created before the
26  * static methods on the ColorEnum are used.
27  * <p>
28  * The class loader sees the two classes here as independent - the enum
29  * class is nested, not an inner class. The static block thus forces the
30  * class load of the outer class, which is needed to initialise the enums.
31  *
32  * @author Stephen Colebourne
33  * @version $Id: NestLinked.java 161244 2005-04-14 06:16:36Z ggregory $
34  */

35
36 public final class NestLinked {
37     
38     public static final ColorEnum RED = new ColorEnum("Red");
39     public static final ColorEnum GREEN = new ColorEnum("Green");
40     public static final ColorEnum BLUE = new ColorEnum("Blue");
41     
42     public NestLinked() {
43         super();
44     }
45     
46     public static final class ColorEnum extends Enum {
47
48         static {
49             // Explicitly reference the class where the enums are defined
50
Object obj = NestLinked.RED;
51         }
52         
53         private ColorEnum(String color) {
54             super(color);
55         }
56
57         public static ColorEnum getEnum(String color) {
58             return (ColorEnum) getEnum(ColorEnum.class, color);
59         }
60
61         public static Map getEnumMap() {
62             return getEnumMap(ColorEnum.class);
63         }
64
65         public static List getEnumList() {
66             return getEnumList(ColorEnum.class);
67         }
68
69         public static Iterator iterator() {
70             return iterator(ColorEnum.class);
71         }
72     }
73 }
74
Popular Tags