Skip to content

Instantly share code, notes, and snippets.

@elucent
Created December 19, 2018 21:19
Show Gist options
  • Save elucent/4f3559334677aa55fd3f6f9ec6537db8 to your computer and use it in GitHub Desktop.
Save elucent/4f3559334677aa55fd3f6f9ec6537db8 to your computer and use it in GitHub Desktop.
public class PropertyEntry {
Class<? extends IProperty> propertyClass;
Identifier name;
Map<Integer, Constructor<? extends IProperty>> ctors = new HashMap<>();
public PropertyEntry(Identifier name, Class<? extends IProperty> propertyClass) {
this.propertyClass = propertyClass;
this.name = name;
}
public IProperty generate(CompoundTag tag) {
return generate().read(tag);
}
public IProperty generate(Object... args) {
try {
Class[] classes = new Class[args.length];
for (int i = 0; i < args.length; i ++) classes[i] = args[i].getClass();
int hash = 0;
for (int i = 0; i < classes.length; i ++) hash ^= classes[i].hashCode();
Constructor<? extends IProperty> ctor = ctors.get(hash);
if (ctor == null) {
ctors.put(hash, ctor = propertyClass.getConstructor(classes));
}
return ctor.newInstance(args);
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (NoSuchMethodException e) {
e.printStackTrace();
return null;
} catch (InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
public IProperty generate() {
try {
return propertyClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
public Identifier getName() {
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment