org.jpmml.evaluator.ArrayUtil 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
/*
* Copyright (c) 2018 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.List;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.dmg.pmml.Array;
import org.jpmml.model.InvalidElementException;
import org.jpmml.model.UnsupportedAttributeException;
public class ArrayUtil {
private ArrayUtil(){
}
static
public int getSize(Array array){
Integer n = array.getN();
if(n != null){
return n;
}
List> content = getContent(array);
return content.size();
}
static
public List> getContent(Array array){
return CacheUtil.getValue(array, ArrayUtil.contentCache);
}
static
public List extends Number> asNumberList(Array array){
List> content = getContent(array);
Array.Type type = array.requireType();
switch(type){
case INT:
case REAL:
return (List)content;
case STRING:
throw new InvalidElementException(array);
default:
throw new UnsupportedAttributeException(array, type);
}
}
static
public List> parse(Array array){
List tokens;
Object value = array.getValue();
if(value instanceof String){
String string = (String)value;
Array.Type type = array.requireType();
switch(type){
case INT:
case REAL:
case STRING:
tokens = org.jpmml.model.ArrayUtil.parse(type, string);
break;
default:
throw new UnsupportedAttributeException(array, type);
}
} else
if(value instanceof List){
List> list = (List>)value;
Function