-
Notifications
You must be signed in to change notification settings - Fork 0
SettingsProviderExamples
Alexei edited this page Mar 19, 2015
·
3 revisions
settings.properties
numbers=1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0
texts=Hi, there|Hello, Mike|Congratulations, Don
host.list=localhost=on;192.168.1.0=off;8.8.8.8=off
IOptions.java
public interface IOptions{
Set<Integer> getNumbers();
@Delimiter("|")
List<String> getTexts();
@Splitter("=")
@Delimiter(";")
Map<String, Boolean> getHostList();
}
Reading config
IConfig c = Config.use("settings.properties");
IOptions o = c.get(IOptions.class);
o.getNumbers(); // Returns a set of 10 elements: from 0 till 9
o.getTexts(); // Returns a list of 3 elements
o.getHostList(); // Returns a map of 3 pairs: [localhost, true], [192.168.1.0, false], [8.8.8.8, true]
Setting group is a named subset of settings.
connection.properties
update.period=300
target.host=192.168.1.1
target.ssl=yes
target.port=33333
target.backup.host=192.168.2.2
target.backup.ssl=yes
target.backup.port=11111
target.localcopy.host=localhost
ITarget.java
public interface ITarget {
String getHost();
@PropertyName("ssl")
@Default("no")
boolean useSSL();
@Default("8080")
int getPort();
}
IConnectionSettings.java
public interface IConnectionSettings {
int getUpdatePeriod();
@GroupField(ITarget.class)
Map<String, ITarget> getTarget();
}
Reading config
IConfig c = Config.use("connection.properties");
IConnectionSettings cs = c.get(IConnectionSettings .class);
Map<String, ITarget> groups = cs.getTarget(); // Returns a map of 3 settings groups.
groups.get(""); // Default settings group: host = 192.168.1.1, port = 33333, ssl = true
groups.get("backup"); // 'backup' settings group: host = 192.168.2.2, port = 11111, ssl = true
groups.get("localcopy"); // 'localcopy' settings group: host = localhost, port = 8080, ssl = false