1 16 package org.apache.commons.lang.enums; 17 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.apache.commons.lang.ClassUtils; 22 23 103 public abstract class ValuedEnum extends Enum { 104 105 106 private static final long serialVersionUID = -7129650521543789085L; 107 108 111 private final int iValue; 112 113 119 protected ValuedEnum(String name, int value) { 120 super(name); 121 iValue = value; 122 } 123 124 136 protected static Enum getEnum(Class enumClass, int value) { 137 if (enumClass == null) { 138 throw new IllegalArgumentException ("The Enum Class must not be null"); 139 } 140 List list = Enum.getEnumList(enumClass); 141 for (Iterator it = list.iterator(); it.hasNext();) { 142 ValuedEnum enumeration = (ValuedEnum) it.next(); 143 if (enumeration.getValue() == value) { 144 return enumeration; 145 } 146 } 147 return null; 148 } 149 150 155 public final int getValue() { 156 return iValue; 157 } 158 159 172 public int compareTo(Object other) { 173 return iValue - ((ValuedEnum) other).iValue; 174 } 175 176 183 public String toString() { 184 if (iToString == null) { 185 String shortName = ClassUtils.getShortClassName(getEnumClass()); 186 iToString = shortName + "[" + getName() + "=" + getValue() + "]"; 187 } 188 return iToString; 189 } 190 } 191 | Popular Tags |