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<>(); 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<>(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 @SafeVarargs 202 public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) { 203 Validate.noNullElements(values); 204 return generateBitVector(enumClass, Arrays.asList(values)); 205 } 206 207 /** 208 * <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p> 209 * 210 * <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p> 211 * 212 * <p>Use this method if you have more than 64 values in your Enum.</p> 213 * 214 * @param enumClass the class of the enum we are working with, not {@code null} 215 * @param values the values we want to convert, not {@code null}, neither containing {@code null} 216 * @param <E> the type of the enumeration 217 * @return a long[] whose values provide a binary representation of the given set of enum values 218 * with least significant digits rightmost. 219 * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} 220 * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null} 221 * @since 3.2 222 */ 223 @SafeVarargs 224 public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) { 225 asEnum(enumClass); 226 Validate.noNullElements(values); 227 final EnumSet<E> condensed = EnumSet.noneOf(enumClass); 228 Collections.addAll(condensed, values); 229 final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1]; 230 for (final E value : condensed) { 231 result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE); 232 } 233 ArrayUtils.reverse(result); 234 return result; 235 } 236 237 /** 238 * <p>Convert a long value created by {@link EnumUtils#generateBitVector} into the set of 239 * enum values that it represents.</p> 240 * 241 * <p>If you store this value, beware any changes to the enum that would affect ordinal values.</p> 242 * @param enumClass the class of the enum we are working with, not {@code null} 243 * @param value the long value representation of a set of enum values 244 * @param <E> the type of the enumeration 245 * @return a set of enum values 246 * @throws NullPointerException if {@code enumClass} is {@code null} 247 * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values 248 * @since 3.0.1 249 */ 250 public static <E extends Enum<E>> EnumSet<E> processBitVector(final Class<E> enumClass, final long value) { 251 checkBitVectorable(enumClass).getEnumConstants(); 252 return processBitVectors(enumClass, value); 253 } 254 255 /** 256 * <p>Convert a {@code long[]} created by {@link EnumUtils#generateBitVectors} into the set of 257 * enum values that it represents.</p> 258 * 259 * <p>If you store this value, beware any changes to the enum that would affect ordinal values.</p> 260 * @param enumClass the class of the enum we are working with, not {@code null} 261 * @param values the long[] bearing the representation of a set of enum values, least significant digits rightmost, not {@code null} 262 * @param <E> the type of the enumeration 263 * @return a set of enum values 264 * @throws NullPointerException if {@code enumClass} is {@code null} 265 * @throws IllegalArgumentException if {@code enumClass} is not an enum class 266 * @since 3.2 267 */ 268 public static <E extends Enum<E>> EnumSet<E> processBitVectors(final Class<E> enumClass, final long... values) { 269 final EnumSet<E> results = EnumSet.noneOf(asEnum(enumClass)); 270 final long[] lvalues = ArrayUtils.clone(Validate.notNull(values)); 271 ArrayUtils.reverse(lvalues); 272 for (final E constant : enumClass.getEnumConstants()) { 273 final int block = constant.ordinal() / Long.SIZE; 274 if (block < lvalues.length && (lvalues[block] & 1L << (constant.ordinal() % Long.SIZE)) != 0) { 275 results.add(constant); 276 } 277 } 278 return results; 279 } 280 281 /** 282 * Validate that {@code enumClass} is compatible with representation in a {@code long}. 283 * @param <E> the type of the enumeration 284 * @param enumClass to check 285 * @return {@code enumClass} 286 * @throws NullPointerException if {@code enumClass} is {@code null} 287 * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values 288 * @since 3.0.1 289 */ 290 private static <E extends Enum<E>> Class<E> checkBitVectorable(final Class<E> enumClass) { 291 final E[] constants = asEnum(enumClass).getEnumConstants(); 292 Validate.isTrue(constants.length <= Long.SIZE, CANNOT_STORE_S_S_VALUES_IN_S_BITS, 293 Integer.valueOf(constants.length), enumClass.getSimpleName(), Integer.valueOf(Long.SIZE)); 294 295 return enumClass; 296 } 297 298 /** 299 * Validate {@code enumClass}. 300 * @param <E> the type of the enumeration 301 * @param enumClass to check 302 * @return {@code enumClass} 303 * @throws NullPointerException if {@code enumClass} is {@code null} 304 * @throws IllegalArgumentException if {@code enumClass} is not an enum class 305 * @since 3.2 306 */ 307 private static <E extends Enum<E>> Class<E> asEnum(final Class<E> enumClass) { 308 Validate.notNull(enumClass, ENUM_CLASS_MUST_BE_DEFINED); 309 Validate.isTrue(enumClass.isEnum(), S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE, enumClass); 310 return enumClass; 311 } 312}