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

eu.unicore.uas.util.PropertyHelper Maven / Gradle / Ivy

The newest version!
package eu.unicore.uas.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Helper for dealing with groups of properties
 * 
 * @author schuller
 */
@SuppressWarnings({"unchecked","rawtypes"})
public class PropertyHelper {

	private final Map properties;

	private final String[] acceptedPatterns;

	private final boolean isRegexp;

	private final Pattern[] patterns;

	/**
	 * filter the given properties using the supplied patterns
	 */
	public PropertyHelper(Map properties, String... patterns) {
		this(properties,false,patterns);
	}

	/**
	 * filter the given properties using the supplied patterns
	 * 
	 * @param properties - the properties
	 * @param isRegexp - whether the patterns denote Java regular expressions
	 * @param patterns - the accepted patterns
	 */
	public PropertyHelper(Map properties, boolean isRegexp, String... patterns) {
		this.properties = properties;
		this.acceptedPatterns = patterns;
		this.isRegexp=isRegexp;
		this.patterns=isRegexp?createPatterns():null;
	}
	
	/**
	 * returns an iterator over the valid keys
	 */
	public Iterator keys() {
		final Iterator backing = properties.keySet().iterator();
		return new Iterator() {
			private String next = null;

			boolean filter(String key) {
				if(isRegexp){
					return filterRegexp(key);
				}
				else return filterPlain(key);
			}

			public boolean hasNext() {
				return getNextMatching()!=null;
			}

			//this is idempotent
			private String getNextMatching() {
				if(next!=null)return next;
				if(!backing.hasNext()){
					next=null;
					return null;
				}
				String res = backing.next();
				if (res == null){
					next=null;
					return null;
				}

				if(!filter(res)){
					//skip
					return getNextMatching();
				}

				next=res;
				return res;
			}

			public String next() {
				String res=getNextMatching();
				next=null;
				return res;
			}

			public void remove() {
				backing.remove();
			}

		};
	}
	
	private Pattern[] createPatterns(){
		Pattern[] ps=new Pattern[acceptedPatterns.length];
		for(int i=0; igetFilteredMap(){
		return getFilteredMap(null);
	}

	/**
	 * gets the properties whose keys match the accepted patterns AND
	 * whose keys contain the supplied string
	 * 
	 * @param containedString
	 * @return a map
	 */
	public MapgetFilteredMap(String containedString){
		Map props=new HashMap();
		Iteratorkeys=keys();
		while(keys.hasNext()){
			String key=keys.next();
			if(containedString==null){
				props.put(key, String.valueOf(properties.get(key)));
				continue;
			}
			if(key.contains(containedString)){
				props.put(key, String.valueOf(properties.get(key)));	
			}
		}
		return props;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy