Module bus.setting

Class GroupedSet

All Implemented Interfaces:
Serializable, Cloneable, Map<String,LinkedHashSet<String>>

public class GroupedSet extends HashMap<String,LinkedHashSet<String>>
A class representing a collection of grouped sets, parsed from a configuration file. In the file, groups are separated by square brackets [], and each group contains a set of unique string values. Ungrouped values and values in a [] group are merged. Duplicate group names are also merged.

Example file format:

 [group1]
 aaa
 bbb
 ccc

 [group2]
 aaa
 ccc
 ddd
 
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • GroupedSet

      public GroupedSet(Charset charset)
      Constructs a new, empty GroupedSet with a specified charset. The configuration must be initialized manually by calling init(java.net.URL, java.nio.charset.Charset) or load(java.net.URL).
      Parameters:
      charset - The character set to use.
    • GroupedSet

      public GroupedSet(String pathOnClasspath, Charset charset)
      Constructs a GroupedSet by loading a file from the classpath.
      Parameters:
      pathOnClasspath - The relative path from the root of the classpath.
      charset - The character set to use.
    • GroupedSet

      public GroupedSet(File configFile, Charset charset)
      Constructs a GroupedSet from a File.
      Parameters:
      configFile - The configuration file.
      charset - The character set to use.
    • GroupedSet

      public GroupedSet(String path, Class<?> clazz, Charset charset)
      Constructs a GroupedSet from a path relative to a given class.
      Parameters:
      path - The path relative to the class.
      clazz - The class to which the path is relative.
      charset - The character set to use.
    • GroupedSet

      public GroupedSet(URL url, Charset charset)
      Constructs a GroupedSet from a URL.
      Parameters:
      url - The URL of the configuration file.
      charset - The character set to use.
    • GroupedSet

      public GroupedSet(String pathOnClasspath)
      Constructs a GroupedSet by loading a file from the classpath with UTF-8 encoding.
      Parameters:
      pathOnClasspath - The relative path from the root of the classpath.
  • Method Details

    • init

      public boolean init(URL groupedSetUrl, Charset charset)
      Initializes this GroupedSet by loading from a URL.
      Parameters:
      groupedSetUrl - The URL of the configuration file.
      charset - The character set to use.
      Returns:
      true if loading was successful.
    • load

      public boolean load(URL groupedSetUrl)
      Loads the settings from the specified URL.
      Parameters:
      groupedSetUrl - The URL of the configuration file.
      Returns:
      true if loading was successful, false otherwise.
    • reload

      public void reload()
      Reloads the configuration file from the original URL.
    • load

      public void load(InputStream settingStream) throws IOException
      Loads settings from an InputStream. This method does not close the stream.
      Parameters:
      settingStream - The input stream to read from.
      Throws:
      IOException - if an I/O error occurs.
    • getPath

      public String getPath()
      Returns:
      The path of the loaded settings file.
    • getGroups

      public Set<String> getGroups()
      Returns:
      A set of all group names.
    • getValues

      public LinkedHashSet<String> getValues(String group)
      Gets the set of values for a specific group.
      Parameters:
      group - The group name. If null, the default (ungrouped) group is used.
      Returns:
      The set of values, or null if the group does not exist.
    • contains

      public boolean contains(String group, String value, String... otherValues)
      Checks if a group contains one or more specified values.
      Parameters:
      group - The group name.
      value - The primary value to check for.
      otherValues - Additional values to check for.
      Returns:
      true if the group's set contains the value(s).
    • contains

      public boolean contains(String group, Collection<String> values)
      Checks if a group contains all values from a given collection.
      Parameters:
      group - The group name.
      values - The collection of values to check for.
      Returns:
      true if the group's set contains all the specified values.