views:

83

answers:

4

Hello, how can I do to simulate a class of type enum in java <5.0 ..??

public final class Week {

    private static final Day[] _week = {Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY};

    public static Day getDayOfWeek(final int index) {
        if (index >= 1 && index <= 7) {
            return _week[index - 1];
        }
        throw new IndexOutOfBoundsException("Invalid index [1..7]");
    }

    public static final class Day {
        public static final Day MONDAY = new Day(1, "Monday");
        public static final Day TUESDAY = new Day(2, "Tuesday");
        public static final Day WEDNESDAY = new Day(3, "Wednesday");
        public static final Day THURSDAY = new Day(4, "Thursday");
        public static final Day FRIDAY = new Day(5, "Friday");
        public static final Day SATURDAY = new Day(6, "Saturday");
        public static final Day SUNDAY = new Day(7, "Sunday");

        private int _value;
        private String _name;

        private Day(final int value, final String name) {
            _value = value;
            _name = name;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return "[Day] " + _name;
        }

        /* (non-Javadoc)
         * @see java.lang.String#equals(java.lang.Object)
         */
        public boolean equals(Object anObject) {
            Day d = (Day)anObject;
            return _value == d._value;
        }
    }

    public static void main(String[] agrs) {
         System.out.println(Week.getDayOfWeek(2));
    }
}
+1  A: 

Joshua Bloch shows you how in the first edition of his "Effective Java". I have to go to work, so I can't type in details now. More to follow.

duffymo
thx for your response ;)
Mercer
+4  A: 

Joshua Bloch wrote, long ago, an article explaining it.

Riduidel
thx for your response ;)
Mercer
+4  A: 

Use the typesafe enum described in effective java. Here is an example given from Joshua Blochs article on this:

// The typesafe enum pattern
public class Suit {
    private final String name;

    private Suit(String name) { this.name = name; }

    public String toString()  { return name; }

    public static final Suit CLUBS =
        new Suit("clubs");
    public static final Suit DIAMONDS =
        new Suit("diamonds");
    public static final Suit HEARTS =
        new Suit("hearts");
    public static final Suit SPADES =
        new Suit("spades");
}

If you want your typesafe enum to be Serializable, remember to control reconstruction via the readResolve method.

krock
add equals and hashCode support :)
helios
thx, so how it works. If i do thos Suit("spades") What will I get ..??
Mercer
@helios, when controlling construction like this the equals method is equivalent to using `==` as there will only ever be one instance of each distinct Suit.
krock
@Mercer you use this by calling `Suit.CLUBS` or `Suit.SPADES` just like you would if Suit was a Java 5 enum. If you want to resolve a `Suit` from a string then you need to provide another method that will compare the string to each of the Suit names to see which one matches.
krock
@krock: d'oh! I'm not very bright today :)
helios
A: 

The solution Krock and Riduidel are suggesting is pretty neat, but AFAIK a switch-case statement won't be possible as it's applicable only for convertible int values and enum constants. switch-case combination is a nice feature, more readable than a bunch of ifs (BTW: switch-case will be compiled as a bunch of ifs anyway, correct?). So, to complete the discussed pattern, could someone tell whether there is a quick way to enable this patteren with functionality allowing switch-case? Adding an int property and getter for it seems the simplest way, but is it the optimal?

delirus