001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.enums;
018
019import java.util.HashMap;
020import java.util.Map;
021
022/**
023 * This class provides a map from enum constant names to its actual enum values.
024 * <p>
025 * This can be used instead of EnumUtils.valueOf() (or <enumclass>.valueOf) for
026 * performance reasons, especially if the lookup of constant names is expected
027 * to fail often. Otherwise EnumUtils.valueOf() (or <enumclass>.valueOf) have a
028 * similar performance.
029 * <p>
030 * In case of looking up strings that do not exist as enum constant EnumUtils
031 * has to catch an IllegalArgumentException which has an even bigger performance
032 * impact. This implementation simply returns <code>null</code> if the element
033 * is not found without this overhead.
034 */
035public class EnumValueMap<E extends Enum<E>> {
036
037        /** Lookup map from element names to enum values. */
038        private final Map<String, E> nameToValues = new HashMap<String, E>();
039
040        /** Flag that determines whether the name lookup is case sensitive. */
041        private final boolean caseSensisitve;
042
043        /** Constructor for a case sensitive map. */
044        public EnumValueMap(Class<E> enumType) {
045                this(enumType, true);
046        }
047
048        /** Constructor. */
049        public EnumValueMap(Class<E> enumType, boolean caseSensisitve) {
050                this.caseSensisitve = caseSensisitve;
051                for (E e : enumType.getEnumConstants()) {
052                        nameToValues.put(getKey(e.name()), e);
053                }
054        }
055
056        /** Returns the lookup map key depending on the case-sensitivity. */
057        private String getKey(String name) {
058                if (caseSensisitve) {
059                        return name;
060                }
061                return name.toLowerCase();
062        }
063
064        /** Gets the enum value with the given name from an internal lookup map. */
065        public E valueOf(String name) {
066                return nameToValues.get(getKey(name));
067        }
068
069}