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