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

org.jpmml.model.visitors.Interner Maven / Gradle / Ivy

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

import java.lang.reflect.Field;
import java.util.List;
import java.util.ListIterator;

import org.dmg.pmml.PMMLObject;
import org.dmg.pmml.VisitorAction;
import org.jpmml.model.ReflectionUtil;

abstract
public class Interner extends AbstractVisitor {

	private Class type = null;


	public Interner(Class type){
		setType(type);
	}

	abstract
	public V intern(V value);

	public void internAll(List values){

		for(ListIterator it = values.listIterator(); it.hasNext(); ){
			it.set(intern(it.next()));
		}
	}

	@Override
	public VisitorAction visit(PMMLObject object){
		Class type = getType();

		List fields = ReflectionUtil.getFields(object.getClass());
		for(Field field : fields){
			Object value = ReflectionUtil.getFieldValue(field, object);

			if(type.isInstance(value)){
				V internedValue = intern(type.cast(value));

				ReflectionUtil.setFieldValue(field, object, internedValue);
			}
		}

		return super.visit(object);
	}

	public Class getType(){
		return this.type;
	}

	private void setType(Class type){
		this.type = type;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy