001package ball.util;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import java.util.TreeMap;
022import java.util.stream.Stream;
023
024/**
025 * {@link java.util.SortedMap} implementation which provides
026 * {@link String#CASE_INSENSITIVE_ORDER} look-up by name to the
027 * corresponding {@link Enum}.
028 *
029 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
030 */
031public class EnumLookupMap extends TreeMap<String,Enum<?>> {
032    private static final long serialVersionUID = -3569976523862652175L;
033
034    /**
035     * Sole constructor.
036     *
037     * @param   types           The {@link Enum} types to include.
038     */
039    @SafeVarargs @SuppressWarnings({ "varargs" })
040    public EnumLookupMap(Class<? extends Enum<?>>... types) {
041        super(String.CASE_INSENSITIVE_ORDER);
042
043        Stream.of(types)
044            .flatMap(t -> Stream.of(t.getEnumConstants()))
045            .forEach(t -> put(t.name(), t));
046    }
047}