Last active
August 29, 2015 14:26
-
-
Save meyerdan/27607478fd11e2fef457 to your computer and use it in GitHub Desktop.
Typesafe property api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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