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

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

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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy