org.diirt.datasource.graphene.CheckedReadFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datasource-graphene Show documentation
Show all versions of datasource-graphene Show documentation
Support for real-time graph rendering with graphene.
/**
* Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
*/
package org.diirt.datasource.graphene;
import java.util.ArrayList;
import java.util.List;
import org.diirt.datasource.ReadFunction;
import org.diirt.vtype.ValueUtil;
/**
*
* @author carcassi
*/
public class CheckedReadFunction implements ReadFunction {
private final Class clazz;
private final Class extends T>[] classes;
private final ReadFunction> arg;
private final String argName;
public CheckedReadFunction(ReadFunction> arg, String argName, Class clazz) {
if (clazz == null) {
throw new NullPointerException("Class can't be null");
}
this.clazz = clazz;
this.classes = null;
this.arg = arg;
this.argName = argName;
}
@SafeVarargs
public CheckedReadFunction(ReadFunction> arg, String argName, Class extends T>... classes) {
if (classes == null) {
throw new NullPointerException("Classes can't be null");
}
this.clazz = null;
this.classes = classes;
this.arg = arg;
this.argName = argName;
}
@Override
public T readValue() {
Object obj = arg.readValue();
if (obj == null) {
return null;
}
if (clazz != null) {
if (clazz.isInstance(obj)) {
return clazz.cast(obj);
} else {
throw new RuntimeException(argName + " must be a " + clazz.getSimpleName() + " (was " + ValueUtil.typeOf(obj).getSimpleName() + ")");
}
} else {
for (Class extends T> aClass : classes) {
if (aClass.isInstance(obj)) {
return aClass.cast(obj);
}
}
StringBuilder sb = new StringBuilder();
List names = new ArrayList<>();
for (Class extends T> aClass : classes) {
names.add(aClass.getSimpleName());
}
throw new RuntimeException(argName + " must be one of " + names.toString() + " (was " + ValueUtil.typeOf(obj).getSimpleName() + ")");
}
}
}