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

org.jpmml.model.FieldUtil Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
/*
 * Copyright (c) 2015 Villu Ruusmann
 */
package org.jpmml.model;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.dmg.pmml.Field;
import org.dmg.pmml.FieldName;

public class FieldUtil {

	private FieldUtil(){
	}

	static
	public Set nameSet(Collection fields){
		Map result = nameMap(fields);

		return result.keySet();
	}

	static
	public  Map nameMap(Collection fields){
		Map result = new LinkedHashMap<>();

		for(F field : fields){
			FieldName name = field.getName();

			F previousField = result.put(name, field);
			if(previousField != null){
				throw new IllegalArgumentException("Fields " + format(field) + " and " + format(previousField) + " have the same name " + name);
			}
		}

		return result;
	}

	static
	public  Set selectAll(Collection fields, Set names){
		return selectAll(fields, names, false);
	}

	static
	public  Set selectAll(Collection fields, Set names, boolean allowPartialSelection){
		Map result = nameMap(fields);

		if(!allowPartialSelection && !(result.keySet()).containsAll(names)){
			Set unmatchedNames = new LinkedHashSet<>(names);
			unmatchedNames.removeAll(result.keySet());

			throw new IllegalArgumentException("Empty selection for names " + unmatchedNames);
		}

		(result.keySet()).retainAll(names);

		return new LinkedHashSet<>(result.values());
	}

	static
	private String format(Field field){
		return String.valueOf(field);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy