Class SettingManager<T extends ISetting>

  • Type Parameters:
    T - the type of setting that will be managed (e.g. IOSetting).

    public class SettingManager<T extends ISetting>
    extends Object
    Provides dynamic management of settings. This was created with the intention of managing 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();
    
     }
     
    Author:
    John May
    See Also:
    ISetting, IOSetting
    Source code:
    main
    Belongs to CDK module:
    io
    Created on:
    20.03.2012
    • Constructor Detail

      • SettingManager

        public SettingManager()
    • Method Detail

      • add

        public T add​(T setting)
        Add a setting to the manager and return the instance to use. If a 'new' setting is added to the manager which matches the name and class of an previously added 'original' setting, the original setting will be returned. Otherwise the new setting is returned. This allows the add to be used in assignments as follows:
        
        
         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.
        Parameters:
        setting - the setting to add
        Returns:
        usable setting
      • get

        public <S extends T> S get​(String name)
        Access the setting stored for given name. If not setting is found the provided name an 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
         
        Type Parameters:
        S - type that will be return
        Parameters:
        name - name of the setting to retrieve
        Returns:
        instance of the setting for the provided name
        See Also:
        get(String, Class)
      • get

        public <S extends T> S 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.
        
         SettingManager manager = ...;
         manger.add(new BooleanIOSetting("Setting", ...));
         
         if(manager.get("Setting", BooleanIOSetting.class).isSet()){
             // do something
         }
         
         
        Type Parameters:
        S - type that will be return
        Parameters:
        name - name of the setting to retrieve
        c - 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 runtime
        Returns:
        instance of the setting
        See Also:
        get(String)
      • has

        public boolean has​(String name)
        Determines whether the manager currently holds a setting of the provided name.
        Parameters:
        name - name of the setting
        Returns:
        whether the manager currently contains the desired setting
      • getSettings

        public Collection<T> getSettings()
        Access a collection of all settings in the manager.
        Returns:
        collection of managed settings
      • toArray

        public T[] toArray​(T[] c)
        Compatibility method generates an array of ISetting objects. This method wraps a call to 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]);
         
        Parameters:
        c - empty array of type to generate
        Returns:
        new fixed array of the settings managed by the manager
        See Also:
        getSettings()