一、前言
关于apache的commons-lang3(3.3.2)源码包中的org.apache.commons.lang3.EnumUtils枚举工具类,对枚举值转换为LinkedHashMap的Map<String, E>、枚举值转换为List<E>集合序列、获取枚举值为EnumSet<E>集合等常用的操作,详情参见如下源码说明。
二、源码说明
package org.apache.commons.lang3;@b@@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.Collections;@b@import java.util.EnumSet;@b@import java.util.LinkedHashMap;@b@import java.util.List;@b@import java.util.Map;@b@@b@public class EnumUtils@b@{@b@ private static final String NULL_ELEMENTS_NOT_PERMITTED = "null elements not permitted";@b@ private static final String CANNOT_STORE_S_S_VALUES_IN_S_BITS = "Cannot store %s %s values in %s bits";@b@ private static final String S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE = "%s does not seem to be an Enum type";@b@ private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined.";@b@@b@ public static <E extends Enum<E>> Map<String, E> getEnumMap(Class<E> enumClass)@b@ {@b@ Map map = new LinkedHashMap();@b@ Enum[] arr$ = (Enum[])enumClass.getEnumConstants(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { Enum e = arr$[i$];@b@ map.put(e.name(), e);@b@ }@b@ return map;@b@ }@b@@b@ public static <E extends Enum<E>> List<E> getEnumList(Class<E> enumClass)@b@ {@b@ return new ArrayList(Arrays.asList(enumClass.getEnumConstants()));@b@ }@b@@b@ public static <E extends Enum<E>> boolean isValidEnum(Class<E> enumClass, String enumName)@b@ {@b@ if (enumName == null)@b@ return false;@b@ try@b@ {@b@ Enum.valueOf(enumClass, enumName);@b@ return true; } catch (IllegalArgumentException ex) {@b@ }@b@ return false;@b@ }@b@@b@ public static <E extends Enum<E>> E getEnum(Class<E> enumClass, String enumName)@b@ {@b@ if (enumName == null)@b@ return null;@b@ try@b@ {@b@ return Enum.valueOf(enumClass, enumName); } catch (IllegalArgumentException ex) {@b@ }@b@ return null;@b@ }@b@@b@ public static <E extends Enum<E>> long generateBitVector(Class<E> enumClass, Iterable<? extends E> values)@b@ {@b@ checkBitVectorable(enumClass);@b@ Validate.notNull(values);@b@ long total = 0L;@b@ for (Enum constant : values) {@b@ Validate.isTrue((constant != null) ? 1 : false, "null elements not permitted", new Object[0]);@b@ total |= 1 << constant.ordinal();@b@ }@b@ return total;@b@ }@b@@b@ public static <E extends Enum<E>> long[] generateBitVectors(Class<E> enumClass, Iterable<? extends E> values)@b@ {@b@ asEnum(enumClass);@b@ Validate.notNull(values);@b@ EnumSet condensed = EnumSet.noneOf(enumClass);@b@ for (Enum constant : values) {@b@ Validate.isTrue((constant != null) ? 1 : false, "null elements not permitted", new Object[0]);@b@ condensed.add(constant);@b@ }@b@ long[] result = new long[(((Enum[])enumClass.getEnumConstants()).length - 1) / 64 + 1];@b@ for (Enum value : condensed)@b@ result[(value.ordinal() / 64)] |= 1 << value.ordinal() % 64;@b@@b@ ArrayUtils.reverse(result);@b@ return result;@b@ }@b@@b@ public static <E extends Enum<E>> long generateBitVector(Class<E> enumClass, E[] values)@b@ {@b@ Validate.noNullElements(values);@b@ return generateBitVector(enumClass, Arrays.asList(values));@b@ }@b@@b@ public static <E extends Enum<E>> long[] generateBitVectors(Class<E> enumClass, E[] values)@b@ {@b@ asEnum(enumClass);@b@ Validate.noNullElements(values);@b@ EnumSet condensed = EnumSet.noneOf(enumClass);@b@ Collections.addAll(condensed, values);@b@ long[] result = new long[(((Enum[])enumClass.getEnumConstants()).length - 1) / 64 + 1];@b@ for (Enum value : condensed)@b@ result[(value.ordinal() / 64)] |= 1 << value.ordinal() % 64;@b@@b@ ArrayUtils.reverse(result);@b@ return result;@b@ }@b@@b@ public static <E extends Enum<E>> EnumSet<E> processBitVector(Class<E> enumClass, long value)@b@ {@b@ checkBitVectorable(enumClass).getEnumConstants();@b@ return processBitVectors(enumClass, new long[] { value });@b@ }@b@@b@ public static <E extends Enum<E>> EnumSet<E> processBitVectors(Class<E> enumClass, long[] values)@b@ {@b@ EnumSet results = EnumSet.noneOf(asEnum(enumClass));@b@ long[] lvalues = ArrayUtils.clone((long[])Validate.notNull(values));@b@ ArrayUtils.reverse(lvalues);@b@ Enum[] arr$ = (Enum[])enumClass.getEnumConstants(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { Enum constant = arr$[i$];@b@ int block = constant.ordinal() / 64;@b@ if ((block < lvalues.length) && ((lvalues[block] & 1 << constant.ordinal() % 64) != 0L))@b@ results.add(constant);@b@ }@b@@b@ return results;@b@ }@b@@b@ private static <E extends Enum<E>> Class<E> checkBitVectorable(Class<E> enumClass)@b@ {@b@ Enum[] constants = (Enum[])asEnum(enumClass).getEnumConstants();@b@ Validate.isTrue((constants.length <= 64) ? 1 : false, "Cannot store %s %s values in %s bits", new Object[] { Integer.valueOf(constants.length), enumClass.getSimpleName(), Integer.valueOf(64) });@b@@b@ return enumClass;@b@ }@b@@b@ private static <E extends Enum<E>> Class<E> asEnum(Class<E> enumClass)@b@ {@b@ Validate.notNull(enumClass, "EnumClass must be defined.", new Object[0]);@b@ Validate.isTrue(enumClass.isEnum(), "%s does not seem to be an Enum type", new Object[] { enumClass });@b@ return enumClass;@b@ }@b@}