Created
August 25, 2015 22:34
-
-
Save RobertFischer/937d12d0552d332dcd1e to your computer and use it in GitHub Desktop.
Example of type-safe properties
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
@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