All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.danilopianini.lang.ReverseEnumMap Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
/*
 * Copyright (C) 2010-2014, Danilo Pianini and contributors
 * listed in the project's pom.xml file.
 * 
 * This file is part of Alchemist, and is distributed under the terms of
 * the GNU General Public License, with a linking exception, as described
 * in the file LICENSE in the Alchemist distribution's top directory.
 */
package org.danilopianini.lang;

import java.io.Serializable;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;

/**
 * This class provides a cache for a generic enum, mapping each value to an int.
 * 
 * @author Danilo Pianini
 *
 * @param 
 */
public class ReverseEnumMap> implements Serializable {
	private static final long serialVersionUID = 660169408203724941L;
	private final TIntObjectMap map = new TIntObjectHashMap<>();
    
    /**
     * @param valueType the class to build the cache upon
     */
    public ReverseEnumMap(final Class valueType) {
        for (final V v : valueType.getEnumConstants()) {
            map.put(v.ordinal(), v);
        }
    }

    /**
     * Converts an integer to the corresponding enum value.
     * 
     * @param num the index of the enum
     * @return the enum value
     */
    public V get(final int num) {
        return map.get(num);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy