
org.jpmml.model.FieldUtil Maven / Gradle / Ivy
/*
* 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 extends Field> fields){
Map result = nameMap(fields);
return result.keySet();
}
static
public Map nameMap(Collection extends F> 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 extends F> fields, Set names){
return selectAll(fields, names, false);
}
static
public Set selectAll(Collection extends F> 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