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

org.yarnandtail.andhow.api.LoaderValues Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package org.yarnandtail.andhow.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The Properties and values loaded by a Loader.
 * 
 * Only Properties for which a Loader found a value will be in the collection.
 * 
 * @author eeverman
 */
public class LoaderValues implements ValueMap {
	public static final List EMPTY_PROP_VALUE_LIST = Collections.emptyList();
	
	private final Loader loader;
	private final List values;
	private final ProblemList problems;
	
	
	/**
	 * A constructor when there is just a problem to report.
	 * 
	 * @param loader
	 * @param problem
	 */
	public LoaderValues(Loader loader, Problem problem) {
		ProblemList probs = new ProblemList();
		probs.add(problem);
		this.problems = new ProblemList.UnmodifiableProblemList(probs);
		
		this.loader = loader;
		values = EMPTY_PROP_VALUE_LIST;
	}
	
	/**
	 * A constructor when there is no problem, but there were no values loaded.
	 * 
	 * @param loader
	 */
	public LoaderValues(Loader loader) {
		this.problems = new ProblemList.UnmodifiableProblemList();
		this.loader = loader;
		values = EMPTY_PROP_VALUE_LIST;
	}
	
	public LoaderValues(Loader loader, List inValues, ProblemList problems) {
		
		ProblemList myProblems = new ProblemList();
		myProblems.addAll(problems);
		
		if (loader == null) {
			throw new RuntimeException("The loader cannot be null");
		}
		
		this.loader = loader;
		
		
		if (inValues != null && inValues.size() > 0) {
			ArrayList newValues = new ArrayList();
			newValues.addAll(inValues);
			newValues.trimToSize();
			values = Collections.unmodifiableList(newValues);
			
			
			//check for value problems
			for (PropertyValue pv : values) {
				myProblems.addAll(pv.getProblems());
			}
			
		} else {
			values = EMPTY_PROP_VALUE_LIST;
		}
		
		this.problems = new ProblemList.UnmodifiableProblemList(myProblems);
	}

	public Loader getLoader() {
		return loader;
	}

	public List getValues() {
		return values;
	}
	
	

	/**
	 * A linear search for the Property in the values loaded by this loader.
	 * 
	 * @param prop
	 * @return 
	 */
	@Override
	public  T getExplicitValue(Property prop) {
		if (prop == null) {
			return null;
		}
		return prop.getValueType().cast(values.stream().filter(pv -> prop.equals(pv.getProperty())).
						findFirst().map(pv -> pv.getValue()).orElse(null)
		);
	}
	
	@Override
	public  T getEffectiveValue(Property prop) {
		if (prop == null) {
			return null;
		}
		
		if (isExplicitlySet(prop)) {
			return getExplicitValue(prop);
		} else {
			return prop.getDefaultValue();
		}
	}

	/**
	 * A linear search for the Property in the values loaded by this loader.
	 * @param prop
	 * @return 
	 */
	@Override
	public boolean isExplicitlySet(Property prop) {
		return values.stream().anyMatch(p -> p.getProperty().equals(prop));
	}
	

	/**
	 * Returns loader and value problems, if any.
	 * @return Never null
	 */
	public ProblemList getProblems() {
		return problems;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy