Skip to content

Instantly share code, notes, and snippets.

@meyerdan
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meyerdan/27607478fd11e2fef457 to your computer and use it in GitHub Desktop.
Save meyerdan/27607478fd11e2fef457 to your computer and use it in GitHub Desktop.
Typesafe property api
public class Property<T> {
protected final String name;
public Property(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
public class PropertyMap<K, V> {
protected final String name;
public PropertyMap(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class PropertyList<T> {
protected final String name;
public PropertyList(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
public class Properties {
protected Map<Object, Object> properties = new HashMap<>();
public <T> T get(Property<T> property) {
return (T) properties.get(property);
}
public <T> List<T> get(PropertyList<T> property) {
return (List<T>) properties.get(property);
}
public <K,V> Map<K,V> get(PropertyMap<K,V> property) {
return (Map<K,V>) properties.get(property);
}
public <T> void addListItem(PropertyList<T> property, T value) {
List<T> list = get(property);
if(list == null) {
list = new ArrayList<>();
set(property, list);
}
list.add(value);
}
public <T> void set(Property<T> property, T value) {
properties.put(property, value);
}
public <T> void set(PropertyList<T> property, List<T> value) {
properties.put(property, value);
}
public <K, V> void putMapEntry(PropertyMap<K,V> property, K key, V value) {
Map<K,V> map = get(property);
if(map == null) {
map = new HashMap<>();
properties.put(property, map);
}
map.put(key, value);
}
public boolean contains(Property<?> property) {
return properties.containsKey(property);
}
public boolean contains(PropertyList<?> property) {
return properties.containsKey(property);
}
public boolean contains(PropertyMap<?,?> property) {
return properties.containsKey(property);
}
}
public class BpmnProperties {
public final static PropertyList<EventDeclaration> EVENT_DECLARATIONS= new PropertyList<>("bpmn.eventDeclarations");
public final static Property<Boolean> IS_FOR_COMPENSATION= new Property<>("bpmn.insForCompensation");
}
Properties properties = new Properties();
List<EventDeclaration> declarations = properties.get(EVENT_DECLARATIONS);
boolean isForCompensation = properties.get(IS_FOR_COMPENSATION);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment