org.jpmml.evaluator.KeyValueAggregator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pmml-evaluator Show documentation
Show all versions of pmml-evaluator Show documentation
JPMML class model evaluator
The newest version!
/*
* Copyright (c) 2015 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see .
*/
package org.jpmml.evaluator;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
public class KeyValueAggregator {
private ValueFactory valueFactory = null;
private int capacity = 0;
private Map> map = new LinkedHashMap<>();
protected KeyValueAggregator(ValueFactory valueFactory, int capacity){
this.valueFactory = valueFactory;
this.capacity = capacity;
}
public void init(Collection keys){
if(!this.map.isEmpty()){
throw new IllegalStateException();
}
for(K key : keys){
ensureVector(key);
}
}
public void add(K key, Number value){
Vector values = ensureVector(key);
values.add(value);
}
public void add(K key, Number coefficient, Number factor){
Vector values = ensureVector(key);
if(coefficient.doubleValue() != 1d){
values.add(coefficient, factor);
} else
{
values.add(factor);
}
}
protected Vector get(K key){
return this.map.get(key);
}
public void clear(){
this.map.clear();
}
protected Set keySet(){
return this.map.keySet();
}
protected Collection> values(){
return this.map.values();
}
protected Set>> entrySet(){
return this.map.entrySet();
}
protected Map> asTransformedMap(Function, Value> function){
return Maps.transformValues(this.map, function);
}
public ValueFactory getValueFactory(){
return this.valueFactory;
}
private Vector ensureVector(K key){
Vector values = this.map.get(key);
if(values == null){
values = this.valueFactory.newVector(this.capacity);
this.map.put(key, values);
}
return values;
}
}