001 /* Enum.java - Base class for all enums 002 Copyright (C) 2004, 2005 Free Software Foundation 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package java.lang; 039 040 import java.io.Serializable; 041 import java.lang.reflect.Field; 042 043 /** 044 * This class represents a Java enumeration. All enumerations are 045 * subclasses of this class. 046 * 047 * @author Tom Tromey (tromey@redhat.com) 048 * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 049 * @since 1.5 050 */ 051 public abstract class Enum<T extends Enum<T>> 052 implements Comparable<T>, Serializable 053 { 054 055 /** 056 * For compatability with Sun's JDK 057 */ 058 private static final long serialVersionUID = -4300926546619394005L; 059 060 /** 061 * The name of this enum constant. 062 */ 063 final String name; 064 065 /** 066 * The number of this enum constant. Each constant is given a number 067 * which matches the order in which it was declared, starting with zero. 068 */ 069 final int ordinal; 070 071 /** 072 * This constructor is used by the compiler to create enumeration constants. 073 * 074 * @param name the name of the enumeration constant. 075 * @param ordinal the number of the enumeration constant, based on the 076 * declaration order of the constants and starting from zero. 077 */ 078 protected Enum(String name, int ordinal) 079 { 080 this.name = name; 081 this.ordinal = ordinal; 082 } 083 084 /** 085 * Returns an Enum for a enum class given a description string of 086 * the enum constant. 087 * 088 * @exception NullPointerException when etype or s are null. 089 * @exception IllegalArgumentException when there is no value s in 090 * the enum etype. 091 */ 092 public static <S extends Enum<S>> S valueOf(Class<S> etype, String s) 093 { 094 if (etype == null || s == null) 095 throw new NullPointerException(); 096 097 try 098 { 099 // XXX We should not use getDeclaredField, because it does 100 // an unnecessary security check. 101 Field f = etype.getDeclaredField(s); 102 if (! f.isEnumConstant()) 103 throw new IllegalArgumentException(s); 104 Class.setAccessible(f); 105 @SuppressWarnings("unchecked") 106 S val = (S) f.get(null); 107 return val; 108 } 109 catch (NoSuchFieldException exception) 110 { 111 throw new IllegalArgumentException(s); 112 } 113 catch (IllegalAccessException exception) 114 { 115 throw new Error("Unable to access Enum class"); 116 } 117 } 118 119 /** 120 * Returns true if this enumeration is equivalent to the supplied object, 121 * <code>o</code>. Only one instance of an enumeration constant exists, 122 * so the comparison is simply done using <code>==</code>. 123 * 124 * @param o the object to compare to this. 125 * @return true if <code>this == o</code>. 126 */ 127 public final boolean equals(Object o) 128 { 129 // Enum constants are singular, so we need only compare `=='. 130 return this == o; 131 } 132 133 /** 134 * Returns the hash code of this constant. This is simply the ordinal. 135 * 136 * @return the hash code of this enumeration constant. 137 */ 138 public final int hashCode() 139 { 140 return ordinal; 141 } 142 143 /** 144 * Returns a textual representation of this enumeration constant. 145 * By default, this is simply the declared name of the constant, but 146 * specific enumeration types may provide an implementation more suited 147 * to the data being stored. 148 * 149 * @return a textual representation of this constant. 150 */ 151 public String toString() 152 { 153 return name; 154 } 155 156 /** 157 * Returns an integer which represents the relative ordering of this 158 * enumeration constant. Enumeration constants are ordered by their 159 * ordinals, which represents their declaration order. So, comparing 160 * two identical constants yields zero, while one declared prior to 161 * this returns a positive integer and one declared after yields a 162 * negative integer. 163 * 164 * @param e the enumeration constant to compare. 165 * @return a negative integer if <code>e.ordinal < this.ordinal</code>, 166 * zero if <code>e.ordinal == this.ordinal</code> and a positive 167 * integer if <code>e.ordinal > this.ordinal</code>. 168 * @throws ClassCastException if <code>e</code> is not an enumeration 169 * constant of the same class. 170 */ 171 public final int compareTo(T e) 172 { 173 if (getDeclaringClass() != e.getDeclaringClass()) 174 throw new ClassCastException(); 175 return ordinal - e.ordinal; 176 } 177 178 /** 179 * Cloning of enumeration constants is prevented, to maintain their 180 * singleton status. 181 * 182 * @return the cloned object. 183 * @throws CloneNotSupportedException as enumeration constants can't be 184 * cloned. 185 */ 186 protected final Object clone() throws CloneNotSupportedException 187 { 188 throw new CloneNotSupportedException("can't clone an enum constant"); 189 } 190 191 /** 192 * Returns the name of this enumeration constant. 193 * 194 * @return the name of the constant. 195 */ 196 public final String name() 197 { 198 return name; 199 } 200 201 /** 202 * Returns the number of this enumeration constant, which represents 203 * the order in which it was originally declared, starting from zero. 204 * 205 * @return the number of this constant. 206 */ 207 public final int ordinal() 208 { 209 return ordinal; 210 } 211 212 /** 213 * Returns the type of this enumeration constant. This is the class 214 * corresponding to the declaration of the enumeration. 215 * 216 * @return the type of this enumeration constant. 217 */ 218 public final Class<T> getDeclaringClass() 219 { 220 Class k = getClass(); 221 // We might be in an anonymous subclass of the enum class, so go 222 // up one more level. 223 if (k.getSuperclass() != Enum.class) 224 k = k.getSuperclass(); 225 return k; 226 } 227 228 /** 229 * Enumerations can not have finalization methods. 230 * 231 * @since 1.6 232 */ 233 protected final void finalize() 234 { 235 } 236 237 }