Class Config

  • Direct Known Subclasses:
    BlobBasedConfig, StoredConfig

    public class Config
    extends java.lang.Object
    Git style .config, .gitconfig, .gitmodules file.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Config.ConfigEnum
      Converts enumeration values into configuration options and vice-versa, allowing to match a config option with an enum value.
      static interface  Config.SectionParser<T>
      Parses a section of the configuration into an application model object.
    • Constructor Summary

      Constructors 
      Constructor Description
      Config()
      Create a configuration with no default fallback.
      Config​(Config defaultConfig)
      Create an empty configuration with a fallback for missing keys.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ListenerHandle addChangeListener​(ConfigChangedListener listener)
      Adds a listener to be notified about changes.
      protected void clear()
      Clear the configuration file
      protected void fireConfigChangedEvent()
      Notifies the listeners
      void fromText​(java.lang.String text)
      Clear this configuration and reset to the contents of the parsed string.
      <T> T get​(Config.SectionParser<T> parser)
      Obtain a handle to a parsed set of configuration values.
      boolean getBoolean​(java.lang.String section, java.lang.String name, boolean defaultValue)
      Get a boolean value from the git config
      boolean getBoolean​(java.lang.String section, java.lang.String subsection, java.lang.String name, boolean defaultValue)
      Get a boolean value from the git config
      <T extends java.lang.Enum<?>>
      T
      getEnum​(java.lang.String section, java.lang.String subsection, java.lang.String name, T defaultValue)
      Parse an enumeration from the configuration.
      <T extends java.lang.Enum<?>>
      T
      getEnum​(T[] all, java.lang.String section, java.lang.String subsection, java.lang.String name, T defaultValue)
      Parse an enumeration from the configuration.
      int getInt​(java.lang.String section, java.lang.String name, int defaultValue)
      Obtain an integer value from the configuration.
      int getInt​(java.lang.String section, java.lang.String subsection, java.lang.String name, int defaultValue)
      Obtain an integer value from the configuration.
      long getLong​(java.lang.String section, java.lang.String name, long defaultValue)
      Obtain an integer value from the configuration.
      long getLong​(java.lang.String section, java.lang.String subsection, java.lang.String name, long defaultValue)
      Obtain an integer value from the configuration.
      java.util.Set<java.lang.String> getNames​(java.lang.String section)  
      java.util.Set<java.lang.String> getNames​(java.lang.String section, boolean recursive)  
      java.util.Set<java.lang.String> getNames​(java.lang.String section, java.lang.String subsection)  
      java.util.Set<java.lang.String> getNames​(java.lang.String section, java.lang.String subsection, boolean recursive)  
      java.util.Set<java.lang.String> getSections()  
      java.lang.String getString​(java.lang.String section, java.lang.String subsection, java.lang.String name)
      Get string value or null if not found.
      java.lang.String[] getStringList​(java.lang.String section, java.lang.String subsection, java.lang.String name)
      Get a list of string values
      java.util.Set<java.lang.String> getSubsections​(java.lang.String section)  
      protected boolean notifyUponTransientChanges()
      Determine whether to issue change events for transient changes.
      void setBoolean​(java.lang.String section, java.lang.String subsection, java.lang.String name, boolean value)
      Add or modify a configuration value.
      <T extends java.lang.Enum<?>>
      void
      setEnum​(java.lang.String section, java.lang.String subsection, java.lang.String name, T value)
      Add or modify a configuration value.
      void setInt​(java.lang.String section, java.lang.String subsection, java.lang.String name, int value)
      Add or modify a configuration value.
      void setLong​(java.lang.String section, java.lang.String subsection, java.lang.String name, long value)
      Add or modify a configuration value.
      void setString​(java.lang.String section, java.lang.String subsection, java.lang.String name, java.lang.String value)
      Add or modify a configuration value.
      void setStringList​(java.lang.String section, java.lang.String subsection, java.lang.String name, java.util.List<java.lang.String> values)
      Set a configuration value.
      java.lang.String toText()  
      void uncache​(Config.SectionParser<?> parser)
      Remove a cached configuration object.
      void unset​(java.lang.String section, java.lang.String subsection, java.lang.String name)
      Remove a configuration value.
      void unsetSection​(java.lang.String section, java.lang.String subsection)
      Remove all configuration values under a single section.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Config

        public Config()
        Create a configuration with no default fallback.
      • Config

        public Config​(Config defaultConfig)
        Create an empty configuration with a fallback for missing keys.
        Parameters:
        defaultConfig - the base configuration to be consulted when a key is missing from this configuration instance.
    • Method Detail

      • getInt

        public int getInt​(java.lang.String section,
                          java.lang.String name,
                          int defaultValue)
        Obtain an integer value from the configuration.
        Parameters:
        section - section the key is grouped within.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        an integer value from the configuration, or defaultValue.
      • getInt

        public int getInt​(java.lang.String section,
                          java.lang.String subsection,
                          java.lang.String name,
                          int defaultValue)
        Obtain an integer value from the configuration.
        Parameters:
        section - section the key is grouped within.
        subsection - subsection name, such a remote or branch name.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        an integer value from the configuration, or defaultValue.
      • getLong

        public long getLong​(java.lang.String section,
                            java.lang.String name,
                            long defaultValue)
        Obtain an integer value from the configuration.
        Parameters:
        section - section the key is grouped within.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        an integer value from the configuration, or defaultValue.
      • getLong

        public long getLong​(java.lang.String section,
                            java.lang.String subsection,
                            java.lang.String name,
                            long defaultValue)
        Obtain an integer value from the configuration.
        Parameters:
        section - section the key is grouped within.
        subsection - subsection name, such a remote or branch name.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        an integer value from the configuration, or defaultValue.
      • getBoolean

        public boolean getBoolean​(java.lang.String section,
                                  java.lang.String name,
                                  boolean defaultValue)
        Get a boolean value from the git config
        Parameters:
        section - section the key is grouped within.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        true if any value or defaultValue is true, false for missing or explicit false
      • getBoolean

        public boolean getBoolean​(java.lang.String section,
                                  java.lang.String subsection,
                                  java.lang.String name,
                                  boolean defaultValue)
        Get a boolean value from the git config
        Parameters:
        section - section the key is grouped within.
        subsection - subsection name, such a remote or branch name.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        true if any value or defaultValue is true, false for missing or explicit false
      • getEnum

        public <T extends java.lang.Enum<?>> T getEnum​(java.lang.String section,
                                                       java.lang.String subsection,
                                                       java.lang.String name,
                                                       T defaultValue)
        Parse an enumeration from the configuration.
        Type Parameters:
        T - type of the enumeration object.
        Parameters:
        section - section the key is grouped within.
        subsection - subsection name, such a remote or branch name.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        the selected enumeration value, or defaultValue.
      • getEnum

        public <T extends java.lang.Enum<?>> T getEnum​(T[] all,
                                                       java.lang.String section,
                                                       java.lang.String subsection,
                                                       java.lang.String name,
                                                       T defaultValue)
        Parse an enumeration from the configuration.
        Type Parameters:
        T - type of the enumeration object.
        Parameters:
        all - all possible values in the enumeration which should be recognized. Typically EnumType.values().
        section - section the key is grouped within.
        subsection - subsection name, such a remote or branch name.
        name - name of the key to get.
        defaultValue - default value to return if no value was present.
        Returns:
        the selected enumeration value, or defaultValue.
      • getString

        public java.lang.String getString​(java.lang.String section,
                                          java.lang.String subsection,
                                          java.lang.String name)
        Get string value or null if not found.
        Parameters:
        section - the section
        subsection - the subsection for the value
        name - the key name
        Returns:
        a String value from the config, null if not found
      • getStringList

        public java.lang.String[] getStringList​(java.lang.String section,
                                                java.lang.String subsection,
                                                java.lang.String name)
        Get a list of string values

        If this instance was created with a base, the base's values are returned first (if any).

        Parameters:
        section - the section
        subsection - the subsection for the value
        name - the key name
        Returns:
        array of zero or more values from the configuration.
      • getSubsections

        public java.util.Set<java.lang.String> getSubsections​(java.lang.String section)
        Parameters:
        section - section to search for.
        Returns:
        set of all subsections of specified section within this configuration and its base configuration; may be empty if no subsection exists. The set's iterator returns sections in the order they are declared by the configuration starting from this instance and progressing through the base.
      • getSections

        public java.util.Set<java.lang.String> getSections()
        Returns:
        the sections defined in this Config. The set's iterator returns sections in the order they are declared by the configuration starting from this instance and progressing through the base.
      • getNames

        public java.util.Set<java.lang.String> getNames​(java.lang.String section)
        Parameters:
        section - the section
        Returns:
        the list of names defined for this section
      • getNames

        public java.util.Set<java.lang.String> getNames​(java.lang.String section,
                                                        java.lang.String subsection)
        Parameters:
        section - the section
        subsection - the subsection
        Returns:
        the list of names defined for this subsection
      • getNames

        public java.util.Set<java.lang.String> getNames​(java.lang.String section,
                                                        boolean recursive)
        Parameters:
        section - the section
        recursive - if true recursively adds the names defined in all base configurations
        Returns:
        the list of names defined for this section
        Since:
        3.2
      • getNames

        public java.util.Set<java.lang.String> getNames​(java.lang.String section,
                                                        java.lang.String subsection,
                                                        boolean recursive)
        Parameters:
        section - the section
        subsection - the subsection
        recursive - if true recursively adds the names defined in all base configurations
        Returns:
        the list of names defined for this subsection
        Since:
        3.2
      • get

        public <T> T get​(Config.SectionParser<T> parser)
        Obtain a handle to a parsed set of configuration values.
        Type Parameters:
        T - type of configuration model to return.
        Parameters:
        parser - parser which can create the model if it is not already available in this configuration file. The parser is also used as the key into a cache and must obey the hashCode and equals contract in order to reuse a parsed model.
        Returns:
        the parsed object instance, which is cached inside this config.
      • uncache

        public void uncache​(Config.SectionParser<?> parser)
        Remove a cached configuration object.

        If the associated configuration object has not yet been cached, this method has no effect.

        Parameters:
        parser - parser used to obtain the configuration object.
        See Also:
        get(SectionParser)
      • addChangeListener

        public ListenerHandle addChangeListener​(ConfigChangedListener listener)
        Adds a listener to be notified about changes.

        Clients are supposed to remove the listeners after they are done with them using the ListenerHandle.remove() method

        Parameters:
        listener - the listener
        Returns:
        the handle to the registered listener
      • notifyUponTransientChanges

        protected boolean notifyUponTransientChanges()
        Determine whether to issue change events for transient changes.

        If true is returned (which is the default behavior), fireConfigChangedEvent() will be called upon each change.

        Subclasses that override this to return false are responsible for issuing fireConfigChangedEvent() calls themselves.

        Returns:
      • fireConfigChangedEvent

        protected void fireConfigChangedEvent()
        Notifies the listeners
      • setInt

        public void setInt​(java.lang.String section,
                           java.lang.String subsection,
                           java.lang.String name,
                           int value)
        Add or modify a configuration value. The parameters will result in a configuration entry like this.
         [section "subsection"]
                 name = value
         
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
        name - parameter name, e.g. "filemode"
        value - parameter value
      • setLong

        public void setLong​(java.lang.String section,
                            java.lang.String subsection,
                            java.lang.String name,
                            long value)
        Add or modify a configuration value. The parameters will result in a configuration entry like this.
         [section "subsection"]
                 name = value
         
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
        name - parameter name, e.g. "filemode"
        value - parameter value
      • setBoolean

        public void setBoolean​(java.lang.String section,
                               java.lang.String subsection,
                               java.lang.String name,
                               boolean value)
        Add or modify a configuration value. The parameters will result in a configuration entry like this.
         [section "subsection"]
                 name = value
         
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
        name - parameter name, e.g. "filemode"
        value - parameter value
      • setEnum

        public <T extends java.lang.Enum<?>> void setEnum​(java.lang.String section,
                                                          java.lang.String subsection,
                                                          java.lang.String name,
                                                          T value)
        Add or modify a configuration value. The parameters will result in a configuration entry like this.
         [section "subsection"]
                 name = value
         
        Type Parameters:
        T - type of the enumeration object.
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
        name - parameter name, e.g. "filemode"
        value - parameter value
      • setString

        public void setString​(java.lang.String section,
                              java.lang.String subsection,
                              java.lang.String name,
                              java.lang.String value)
        Add or modify a configuration value. The parameters will result in a configuration entry like this.
         [section "subsection"]
                 name = value
         
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
        name - parameter name, e.g. "filemode"
        value - parameter value, e.g. "true"
      • unset

        public void unset​(java.lang.String section,
                          java.lang.String subsection,
                          java.lang.String name)
        Remove a configuration value.
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
        name - parameter name, e.g. "filemode"
      • unsetSection

        public void unsetSection​(java.lang.String section,
                                 java.lang.String subsection)
        Remove all configuration values under a single section.
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
      • setStringList

        public void setStringList​(java.lang.String section,
                                  java.lang.String subsection,
                                  java.lang.String name,
                                  java.util.List<java.lang.String> values)
        Set a configuration value.
         [section "subsection"]
                 name = value
         
        Parameters:
        section - section name, e.g "branch"
        subsection - optional subsection value, e.g. a branch name
        name - parameter name, e.g. "filemode"
        values - list of zero or more values for this key.
      • toText

        public java.lang.String toText()
        Returns:
        this configuration, formatted as a Git style text file.
      • fromText

        public void fromText​(java.lang.String text)
                      throws ConfigInvalidException
        Clear this configuration and reset to the contents of the parsed string.
        Parameters:
        text - Git style text file listing configuration properties.
        Throws:
        ConfigInvalidException - the text supplied is not formatted correctly. No changes were made to this.
      • clear

        protected void clear()
        Clear the configuration file