Skip to content

Instantly share code, notes, and snippets.

@RobertFischer
Created August 25, 2015 22:34
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 RobertFischer/937d12d0552d332dcd1e to your computer and use it in GitHub Desktop.
Save RobertFischer/937d12d0552d332dcd1e to your computer and use it in GitHub Desktop.
Example of type-safe properties
@CompileStatic
abstract class MagnusProperty<T> {
final String key;
MagnusProperty(String key) {
assert key : "No key provided!"
this.key = key
}
abstract T fromString(String value);
}
class StringMagnusProperty extends MagnusProperty<String> {
StringMagnusProperty(String key) {
super(key);
}
String fromString(String value) {
return value;
}
}
class MagnusProperties {
static final StringMagnusProperty SOME_PROPERTY = new StringMagnusProperty("some.property");
<T> T getProperty(MagnusProperty<T> property) {
String value = /* fetch value from DB using property.key */
return property.fromString(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment