View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.util.codingsupport;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.NoSuchElementException;
22  
23  /**
24   * The parent class for comparable enums using the typesafe enum pattern.
25   * <p>
26   * <h2>How to Use</h2>
27   * <H3>Usage example</H3>
28   * The following code shows how to write an enumeration with the three possible
29   * values <BR>
30   * <code>{EXPECTED, NORMAL, UNEXPECTED}</code>:<code> <pre>
31   *
32   *     /**
33   *      * Severity Levels for reporting exceptions with code traces.
34   *      * /
35   *     static public class SeverityLevel extends AbstractComparableEnum {
36   *
37   *         private SeverityLevel (String name, int code)
38   *         {
39   *             super (name, code);
40   *         }
41   *
42   *         static public final SeverityLevel EXPECTED =
43   *             new SeverityLevel (&quot;EXPECTED&quot;,1);
44   *
45   *         static public final SeverityLevel NORMAL =
46   *             new SeverityLevel (&quot;NORMAL&quot;,2);
47   *
48   *         static public final SeverityLevel UNEXPECTED =
49   *             new SeverityLevel (&quot;UNEXPECTED&quot;,3);
50   *     }
51   *
52   * </pre> </code> Please refer also to the Javadoc of the uncomparable enums in
53   * {@link AbstractDefaultEnum}. The duplication of <code>readResolve</code>
54   * method in each enumeration is not needed because JDK 1.2.2 is no longer
55   * supported by EL4J.
56   *
57   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/codingsupport/AbstractComparableEnum.java $
58   *
59   * @author Raphael Boog (RBO)
60   */
61  public abstract class AbstractComparableEnum extends AbstractDefaultEnum
62  	implements Comparable<AbstractComparableEnum> {
63  	/**
64  	 * The list of all comparable enums ever created.
65  	 */
66  	protected static final ArrayList<AbstractComparableEnum> ENUM_LIST =
67  		new ArrayList<AbstractComparableEnum>();
68  
69  	/**
70  	 * The ordinal assigned to this comparable enum. This value is directly used
71  	 * for comparison.
72  	 */
73  	protected final int m_ordinal;
74  
75  	/**
76  	 * Override this default constructor with your own constructor.
77  	 *
78  	 * @param name Is the name.
79  	 * @param code Is the code.
80  	 */
81  	protected AbstractComparableEnum(String name, int code) {
82  		super(name, code);
83  
84  		synchronized (AbstractComparableEnum.class) {
85  			m_ordinal = ENUM_LIST.size();
86  			ENUM_LIST.add(this);
87  		}
88  	}
89  
90  	/**
91  	 * @param myClass Is the type to count.
92  	 * @return Return the number of enums of an enumeration.
93  	 */
94  	protected static int size(Class<?> myClass) {
95  		int result = 0;
96  
97  		synchronized (AbstractComparableEnum.class) {
98  			for (int i = 0; i < ENUM_LIST.size(); i++) {
99  				if (myClass == ENUM_LIST.get(i).getClass()) {
100 					result++;
101 				}
102 			}
103 		}
104 
105 		return result;
106 	}
107 
108 	/**
109 	 * @param myClass Is the class to be used.
110 	 * @return Return an iterator for the elements of the specified enum class.
111 	 */
112 	protected static Iterator<AbstractComparableEnum> iterator(final Class<?> myClass) {
113 		Iterator<AbstractComparableEnum> it = new Iterator<AbstractComparableEnum>() {
114 			private int m_currentOrdinal = -1;
115 
116 			public boolean hasNext() {
117 				synchronized (AbstractComparableEnum.class) {
118 					for (int i = m_currentOrdinal + 1;
119 						i < ENUM_LIST.size(); i++) {
120 						if (myClass == ENUM_LIST.get(i).getClass()) {
121 							m_currentOrdinal = i - 1;
122 
123 							return true;
124 						}
125 					}
126 				}
127 
128 				return false;
129 			}
130 
131 			public AbstractComparableEnum next() {
132 				synchronized (AbstractComparableEnum.class) {
133 					for (int i = m_currentOrdinal + 1;
134 						i < ENUM_LIST.size(); i++) {
135 						if (myClass == ENUM_LIST.get(i).getClass()) {
136 							m_currentOrdinal = i;
137 
138 							return ENUM_LIST.get(i);
139 						}
140 					}
141 				}
142 
143 				throw new NoSuchElementException();
144 			}
145 
146 			public void remove() {
147 				throw new UnsupportedOperationException();
148 			}
149 		};
150 
151 		return it;
152 	}
153 
154 	/**
155 	 * {@inheritDoc}
156 	 */
157 	public int compareTo(AbstractComparableEnum o) {
158 		return m_ordinal - ((AbstractComparableEnum) o).m_ordinal;
159 	}
160 }