All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoftware.util.convert.EnumConversions Maven / Gradle / Ivy

The newest version!
package com.cedarsoftware.util.convert;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author John DeRegnaucourt ([email protected])
 *         
* Copyright (c) Cedar Software LLC *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* License *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ final class EnumConversions { private EnumConversions() {} static Map toMap(Object from, Converter converter) { Enum enumInstance = (Enum) from; Map target = new LinkedHashMap<>(); target.put("name", enumInstance.name()); return target; } @SuppressWarnings("unchecked") static > EnumSet toEnumSet(Object from, Class target) { if (!target.isEnum()) { throw new IllegalArgumentException("target type " + target.getName() + " must be an Enum, which instructs the EnumSet type to create."); } Class enumClass = (Class) target; EnumSet enumSet = EnumSet.noneOf(enumClass); if (from instanceof Collection) { processElements((Collection) from, enumSet, enumClass); } else if (from.getClass().isArray()) { processArrayElements(from, enumSet, enumClass); } else { throw new IllegalArgumentException("Source must be a Collection or Array, found: " + from.getClass().getName()); } return enumSet; } private static > void processArrayElements(Object array, EnumSet enumSet, Class enumClass) { int length = Array.getLength(array); T[] enumConstants = null; // Lazy initialization for (int i = 0; i < length; i++) { Object element = Array.get(array, i); if (element != null) { enumConstants = processElement(element, enumSet, enumClass, enumConstants); } } } private static > void processElements(Collection collection, EnumSet enumSet, Class enumClass) { T[] enumConstants = null; // Lazy initialization for (Object element : collection) { if (element != null) { enumConstants = processElement(element, enumSet, enumClass, enumConstants); } } } private static > T[] processElement(Object element, EnumSet enumSet, Class enumClass, T[] enumConstants) { if (enumClass.isInstance(element)) { enumSet.add(enumClass.cast(element)); } else if (element instanceof String) { enumSet.add(Enum.valueOf(enumClass, (String) element)); } else if (element instanceof Number) { // Lazy load enum constants when first numeric value is encountered if (enumConstants == null) { enumConstants = enumClass.getEnumConstants(); } int ordinal = ((Number) element).intValue(); if (ordinal < 0 || ordinal >= enumConstants.length) { throw new IllegalArgumentException( String.format("Invalid ordinal value %d for enum %s. Must be between 0 and %d", ordinal, enumClass.getName(), enumConstants.length - 1)); } enumSet.add(enumConstants[ordinal]); } else { throw new IllegalArgumentException(element.getClass().getName() + " found in source collection/array is not convertible to " + enumClass.getName()); } return enumConstants; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy