01: package snow.utils.gui;
02:
03: import java.util.*;
04:
05: /** Utilities to work with enumerations. Especially for their persistence.
06: */
07: public final class EnumUtils {
08: private EnumUtils() {
09: }
10:
11: public static final <E extends Enum<E>> E getEnumFromOrdinal(
12: int ord, Class<E> c) {
13: EnumSet<E> es = EnumSet.allOf(c);
14: Iterator<E> it = es.iterator();
15: while (it.hasNext()) {
16: E e = it.next();
17: if (e.ordinal() == ord)
18: return e;
19: }
20: return null;
21: }
22:
23: // Vector => can be used in combobox as-is
24: public static final <E extends Enum<E>> Vector<String> getEnumItemsAsString(
25: Class<E> c) {
26: Vector<String> items = new Vector<String>();
27: EnumSet<E> es = EnumSet.allOf(c);
28: Iterator<E> it = es.iterator();
29: while (it.hasNext()) {
30: E e = it.next();
31: items.add("" + e);
32: }
33: return items;
34: }
35:
36: public static final <E extends Enum<E>> Vector<E> getEnumItems(
37: Class<E> enumClass) {
38: Vector<E> items = new Vector<E>();
39: EnumSet<E> es = EnumSet.allOf(enumClass);
40: Iterator<E> it = es.iterator();
41: while (it.hasNext()) {
42: E e = it.next();
43: items.add(e);
44: }
45: return items;
46: }
47:
48: }
|