001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://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.apache.commons.lang3;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collections;
022import java.util.EnumSet;
023import java.util.List;
024import java.util.Map;
025import java.util.Objects;
026import java.util.function.Function;
027import java.util.function.ToIntFunction;
028import java.util.stream.Collectors;
029import java.util.stream.Stream;
030
031/**
032 * Utility library to provide helper methods for Java enums.
033 *
034 * <p>#ThreadSafe#</p>
035 *
036 * @since 3.0
037 */
038public class EnumUtils {
039
040    private static final String CANNOT_STORE_S_S_VALUES_IN_S_BITS = "Cannot store %s %s values in %s bits";
041    private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined.";
042    private static final String NULL_ELEMENTS_NOT_PERMITTED = "null elements not permitted";
043    private static final String S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE = "%s does not seem to be an Enum type";
044
045    /**
046     * Validate {@code enumClass}.
047     * @param <E> the type of the enumeration
048     * @param enumClass to check
049     * @return {@code enumClass}
050     * @throws NullPointerException if {@code enumClass} is {@code null}
051     * @throws IllegalArgumentException if {@code enumClass} is not an enum class
052     * @since 3.2
053     */
054    private static <E extends Enum<E>> Class<E> asEnum(final Class<E> enumClass) {
055        Objects.requireNonNull(enumClass, ENUM_CLASS_MUST_BE_DEFINED);
056        Validate.isTrue(enumClass.isEnum(), S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE, enumClass);
057        return enumClass;
058    }
059
060    /**
061     * Validate that {@code enumClass} is compatible with representation in a {@code long}.
062     * @param <E> the type of the enumeration
063     * @param enumClass to check
064     * @return {@code enumClass}
065     * @throws NullPointerException if {@code enumClass} is {@code null}
066     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values
067     * @since 3.0.1
068     */
069    private static <E extends Enum<E>> Class<E> checkBitVectorable(final Class<E> enumClass) {
070        final E[] constants = asEnum(enumClass).getEnumConstants();
071        Validate.isTrue(constants.length <= Long.SIZE, CANNOT_STORE_S_S_VALUES_IN_S_BITS,
072            Integer.valueOf(constants.length), enumClass.getSimpleName(), Integer.valueOf(Long.SIZE));
073        return enumClass;
074    }
075
076    /**
077     * Creates a long bit vector representation of the given array of Enum values.
078     *
079     * <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
080     *
081     * <p>Do not use this method if you have more than 64 values in your Enum, as this
082     * would create a value greater than a long can hold.</p>
083     *
084     * @param enumClass the class of the enum we are working with, not {@code null}
085     * @param values    the values we want to convert, not {@code null}
086     * @param <E>       the type of the enumeration
087     * @return a long whose value provides a binary representation of the given set of enum values.
088     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
089     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values
090     * @since 3.0.1
091     * @see #generateBitVectors(Class, Iterable)
092     */
093    @SafeVarargs
094    public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) {
095        Validate.noNullElements(values);
096        return generateBitVector(enumClass, Arrays.asList(values));
097    }
098
099    /**
100     * Creates a long bit vector representation of the given subset of an Enum.
101     *
102     * <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
103     *
104     * <p>Do not use this method if you have more than 64 values in your Enum, as this
105     * would create a value greater than a long can hold.</p>
106     *
107     * @param enumClass the class of the enum we are working with, not {@code null}
108     * @param values    the values we want to convert, not {@code null}, neither containing {@code null}
109     * @param <E>       the type of the enumeration
110     * @return a long whose value provides a binary representation of the given set of enum values.
111     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
112     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values,
113     *                                  or if any {@code values} {@code null}
114     * @since 3.0.1
115     * @see #generateBitVectors(Class, Iterable)
116     */
117    public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final Iterable<? extends E> values) {
118        checkBitVectorable(enumClass);
119        Objects.requireNonNull(values, "values");
120        long total = 0;
121        for (final E constant : values) {
122            Objects.requireNonNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
123            total |= 1L << constant.ordinal();
124        }
125        return total;
126    }
127
128    /**
129     * Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.
130     *
131     * <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
132     *
133     * <p>Use this method if you have more than 64 values in your Enum.</p>
134     *
135     * @param enumClass the class of the enum we are working with, not {@code null}
136     * @param values    the values we want to convert, not {@code null}, neither containing {@code null}
137     * @param <E>       the type of the enumeration
138     * @return a long[] whose values provide a binary representation of the given set of enum values
139     *         with the least significant digits rightmost.
140     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
141     * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null}
142     * @since 3.2
143     */
144    @SafeVarargs
145    public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) {
146        asEnum(enumClass);
147        Validate.noNullElements(values);
148        final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
149        Collections.addAll(condensed, values);
150        final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
151        for (final E value : condensed) {
152            result[value.ordinal() / Long.SIZE] |= 1L << value.ordinal() % Long.SIZE;
153        }
154        ArrayUtils.reverse(result);
155        return result;
156    }
157
158    /**
159     * Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.
160     *
161     * <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
162     *
163     * <p>Use this method if you have more than 64 values in your Enum.</p>
164     *
165     * @param enumClass the class of the enum we are working with, not {@code null}
166     * @param values    the values we want to convert, not {@code null}, neither containing {@code null}
167     * @param <E>       the type of the enumeration
168     * @return a long[] whose values provide a binary representation of the given set of enum values
169     *         with the least significant digits rightmost.
170     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
171     * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null}
172     * @since 3.2
173     */
174    public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final Iterable<? extends E> values) {
175        asEnum(enumClass);
176        Objects.requireNonNull(values, "values");
177        final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
178        values.forEach(constant -> condensed.add(Objects.requireNonNull(constant, NULL_ELEMENTS_NOT_PERMITTED)));
179        final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
180        for (final E value : condensed) {
181            result[value.ordinal() / Long.SIZE] |= 1L << value.ordinal() % Long.SIZE;
182        }
183        ArrayUtils.reverse(result);
184        return result;
185    }
186
187    /**
188     * Gets the enum for the class, returning {@code null} if not found.
189     *
190     * <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
191     * for an invalid enum name.</p>
192     *
193     * @param <E> the type of the enumeration
194     * @param enumClass  the class of the enum to query, not null
195     * @param enumName   the enum name, null returns null
196     * @return the enum, null if not found
197     */
198    public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) {
199        return getEnum(enumClass, enumName, null);
200    }
201
202    /**
203     * Gets the enum for the class, returning {@code defaultEnum} if not found.
204     *
205     * <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
206     * for an invalid enum name.</p>
207     *
208     * @param <E> the type of the enumeration
209     * @param enumClass   the class of the enum to query, not null
210     * @param enumName    the enum name, null returns default enum
211     * @param defaultEnum the default enum
212     * @return the enum, default enum if not found
213     * @since 3.10
214     */
215    public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName, final E defaultEnum) {
216        if (enumName == null) {
217            return defaultEnum;
218        }
219        try {
220            return Enum.valueOf(enumClass, enumName);
221        } catch (final IllegalArgumentException ex) {
222            return defaultEnum;
223        }
224    }
225
226    /**
227     * Gets the enum for the class, returning {@code null} if not found.
228     *
229     * <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
230     * for an invalid enum name and performs case insensitive matching of the name.</p>
231     *
232     * @param <E>         the type of the enumeration
233     * @param enumClass   the class of the enum to query, not null
234     * @param enumName    the enum name, null returns null
235     * @return the enum, null if not found
236     * @since 3.8
237     */
238    public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName) {
239        return getEnumIgnoreCase(enumClass, enumName, null);
240    }
241
242    /**
243     * Gets the enum for the class, returning {@code defaultEnum} if not found.
244     *
245     * <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
246     * for an invalid enum name and performs case insensitive matching of the name.</p>
247     *
248     * @param <E>         the type of the enumeration
249     * @param enumClass   the class of the enum to query, not null
250     * @param enumName    the enum name, null returns default enum
251     * @param defaultEnum the default enum
252     * @return the enum, default enum if not found
253     * @since 3.10
254     */
255    public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName,
256        final E defaultEnum) {
257        return getFirstEnumIgnoreCase(enumClass, enumName, Enum::name, defaultEnum);
258    }
259
260    /**
261     * Gets the {@link List} of enums.
262     *
263     * <p>This method is useful when you need a list of enums rather than an array.</p>
264     *
265     * @param <E> the type of the enumeration
266     * @param enumClass  the class of the enum to query, not null
267     * @return the modifiable list of enums, never null
268     */
269    public static <E extends Enum<E>> List<E> getEnumList(final Class<E> enumClass) {
270        return new ArrayList<>(Arrays.asList(enumClass.getEnumConstants()));
271    }
272
273    /**
274     * Gets the {@link Map} of enums by name.
275     *
276     * <p>This method is useful when you need a map of enums by name.</p>
277     *
278     * @param <E> the type of the enumeration
279     * @param enumClass  the class of the enum to query, not null
280     * @return the modifiable map of enum names to enums, never null
281     */
282    public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
283        return getEnumMap(enumClass, E::name);
284    }
285
286    /**
287     * Gets the {@link Map} of enums by name.
288     *
289     * <p>
290     * This method is useful when you need a map of enums by name.
291     * </p>
292     *
293     * @param <E>         the type of enumeration
294     * @param <K>         the type of the map key
295     * @param enumClass   the class of the enum to query, not null
296     * @param keyFunction the function to query for the key, not null
297     * @return the modifiable map of enums, never null
298     * @since 3.13.0
299     */
300    public static <E extends Enum<E>, K> Map<K, E> getEnumMap(final Class<E> enumClass, final Function<E, K> keyFunction) {
301        return Stream.of(enumClass.getEnumConstants()).collect(Collectors.toMap(keyFunction::apply, Function.identity()));
302    }
303
304    /**
305     * Gets the enum for the class in a system property, returning {@code defaultEnum} if not found.
306     *
307     * <p>
308     * This method differs from {@link Enum#valueOf} in that it does not throw an exception for an invalid enum name.
309     * </p>
310     *
311     * @param <E> the type of the enumeration
312     * @param enumClass the class of the enum to query, not null
313     * @param propName the system property key for the enum name, null returns default enum
314     * @param defaultEnum the default enum
315     * @return the enum, default enum if not found
316     * @since 3.13.0
317     */
318    public static <E extends Enum<E>> E getEnumSystemProperty(final Class<E> enumClass, final String propName,
319        final E defaultEnum) {
320        return enumClass == null || propName == null ? defaultEnum
321            : getEnum(enumClass, System.getProperty(propName), defaultEnum);
322    }
323
324    /**
325     * Gets the enum for the class and value, returning {@code defaultEnum} if not found.
326     *
327     * <p>
328     * This method differs from {@link Enum#valueOf} in that it does not throw an exception for an invalid enum name and performs case insensitive matching of
329     * the name.
330     * </p>
331     *
332     * @param <E>           the type of the enumeration.
333     * @param enumClass     the class of the enum to query, not null.
334     * @param value         the enum name, null returns default enum.
335     * @param toIntFunction the function that gets an int for an enum for comparison to {@code value}.
336     * @param defaultEnum   the default enum.
337     * @return an enum, default enum if not found.
338     * @since 3.18.0
339     */
340    public static <E extends Enum<E>> E getFirstEnum(final Class<E> enumClass, final int value, final ToIntFunction<E> toIntFunction, final E defaultEnum) {
341        if (isEnum(enumClass)) {
342            return defaultEnum;
343        }
344        return Stream.of(enumClass.getEnumConstants()).filter(e -> value == toIntFunction.applyAsInt(e)).findFirst().orElse(defaultEnum);
345    }
346
347    /**
348     * Gets the enum for the class, returning {@code defaultEnum} if not found.
349     *
350     * <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
351     * for an invalid enum name and performs case insensitive matching of the name.</p>
352     *
353     * @param <E>         the type of the enumeration
354     * @param enumClass   the class of the enum to query, not null
355     * @param enumName    the enum name, null returns default enum
356     * @param stringFunction the function that gets the string for an enum for comparison to {@code enumName}.
357     * @param defaultEnum the default enum
358     * @return an enum, default enum if not found
359     * @since 3.13.0
360     */
361    public static <E extends Enum<E>> E getFirstEnumIgnoreCase(final Class<E> enumClass, final String enumName, final Function<E, String> stringFunction,
362            final E defaultEnum) {
363            if (enumName == null || !enumClass.isEnum()) {
364                return defaultEnum;
365            }
366            return Stream.of(enumClass.getEnumConstants()).filter(e -> enumName.equalsIgnoreCase(stringFunction.apply(e))).findFirst().orElse(defaultEnum);
367        }
368
369    private static <E extends Enum<E>> boolean isEnum(final Class<E> enumClass) {
370        return enumClass != null && !enumClass.isEnum();
371    }
372
373    /**
374     * Checks if the specified name is a valid enum for the class.
375     *
376     * <p>This method differs from {@link Enum#valueOf} in that it checks if the name is
377     * a valid enum without needing to catch the exception.</p>
378     *
379     * @param <E> the type of the enumeration
380     * @param enumClass  the class of the enum to query, not null
381     * @param enumName   the enum name, null returns false
382     * @return true if the enum name is valid, otherwise false
383     */
384    public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) {
385        return getEnum(enumClass, enumName) != null;
386    }
387
388    /**
389     * Checks if the specified name is a valid enum for the class.
390     *
391     * <p>This method differs from {@link Enum#valueOf} in that it checks if the name is
392     * a valid enum without needing to catch the exception
393     * and performs case insensitive matching of the name.</p>
394     *
395     * @param <E> the type of the enumeration
396     * @param enumClass  the class of the enum to query, not null
397     * @param enumName   the enum name, null returns false
398     * @return true if the enum name is valid, otherwise false
399     * @since 3.8
400     */
401    public static <E extends Enum<E>> boolean isValidEnumIgnoreCase(final Class<E> enumClass, final String enumName) {
402        return getEnumIgnoreCase(enumClass, enumName) != null;
403    }
404
405    /**
406     * Convert a long value created by {@link EnumUtils#generateBitVector} into the set of
407     * enum values that it represents.
408     *
409     * <p>If you store this value, beware any changes to the enum that would affect ordinal values.</p>
410     * @param enumClass the class of the enum we are working with, not {@code null}
411     * @param value     the long value representation of a set of enum values
412     * @param <E>       the type of the enumeration
413     * @return a set of enum values
414     * @throws NullPointerException if {@code enumClass} is {@code null}
415     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values
416     * @since 3.0.1
417     */
418    public static <E extends Enum<E>> EnumSet<E> processBitVector(final Class<E> enumClass, final long value) {
419        checkBitVectorable(enumClass).getEnumConstants();
420        return processBitVectors(enumClass, value);
421    }
422
423    /**
424     * Convert a {@code long[]} created by {@link EnumUtils#generateBitVectors} into the set of
425     * enum values that it represents.
426     *
427     * <p>If you store this value, beware any changes to the enum that would affect ordinal values.</p>
428     * @param enumClass the class of the enum we are working with, not {@code null}
429     * @param values     the long[] bearing the representation of a set of enum values, the least significant digits rightmost, not {@code null}
430     * @param <E>       the type of the enumeration
431     * @return a set of enum values
432     * @throws NullPointerException if {@code enumClass} is {@code null}
433     * @throws IllegalArgumentException if {@code enumClass} is not an enum class
434     * @since 3.2
435     */
436    public static <E extends Enum<E>> EnumSet<E> processBitVectors(final Class<E> enumClass, final long... values) {
437        final EnumSet<E> results = EnumSet.noneOf(asEnum(enumClass));
438        final long[] lvalues = ArrayUtils.clone(Objects.requireNonNull(values, "values"));
439        ArrayUtils.reverse(lvalues);
440        for (final E constant : enumClass.getEnumConstants()) {
441            final int block = constant.ordinal() / Long.SIZE;
442            if (block < lvalues.length && (lvalues[block] & 1L << constant.ordinal() % Long.SIZE) != 0) {
443                results.add(constant);
444            }
445        }
446        return results;
447    }
448
449    /**
450     * This constructor is public to permit tools that require a JavaBean
451     * instance to operate.
452     *
453     * @deprecated TODO Make private in 4.0.
454     */
455    @Deprecated
456    public EnumUtils() {
457        // empty
458    }
459}