create simple property API

This commit is contained in:
dfsek
2021-07-17 10:22:21 -07:00
parent 646d8970aa
commit 60edef989c
3 changed files with 30 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package com.dfsek.terra.api.properties;
import java.util.HashMap;
import java.util.Map;
public class Context {
private final Map<Class<? extends Properties>, Properties> map = new HashMap<>();
@SuppressWarnings("unchecked")
public <T extends Properties> T get(Class<T> clazz) {
return (T) map.computeIfAbsent(clazz, k -> {
throw new IllegalArgumentException("No properties registered for class " + clazz.getCanonicalName());
});
}
public Context put(Properties properties) {
if(map.containsKey(properties.getClass())) throw new IllegalArgumentException("Property for class " + properties.getClass().getCanonicalName() + " already registered.");
map.put(properties.getClass(), properties);
return this;
}
}

View File

@@ -0,0 +1,4 @@
package com.dfsek.terra.api.properties;
public interface Properties {
}

View File

@@ -0,0 +1,5 @@
package com.dfsek.terra.api.properties;
public interface PropertyHolder {
Context getContext();
}