Coverage Report - org.apache.commons.lang3.EnumUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumUtils
100%
67/67
96%
25/26
2,231
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Arrays;
 21  
 import java.util.Collections;
 22  
 import java.util.EnumSet;
 23  
 import java.util.LinkedHashMap;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 
 27  
 /**
 28  
  * <p>Utility library to provide helper methods for Java enums.</p>
 29  
  *
 30  
  * <p>#ThreadSafe#</p>
 31  
  *
 32  
  * @since 3.0
 33  
  */
 34  
 public class EnumUtils {
 35  
 
 36  
     private static final String NULL_ELEMENTS_NOT_PERMITTED = "null elements not permitted";
 37  
     private static final String CANNOT_STORE_S_S_VALUES_IN_S_BITS = "Cannot store %s %s values in %s bits";
 38  
     private static final String S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE = "%s does not seem to be an Enum type";
 39  
     private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined.";
 40  
 
 41  
     /**
 42  
      * This constructor is public to permit tools that require a JavaBean
 43  
      * instance to operate.
 44  
      */
 45  1
     public EnumUtils() {
 46  1
     }
 47  
 
 48  
     /**
 49  
      * <p>Gets the {@code Map} of enums by name.</p>
 50  
      *
 51  
      * <p>This method is useful when you need a map of enums by name.</p>
 52  
      *
 53  
      * @param <E> the type of the enumeration
 54  
      * @param enumClass  the class of the enum to query, not null
 55  
      * @return the modifiable map of enum names to enums, never null
 56  
      */
 57  
     public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
 58  1
         final Map<String, E> map = new LinkedHashMap<String, E>();
 59  4
         for (final E e: enumClass.getEnumConstants()) {
 60  3
             map.put(e.name(), e);
 61  
         }
 62  1
         return map;
 63  
     }
 64  
 
 65  
     /**
 66  
      * <p>Gets the {@code List} of enums.</p>
 67  
      *
 68  
      * <p>This method is useful when you need a list of enums rather than an array.</p>
 69  
      *
 70  
      * @param <E> the type of the enumeration
 71  
      * @param enumClass  the class of the enum to query, not null
 72  
      * @return the modifiable list of enums, never null
 73  
      */
 74  
     public static <E extends Enum<E>> List<E> getEnumList(final Class<E> enumClass) {
 75  1
         return new ArrayList<E>(Arrays.asList(enumClass.getEnumConstants()));
 76  
     }
 77  
 
 78  
     /**
 79  
      * <p>Checks if the specified name is a valid enum for the class.</p>
 80  
      *
 81  
      * <p>This method differs from {@link Enum#valueOf} in that checks if the name is
 82  
      * a valid enum without needing to catch the exception.</p>
 83  
      *
 84  
      * @param <E> the type of the enumeration
 85  
      * @param enumClass  the class of the enum to query, not null
 86  
      * @param enumName   the enum name, null returns false
 87  
      * @return true if the enum name is valid, otherwise false
 88  
      */
 89  
     public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) {
 90  6
         if (enumName == null) {
 91  1
             return false;
 92  
         }
 93  
         try {
 94  5
             Enum.valueOf(enumClass, enumName);
 95  3
             return true;
 96  1
         } catch (final IllegalArgumentException ex) {
 97  1
             return false;
 98  
         }
 99  
     }
 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  6
         if (enumName == null) {
 114  1
             return null;
 115  
         }
 116  
         try {
 117  5
             return Enum.valueOf(enumClass, enumName);
 118  1
         } catch (final IllegalArgumentException ex) {
 119  1
             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  25
         checkBitVectorable(enumClass);
 143  19
         Validate.notNull(values);
 144  18
         long total = 0;
 145  18
         for (final E constant : values) {
 146  30
             Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED);
 147  29
             total |= 1 << constant.ordinal();
 148  29
         }
 149  17
         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  12
         asEnum(enumClass);
 170  9
         Validate.notNull(values);
 171  9
         final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
 172  9
         for (final E constant : values) {
 173  14
             Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED);
 174  13
             condensed.add(constant);
 175  13
         }
 176  8
         final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
 177  8
         for (final E value : condensed) {
 178  12
             result[value.ordinal() / Long.SIZE] |= 1 << (value.ordinal() % Long.SIZE);
 179  12
         }
 180  8
         ArrayUtils.reverse(result);
 181  8
         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  14
         Validate.noNullElements(values);
 203  12
         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  13
         asEnum(enumClass);
 224  11
         Validate.noNullElements(values);
 225  9
         final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
 226  9
         Collections.addAll(condensed, values);
 227  9
         final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
 228  9
         for (final E value : condensed) {
 229  15
             result[value.ordinal() / Long.SIZE] |= 1 << (value.ordinal() % Long.SIZE);
 230  15
         }
 231  9
         ArrayUtils.reverse(result);
 232  9
         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  10
         checkBitVectorable(enumClass).getEnumConstants();
 250  8
         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  33
         final EnumSet<E> results = EnumSet.noneOf(asEnum(enumClass));
 268  32
         final long[] lvalues = ArrayUtils.clone(Validate.notNull(values));
 269  32
         ArrayUtils.reverse(lvalues);
 270  128
         for (final E constant : enumClass.getEnumConstants()) {
 271  96
             final int block = constant.ordinal() / Long.SIZE;
 272  96
             if (block < lvalues.length && (lvalues[block] & 1 << (constant.ordinal() % Long.SIZE)) != 0) {
 273  48
                 results.add(constant);
 274  
             }
 275  
         }
 276  32
         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  35
         final E[] constants = asEnum(enumClass).getEnumConstants();
 290  30
         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  27
         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  93
         Validate.notNull(enumClass, ENUM_CLASS_MUST_BE_DEFINED);
 307  86
         Validate.isTrue(enumClass.isEnum(), S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE, enumClass);
 308  82
         return enumClass;
 309  
     }
 310  
 }