IntEnum.java 3.02 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Matt Tucker's avatar
Matt Tucker committed
19
 */
20

Matt Tucker's avatar
Matt Tucker committed
21 22
package org.jivesoftware.util;

23
import java.util.*;
Matt Tucker's avatar
Matt Tucker committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37

/**
 * <p>A type safe enumeration object that is keyed by an Int
 * value for switch statements and storage in DBs.</p>
 * <p/>
 * <p>Used for indicating distinct states in a generic manner
 * where each enum should have an associated int value. The
 * given int should be unique for each enum value as hashCode
 * and equals depends solely on the int value given. Most
 * child classes should extend IntEnum and create static instances.</p>
 *
 * @author Iain Shigeoka
 */
public class IntEnum extends Enum {
38

Matt Tucker's avatar
Matt Tucker committed
39
    private int value;
40
    protected static Hashtable enumTypes = new Hashtable();
Matt Tucker's avatar
Matt Tucker committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    protected IntEnum(String name, int val) {
        super(name);
        this.value = val;
    }

    /**
     * Returns the int value associated with the enum.
     *
     * @return the int value of the enum.
     */
    public int getValue() {
        return value;
    }

56 57
    @Override
	public boolean equals(Object object) {
Matt Tucker's avatar
Matt Tucker committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        if (this == object) {
            return true;
        }
        else if ((this.getClass().isInstance(object)) && value == (((IntEnum)object).value)) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * <p>Checks in an enum for use in the getEnumFromInt() method.</p>
     *
     * @param enumeration The enum to be registered
     */
    protected static void register(IntEnum enumeration) {
75
        Map enums = (Map)enumTypes.get(enumeration.getClass());
Matt Tucker's avatar
Matt Tucker committed
76
        if (enums == null) {
77
            enums = new HashMap<Integer,Object>();
Matt Tucker's avatar
Matt Tucker committed
78 79 80 81 82 83 84 85 86 87 88 89 90
            enumTypes.put(enumeration.getClass(), enums);
        }
        enums.put(enumeration.getValue(), enumeration);
    }

    /**
     * <p>Obtain the enum associated with the given value.</p>
     * <p>Values must be registered earlier using the register() method.</p>
     *
     * @param value the value to lookup the enum for
     * @return The associated enum or null if no matching enum exists
     */
    protected static IntEnum getEnumFromInt(Class enumClass, int value) {
91
        Map enums = (Map)enumTypes.get(enumClass);
Matt Tucker's avatar
Matt Tucker committed
92 93 94 95 96 97
        if (enums != null) {
            return (IntEnum)enums.get(value);
        }
        return null;
    }

98 99
    @Override
	public int hashCode() {
Matt Tucker's avatar
Matt Tucker committed
100 101 102
        return value;
    }

103 104
    @Override
	public String toString() {
Matt Tucker's avatar
Matt Tucker committed
105 106
        return Integer.toString(value) + " " + super.toString();
    }
107
}