1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 21 package org.apache.directory.server.dns.util; 22 23 24 import java.util.HashMap; 25 import java.util.Map; 26 27 import org.apache.directory.server.i18n.I18n; 28 29 30 /** 31 * A map to easily get the actual Enum instance from it's value as seen in the 32 * <a href="http://www.javaspecialists.co.za/archive/newsletter.do?issue=113"> 33 * The JavaSpecialists newsletter</a>. 34 * 35 * @param <K> 36 * @param <E> 37 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 38 */ 39 public class ReverseEnumMap<K, E extends Enum<E> & EnumConverter<K>> 40 { 41 private Map<K, E> reverseMap = new HashMap<>(); 42 43 44 /** 45 * Creates a new instance of ReverseEnumMap. 46 * 47 * @param enumType 48 */ 49 public ReverseEnumMap( Class<E> enumType ) 50 { 51 for ( E e : enumType.getEnumConstants() ) 52 { 53 reverseMap.put( e.convert(), e ); 54 } 55 } 56 57 58 /** 59 * Return the enum given an ordinal value. 60 * 61 * @param value 62 * @return The enum. 63 */ 64 public E get( K value ) 65 { 66 E e = reverseMap.get( value ); 67 if ( e == null ) 68 { 69 throw new IllegalArgumentException( I18n.err( I18n.ERR_650, value ) ); 70 } 71 return e; 72 } 73 }