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
28 /**
29 * A map to easily get the actual Enum instance from it's value as seen in the
30 * <a href="http://www.javaspecialists.co.za/archive/newsletter.do?issue=113">
31 * The JavaSpecialists newsletter</a>.
32 *
33 * @param <K>
34 * @param <E>
35 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36 * @version $Rev$, $Date$
37 */
38 public class ReverseEnumMap<K, E extends Enum<E> & EnumConverter<K>>
39 {
40 private Map<K, E> reverseMap = new HashMap<K, E>();
41
42
43 /**
44 * Creates a new instance of ReverseEnumMap.
45 *
46 * @param enumType
47 */
48 public ReverseEnumMap( Class<E> enumType )
49 {
50 for ( E e : enumType.getEnumConstants() )
51 {
52 reverseMap.put( e.convert(), e );
53 }
54 }
55
56
57 /**
58 * Return the enum given an ordinal value.
59 *
60 * @param value
61 * @return The enum.
62 */
63 public E get( K value )
64 {
65 E e = reverseMap.get( value );
66 if ( e == null )
67 {
68 throw new IllegalArgumentException( "Invalid enum value: " + value );
69 }
70 return e;
71 }
72 }