public static final int SUNDAY = 0; public static final int MONDAY = 1; public static final int TUESDAY = 2; public static final int WEDNESDAY = 3; public static final int THURSDAY = 4; public static final int FRIDAY = 5; public static final int SATURDAY = 6;
for (Day d = SUNDAY; d <= SATURDAY; ++d) { switch(d) { case MONDAY: ...; break; case TUESDAY: ...; break; case WEDNESDAY: ...; break; case THURSDAY: ...; break; case FRIDAY: ...; break; case SATURDAY: case SUNDAY: ...; } }
public static final int SUNDAY = 0; public static final int MONDAY = 1; public static final int TUESDAY = 2; public static final int WEDNESDAY = 3; public static final int THURSDAY = 4; public static final int FRIDAY = 5; public static final int SATURDAY = 6;
import java.util.Map; import java.util.HashMap; import java.util.Iterator; import java.io.Serializable; import java.io.InvalidObjectException; public final class Day implements Comparable, Serializable { private static int size = 0; private static int nextOrd = 0; private static Map nameMap = new HashMap(10); private static Day first = null; private static Day last = null; private final int ord; private final String label; private Day prev; private Day next; public static final Day SUNDAY = new Day("SUNDAY"); public static final Day MONDAY = new Day("MONDAY"); public static final Day TUESDAY = new Day("TUESDAY"); public static final Day WEDNESDAY = new Day("WEDNESDAY"); public static final Day THURSDAY = new Day("THURSDAY"); public static final Day FRIDAY = new Day("FRIDAY"); public static final Day SATURDAY = new Day("SATURDAY"); /** * 用所给的标签创建一个新的day. * (Uses default value for ord.) */ private Day(String label) { this(label, nextOrd); } /** * Constructs a new Day with its label and ord value. */ private Day(String label, int ord) { this.label = label; this.ord = ord; ++size; nextOrd = ord + 1; nameMap.put(label, this); if (first == null) first = this; if (last != null) { this.prev = last; last.next = this; } last = this; } /** * Compares two Day objects based on their ordinal values. * Satisfies requirements of interface java.lang.Comparable. */ public int compareTo(Object obj) { return ord - ((Day)obj).ord; } /** * Compares two Day objects for equality. Returns true * only if the specified Day is equal to this one. */ public boolean equals(Object obj) { return super.equals(obj); } /** * Returns a hash code value for this Day. */ public int hashCode() { return super.hashCode(); } /** * Resolves deserialized Day objects. * @throws InvalidObjectException if deserialization fails. */ private Object readResolve() throws InvalidObjectException { Day d = get(label); if (d != null) return d; else { String msg = "invalid deserialized object: label = "; throw new InvalidObjectException(msg + label); } } /** * Returns Day with the specified label. * Returns null if not found. */ public static Day get(String label) { return (Day) nameMap.get(label); } /** * Returns the label for this Day. */ public String toString() { return label; } /** * Always throws CloneNotSupportedException; guarantees that * Day objects are never cloned. * * @return (never returns) */ protected Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } /** * Returns an iterator over all Day objects in declared order. */ public static Iterator iterator() { // anonymous inner class return new Iterator() { private Day current = first; public boolean hasNext() { return current != null; } public Object next() { Day d = current; current = current.next(); return d; } public void remove() { throw new UnsupportedOperationException(); } }; } /** * Returns the ordinal value of this Day. */ public int ord() { return this.ord; } /** * Returns the number of declared Day objects. */ public static int size() { return size; } /** * Returns the first declared Day. */ public static Day first() { return first; } /** * Returns the last declared Day. */ public static Day last() { return last; } /** * Returns the previous Day before this one in declared order. * Returns null for the first declared Day. */ public Day prev() { return this.prev; } /** * Returns the next Day after this one in declared order. * Returns null for the last declared Day. */ public Day next() { return this.next; } }