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 *      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.apache.commons.lang3;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collections;
022import java.util.EnumSet;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * <p>Utility library to provide helper methods for Java enums.</p>
029 *
030 * <p>#ThreadSafe#</p>
031 *
032 * @since 3.0
033 */
034public class EnumUtils {
035
036    private static final String NULL_ELEMENTS_NOT_PERMITTED = "null elements not permitted";
037    private static final String CANNOT_STORE_S_S_VALUES_IN_S_BITS = "Cannot store %s %s values in %s bits";
038    private static final String S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE = "%s does not seem to be an Enum type";
039    private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined.";
040
041    /**
042     * This constructor is public to permit tools that require a JavaBean
043     * instance to operate.
044     */
045    public EnumUtils() {
046    }
047
048    /**
049     * <p>Gets the {@code Map} of enums by name.</p>
050     *
051     * <p>This method is useful when you need a map of enums by name.</p>
052     *
053     * @param <E> the type of the enumeration
054     * @param enumClass  the class of the enum to query, not null
055     * @return the modifiable map of enum names to enums, never null
056     */
057    public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
058        final Map<String, E> map = new LinkedHashMap<String, E>();
059        for (final E e: enumClass.getEnumConstants()) {
060            map.put(e.name(), e);
061        }
062        return map;
063    }
064
065    /**
066     * <p>Gets the {@code List} of enums.</p>
067     *
068     * <p>This method is useful when you need a list of enums rather than an array.</p>
069     *
070     * @param <E> the type of the enumeration
071     * @param enumClass  the class of the enum to query, not null
072     * @return the modifiable list of enums, never null
073     */
074    public static <E extends Enum<E>> List<E> getEnumList(final Class<E> enumClass) {
075        return new ArrayList<E>(Arrays.asList(enumClass.getEnumConstants()));
076    }
077
078    /**
079     * <p>Checks if the specified name is a valid enum for the class.</p>
080     *
081     * <p>This method differs from {@link Enum#valueOf} in that checks if the name is
082     * a valid enum without needing to catch the exception.</p>
083     *
084     * @param <E> the type of the enumeration
085     * @param enumClass  the class of the enum to query, not null
086     * @param enumName   the enum name, null returns false
087     * @return true if the enum name is valid, otherwise false
088     */
089    public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) {
090        if (enumName == null) {
091            return false;
092        }
093        try {
094            Enum.valueOf(enumClass, enumName);
095            return true;
096        } catch (final IllegalArgumentException ex) {
097            return false;
098        }
099    }
100
101    /**
102     * <p>Gets the enum for the class, returning {@code null} if not found.</p>
103     *
104     * <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
105     * for an invalid enum name.</p>
106     *
107     * @param <E> the type of the enumeration
108     * @param enumClass  the class of the enum to query, not null
109     * @param enumName   the enum name, null returns null
110     * @return the enum, null if not found
111     */
112    public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) {
113        if (enumName == null) {
114            return null;
115        }
116        try {
117            return Enum.valueOf(enumClass, enumName);
118        } catch (final IllegalArgumentException ex) {
119            return null;
120        }
121    }
122
123    /**
124     * <p>Creates a long bit vector representation of the given subset of an Enum.</p>
125     *
126     * <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
127     *
128     * <p>Do not use this method if you have more than 64 values in your Enum, as this
129     * would create a value greater than a long can hold.</p>
130     *
131     * @param enumClass the class of the enum we are working with, not {@code null}
132     * @param values    the values we want to convert, not {@code null}, neither containing {@code null}
133     * @param <E>       the type of the enumeration
134     * @return a long whose value provides a binary representation of the given set of enum values.
135     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
136     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values,
137     *                                  or if any {@code values} {@code null}
138     * @since 3.0.1
139     * @see #generateBitVectors(Class, Iterable)
140     */
141    public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final Iterable<? extends E> values) {
142        checkBitVectorable(enumClass);
143        Validate.notNull(values);
144        long total = 0;
145        for (final E constant : values) {
146            Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED);
147            total |= 1L << constant.ordinal();
148        }
149        return total;
150    }
151
152    /**
153     * <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p>
154     *
155     * <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
156     *
157     * <p>Use this method if you have more than 64 values in your Enum.</p>
158     *
159     * @param enumClass the class of the enum we are working with, not {@code null}
160     * @param values    the values we want to convert, not {@code null}, neither containing {@code null}
161     * @param <E>       the type of the enumeration
162     * @return a long[] whose values provide a binary representation of the given set of enum values
163     *         with least significant digits rightmost.
164     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
165     * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null}
166     * @since 3.2
167     */
168    public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final Iterable<? extends E> values) {
169        asEnum(enumClass);
170        Validate.notNull(values);
171        final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
172        for (final E constant : values) {
173            Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED);
174            condensed.add(constant);
175        }
176        final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
177        for (final E value : condensed) {
178            result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE);
179        }
180        ArrayUtils.reverse(result);
181        return result;
182    }
183
184    /**
185     * <p>Creates a long bit vector representation of the given array of Enum values.</p>
186     *
187     * <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
188     *
189     * <p>Do not use this method if you have more than 64 values in your Enum, as this
190     * would create a value greater than a long can hold.</p>
191     *
192     * @param enumClass the class of the enum we are working with, not {@code null}
193     * @param values    the values we want to convert, not {@code null}
194     * @param <E>       the type of the enumeration
195     * @return a long whose value provides a binary representation of the given set of enum values.
196     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
197     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values
198     * @since 3.0.1
199     * @see #generateBitVectors(Class, Iterable)
200     */
201    public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) {
202        Validate.noNullElements(values);
203        return generateBitVector(enumClass, Arrays.<E> asList(values));
204    }
205
206    /**
207     * <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p>
208     *
209     * <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
210     *
211     * <p>Use this method if you have more than 64 values in your Enum.</p>
212     *
213     * @param enumClass the class of the enum we are working with, not {@code null}
214     * @param values    the values we want to convert, not {@code null}, neither containing {@code null}
215     * @param <E>       the type of the enumeration
216     * @return a long[] whose values provide a binary representation of the given set of enum values
217     *         with least significant digits rightmost.
218     * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null}
219     * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null}
220     * @since 3.2
221     */
222    public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) {
223        asEnum(enumClass);
224        Validate.noNullElements(values);
225        final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
226        Collections.addAll(condensed, values);
227        final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
228        for (final E value : condensed) {
229            result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE);
230        }
231        ArrayUtils.reverse(result);
232        return result;
233    }
234
235    /**
236     * <p>Convert a long value created by {@link EnumUtils#generateBitVector} into the set of
237     * enum values that it represents.</p>
238     *
239     * <p>If you store this value, beware any changes to the enum that would affect ordinal values.</p>
240     * @param enumClass the class of the enum we are working with, not {@code null}
241     * @param value     the long value representation of a set of enum values
242     * @param <E>       the type of the enumeration
243     * @return a set of enum values
244     * @throws NullPointerException if {@code enumClass} is {@code null}
245     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values
246     * @since 3.0.1
247     */
248    public static <E extends Enum<E>> EnumSet<E> processBitVector(final Class<E> enumClass, final long value) {
249        checkBitVectorable(enumClass).getEnumConstants();
250        return processBitVectors(enumClass, value);
251    }
252
253    /**
254     * <p>Convert a {@code long[]} created by {@link EnumUtils#generateBitVectors} into the set of
255     * enum values that it represents.</p>
256     *
257     * <p>If you store this value, beware any changes to the enum that would affect ordinal values.</p>
258     * @param enumClass the class of the enum we are working with, not {@code null}
259     * @param values     the long[] bearing the representation of a set of enum values, least significant digits rightmost, not {@code null}
260     * @param <E>       the type of the enumeration
261     * @return a set of enum values
262     * @throws NullPointerException if {@code enumClass} is {@code null}
263     * @throws IllegalArgumentException if {@code enumClass} is not an enum class
264     * @since 3.2
265     */
266    public static <E extends Enum<E>> EnumSet<E> processBitVectors(final Class<E> enumClass, final long... values) {
267        final EnumSet<E> results = EnumSet.noneOf(asEnum(enumClass));
268        final long[] lvalues = ArrayUtils.clone(Validate.notNull(values));
269        ArrayUtils.reverse(lvalues);
270        for (final E constant : enumClass.getEnumConstants()) {
271            final int block = constant.ordinal() / Long.SIZE;
272            if (block < lvalues.length && (lvalues[block] & 1L << (constant.ordinal() % Long.SIZE)) != 0) {
273                results.add(constant);
274            }
275        }
276        return results;
277    }
278
279    /**
280     * Validate that {@code enumClass} is compatible with representation in a {@code long}.
281     * @param <E> the type of the enumeration
282     * @param enumClass to check
283     * @return {@code enumClass}
284     * @throws NullPointerException if {@code enumClass} is {@code null}
285     * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values
286     * @since 3.0.1
287     */
288    private static <E extends Enum<E>> Class<E> checkBitVectorable(final Class<E> enumClass) {
289        final E[] constants = asEnum(enumClass).getEnumConstants();
290        Validate.isTrue(constants.length <= Long.SIZE, CANNOT_STORE_S_S_VALUES_IN_S_BITS, 
291            Integer.valueOf(constants.length), enumClass.getSimpleName(), Integer.valueOf(Long.SIZE));
292
293        return enumClass;
294    }
295
296    /**
297     * Validate {@code enumClass}.
298     * @param <E> the type of the enumeration
299     * @param enumClass to check
300     * @return {@code enumClass}
301     * @throws NullPointerException if {@code enumClass} is {@code null}
302     * @throws IllegalArgumentException if {@code enumClass} is not an enum class
303     * @since 3.2
304     */
305    private static <E extends Enum<E>> Class<E> asEnum(final Class<E> enumClass) {
306        Validate.notNull(enumClass, ENUM_CLASS_MUST_BE_DEFINED);
307        Validate.isTrue(enumClass.isEnum(), S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE, enumClass);
308        return enumClass;
309    }
310}