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

org.kasource.commons.reflection.collection.PackageMap Maven / Gradle / Ivy

There is a newer version: 2.1
Show newest version
package org.kasource.commons.reflection.collection;

import java.util.Map;

/**
 * Wraps a map and resolves super packages of the key.
 * 
 * @author rikardwi
 *
 * @param  Content type of the map
 **/
public class PackageMap {

    private Map map;
    
    public PackageMap() {}
    
    public PackageMap(Map map) {
        this.map = map;
    }
    
    
    public T get(Class clazz) {
        return get(clazz.getName());
    }
    
    public T get(String packageOrClassName) {
        if(map == null || map.isEmpty()) {
            return null;
        }
        int index = packageOrClassName.lastIndexOf(".");
        String packageName = packageOrClassName;
        T object = map.get(packageName);
        
        while (object == null && index > 0) {
            packageName = packageName.substring(0, index);
            object = map.get(packageName);
            index = packageName.lastIndexOf(".");
        }
        
        return object;
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy