T
- the type of setting that will be managed (e.g. IOSetting).public class SettingManager<T extends ISetting> extends Object
IOSetting
's for IChemObjectIO
however it could be recycled for other purposes where dynamic settings are required.
Settings are stored in a Map
using the name of the setting as the key. The name is
normalised (lowercase and whitespace removal) to allow 'fuzzy' setting access. This means
that character case differences do not affect the retrieval of objects.
Usage:
// create the manager and add a setting
SettingManager<IOSetting> manager = new SettingManager<IOSetting>();
manager.add(new BooleanIOSetting("Sample", IOSetting.MEDIUM, "This is a sample?", "true"));
// check the setting is present (case insensitive
if(manager.has("sample")) {
// access requiring multiple lines of code
BooleanIOSetting setting = manager.get("sample");
String v1 = setting.getSetting();
// single line access (useful for conditional statements)
String v2 = manager.get("sample", BooleanIOSetting.class).getSetting();
}
Constructor and Description |
---|
SettingManager() |
Modifier and Type | Method and Description |
---|---|
T |
add(T setting)
Add a setting to the manager and return the instance to use.
|
<S extends T> |
get(String name)
Access the setting stored for given name.
|
<S extends T> |
get(String name,
Class<S> c)
Convenience method that allows specification of return ISetting type so that you can nest the call to
access the setting value.
|
Collection<T> |
getSettings()
Access a collection of all settings in the manager.
|
boolean |
has(String name)
Determines whether the manager currently holds a setting of
the provided name.
|
T[] |
toArray(T[] c)
Compatibility method generates an array of ISetting objects.
|
public T add(T setting)
SettingManager manager = new SettingManager();
BooleanIOSetting setting1 = manager.add(new BooleanIOSetting("use.3d", ...));
BooleanIOSetting setting2 = manager.add(new BooleanIOSetting("use.3d", ...));
// setting1 == setting2 and so changing a field in setting1 will also change the field
// in setting2
If the names are not equal or the names are equal but the classes are not the new setting
is added and returned.setting
- the setting to addpublic <S extends T> S get(String name)
InvalidParameterException
will be thrown. The method is generic
to allow simplified access to settings. This however means that if the incorrect
type is provided a ClassCastException
may be thrown.
SettingManager manager = ...;
manger.add(new BooleanIOSetting("name", ...));
BooleanIOSetting setting = manager.get("Name"); // okay
OptionIOSetting setting = manager.get("Name"); // class cast exception
S
- type that will be returnname
- name of the setting to retrieveget(String, Class)
public <S extends T> S get(String name, Class<S> c)
SettingManager manager = ...;
manger.add(new BooleanIOSetting("Setting", ...));
if(manager.get("Setting", BooleanIOSetting.class).isSet()){
// do something
}
S
- type that will be returnname
- name of the setting to retrievec
- the class of the setting (matching generic return type). This is need
as due to type erasure we don't know the class of 'S' at runtimeget(String)
public boolean has(String name)
name
- name of the settingpublic Collection<T> getSettings()
public T[] toArray(T[] c)
Collection.toArray(Object[])
} and so is used the same way.
Note: it is preferable to use the collection's accessor getSettings()
Usage:
IOSetting[] settings = manager.toArray(new IOSetting[0]);
c
- empty array of type to generategetSettings()
Copyright © 2018. All Rights Reserved.