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

org.yarnandtail.andhow.internal.ValueProblem Maven / Gradle / Ivy

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

import org.yarnandtail.andhow.api.*;
import org.yarnandtail.andhow.util.TextUtil;

/**
 * Problems with invalid values, values that cannot be converted to their destination type.
 * 
 * @author eeverman
 */
public abstract class ValueProblem implements Problem {
	
	protected LoaderValueCoord badValueCoord;
	
	public LoaderValueCoord getBadValueCoord() {
		return badValueCoord;
	}
	
	
	@Override
	public String getFullMessage() {
		return getProblemContext() + ": " + getProblemDescription();
	}
	
	@Override
	public String getProblemContext() {
		
		if (badValueCoord != null) {
			
			String loadDesc = UNKNOWN;
			
			if (badValueCoord.getLoader() != null && 
					badValueCoord.getLoader().getSpecificLoadDescription() != null) {
				loadDesc = badValueCoord.getLoader().getSpecificLoadDescription();
			}
			return TextUtil.format("Property {} loaded from {}", 
						badValueCoord.getPropName(), loadDesc);
		} else {
			return UNKNOWN;
		}
	}
	
	public static class InvalidValueProblem extends ValueProblem {
		LoaderValueCoord def;
		Validator validator;
		T value;
		
		public InvalidValueProblem(
				Loader loader, Class group, Property prop, 
				T value, Validator validator) {
			badValueCoord = new LoaderValueCoord(loader, group, prop);
			this.validator = validator;
			this.value = value;
		}
		
		@Override
		public String getProblemDescription() {
			return validator.getInvalidMessage(value);
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy