org.freshvanilla.lang.misc.SafeIntFieldAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.paremus.dosgi.net Show documentation
Show all versions of com.paremus.dosgi.net Show documentation
An implementation of OSGi Remote Service Admin, enhanced to
provide support for OSGi Promises and streaming using OSGi
PushStreams.
The newest version!
package org.freshvanilla.lang.misc;
import java.lang.reflect.Field;
class SafeIntFieldAccessor implements FieldAccessor {
private final Field f;
SafeIntFieldAccessor(Field f) {
this.f = f;
}
public Integer getField(Pojo pojo) {
try {
return (Integer) f.get(pojo);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public boolean getBoolean(Pojo pojo) {
return getField(pojo) != 0;
}
public long getNum(Pojo pojo) {
return getField(pojo);
}
public double getDouble(Pojo pojo) {
return getField(pojo);
}
public void setField(Pojo pojo, Integer object) {
try {
f.set(pojo, object);
} catch (Exception e) {
throw new RuntimeException();
}
}
public void setBoolean(Pojo pojo, boolean flag) {
setField(pojo, (int)(flag ? 1 : 0));
}
public void setNum(Pojo pojo, long value) {
setField(pojo, (int)value);
}
public void setDouble(Pojo pojo, double value) {
setField(pojo, (int)value);
}
}