se.hiq.oss.commons.reflection.collection.PackageMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-reflection Show documentation
Show all versions of commons-reflection Show documentation
Common utilities and classes for reflection
The newest version!
package se.hiq.oss.commons.reflection.collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
/**
* Wraps a map and resolves super packages of the key.
*
* @param Content type of the map
* @author rikardwi
**/
public class PackageMap {
private Map map = new HashMap();
public PackageMap() {
}
public PackageMap(final Map map) {
this.map.putAll(map);
}
public T get(Class clazz) {
return get(clazz.getPackage().getName());
}
public T get(final String packagePath) {
String packageName = packagePath;
T object = map.get(packageName);
String subPackageName = StringUtils.substringBeforeLast(packageName, ".");
while (object == null && !subPackageName.equals(packageName)) {
packageName = subPackageName;
object = map.get(packageName);
subPackageName = StringUtils.substringBeforeLast(packageName, ".");
}
return object;
}
}