com.artemis.io.DefaultObjectStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of artemis-odb-serializer Show documentation
Show all versions of artemis-odb-serializer Show documentation
Fork of Artemis Entity System Framework.
package com.artemis.io;
import com.artemis.utils.reflect.ClassReflection;
import com.artemis.utils.reflect.Field;
import com.artemis.utils.reflect.ReflectionException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* This class is only public in order to keep GWT happy.
* Nothing to see here (API can change without prior notice)
*/
public class DefaultObjectStore {
private HashMap defaultValues = new HashMap();
private boolean usePrototypes = true;
public boolean hasDefaultValues(Object object) {
if (!usePrototypes)
return false;
TypeData typeData = defaultValues.get(object.getClass());
if (typeData == null) {
typeData = new TypeData(newInstance(object.getClass()));
defaultValues.put(object.getClass(), typeData);
}
return typeData.hasDefaultValues(object);
}
private Object newInstance(Class> type) {
try {
return ClassReflection.newInstance(type);
} catch (ReflectionException e) {
throw new RuntimeException(e);
}
}
public void setUsePrototypes(boolean usePrototypes) {
this.usePrototypes = usePrototypes;
}
static class TypeData {
private final Object object;
private final Class type;
private final Field[] fields;
TypeData(Object object) {
this.object = object;
this.type = object.getClass();
Field[] fields = ClassReflection.getDeclaredFields(type);
List filtered = new ArrayList();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
if (!f.isStatic()) {
f.setAccessible(true);
filtered.add(f);
}
}
this.fields = filtered.toArray(new Field[0]);
}
boolean hasDefaultValues(Object other) {
assert (other.getClass() == type);
try {
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
if (!equals(f.get(object), f.get(other)))
return false;
}
return true;
} catch (ReflectionException e) {
throw new RuntimeException(e);
}
}
private static boolean equals(Object a, Object b) {
return (a != null) ? a.equals(b) : b == null;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy