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) 2009, 2015, Danilo Pianini and contributors
 * listed in the project's build.gradle or pom.xml file.
 *
 * This file is distributed under the terms of the Apache License, version 2.0
 *******************************************************************************/
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 - 2024 Weber Informatics LLC | Privacy Policy