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

org.yarnandtail.andhow.name.CaseInsensitiveNaming Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.yarnandtail.andhow.api.*;
import org.yarnandtail.andhow.util.AndHowUtil;
import org.yarnandtail.andhow.api.BasePropertyGroup;

/**
 * Case insensitive naming.
 * 
 * @author eeverman
 */
public class CaseInsensitiveNaming implements NamingStrategy {
	
	List EMPTY_NAMES = Collections.emptyList();

	@Override
	public PropertyNaming buildNames(Property prop, 
			Class parentGroup) throws Exception {

		String canonName = AndHowUtil.getCanonicalName(parentGroup, prop);
		return buildNamesFromCanonical(prop, parentGroup, canonName);

	}
	
	public PropertyNaming buildNamesFromCanonical(Property prop, 
			Class parentGroup, String canonicalName) {
		
		if (canonicalName == null) return null;
		
		EffectiveName canon = new EffectiveName(canonicalName, toEffectiveName(canonicalName), true, true);
		
		List effAliases = EMPTY_NAMES;
		
		if (prop.getRequestedAliases().size() > 0) {
			effAliases = new ArrayList();
			
			List reqAliases = prop.getRequestedAliases();
			
			for (Name a : reqAliases) {
				
				EffectiveName en = new EffectiveName(
						a.getActualName(), toEffectiveName(a.getActualName()),
						a.isIn(), a.isOut());
				
				effAliases.add(en);
			}
		}
		

		PropertyNaming naming = new PropertyNaming(canon, effAliases);
		return naming;
	}
	
	@Override
	public String toEffectiveName(String name) {
		if (name != null) {
			return name.toUpperCase();
		} else {
			return null;
		}
	}
	
	@Override
	public String getNameMatchingDescription() {
		return "When reading property names, matching is done in a case insensitive way, " +
				"so 'Bob' would match 'bOB'.";
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy