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

se.hiq.oss.commons.reflection.collection.ClassMap Maven / Gradle / Ivy

The newest version!
package se.hiq.oss.commons.reflection.collection;


import java.util.HashMap;
import java.util.Map;


/**
 * Wraps a map and resolves super classes and / or interfaces of the key.
 * 

* {@code * Map, String> map = new HashMap, String>(); * map.put(java.lang.Object.class, "Object"); * map.put(java.lang.Integer.class, "Integer") * ClassMap classMap = new ClassMap(); * classMap.setMap(map); * String doubleString = classMap.get(java.lang.Double); * assert(doubleString.equals("Object")) * String numberString = classMap.get(java.lang.Number); * assert(numberString.equals("Object")) * String integerString = classMap.get(java.lang.Integer); * assert(numberString.equals("Integer")) * } * * @param Content type of the map * @author rikardwi **/ public class ClassMap { private Map, T> map = new HashMap, T>(); public ClassMap() { } public ClassMap(final Map, T> map) { this.map.putAll(map); } /** * Returns the value of a map entry which key matches the supplied class * or any of its super classes or any of its interfaces. *

* Values are resolved using the most specific classes and interfaces first * and then more general (base classes). * * @param clazz Class to resolve value for by inspecting the class META data. * @return value found for the supplied class or null if no value could be resolved. **/ public T get(final Class clazz) { Class classToInspect = clazz; T object = map.get(classToInspect); if (object == null) { object = getByInterfaces(classToInspect); } while (object == null && classToInspect.getSuperclass() != null) { classToInspect = classToInspect.getSuperclass(); object = map.get(classToInspect); if (object == null) { object = getByInterfaces(classToInspect); } } return object; } /** * Returns the value associated with the any * interface implemented by the supplied class. *

* This method will return the first match only, if * more than one value can be resolved the first is * returned. * * @param clazz Class to inspect interfaces of. * @return The resolved value if found, else null. **/ public T getByInterfaces(final Class clazz) { T object = null; Class[] interfaces = clazz.getInterfaces(); for (Class interfaceClass : interfaces) { object = map.get(interfaceClass); if (object != null) { break; } } return object; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy