Class MemoryStyleSource

java.lang.Object
org.jline.style.MemoryStyleSource
All Implemented Interfaces:
StyleSource

public class MemoryStyleSource extends Object implements StyleSource
In-memory implementation of StyleSource.

This class provides a thread-safe implementation of StyleSource that stores style definitions in memory using concurrent hash maps. It is suitable for use in applications that need to dynamically define and modify styles at runtime.

Example usage:

 MemoryStyleSource source = new MemoryStyleSource();
 source.set("messages", "error", "bold,fg:red");
 source.set("messages", "warning", "bold,fg:yellow");
 source.set("links", "url", "fg:blue,underline");

 // Use the source with a StyleResolver
 StyleResolver resolver = new StyleResolver(source, "messages");
 AttributedStyle errorStyle = resolver.resolve(".error");
 
Since:
3.4
See Also:
  • Constructor Details

    • MemoryStyleSource

      public MemoryStyleSource()
  • Method Details

    • get

      @Nullable public String get(String group, String name)
      Description copied from interface: StyleSource
      Returns the style definition for the given style group and name, or null if not found.

      This method retrieves a style definition from the source. Style definitions are strings in the format understood by StyleResolver, such as "bold,fg:red" or "underline,fg:blue".

      Style groups are used to organize styles by category or purpose, such as "error", "warning", or "info" styles within a "messages" group.

      Specified by:
      get in interface StyleSource
      Parameters:
      group - the style group name (must not be null)
      name - the style name within the group (must not be null)
      Returns:
      the style definition string, or null if no style is defined for the given group and name
    • set

      public void set(String group, String name, String style)
      Description copied from interface: StyleSource
      Sets a style definition for the given style group and name.

      This method stores a style definition in the source. Style definitions are strings in the format understood by StyleResolver, such as "bold,fg:red" or "underline,fg:blue".

      If a style with the same group and name already exists, it will be replaced.

      Example:

       source.set("messages", "error", "bold,fg:red");
       source.set("messages", "warning", "bold,fg:yellow");
       source.set("links", "url", "fg:blue,underline");
       
      Specified by:
      set in interface StyleSource
      Parameters:
      group - the style group name (must not be null)
      name - the style name within the group (must not be null)
      style - the style definition string (must not be null)
    • remove

      public void remove(String group)
      Description copied from interface: StyleSource
      Removes all styles for the given style group.

      This method removes all style definitions associated with the specified group. If the group does not exist or has no styles, this method has no effect.

      Specified by:
      remove in interface StyleSource
      Parameters:
      group - the style group name to remove (must not be null)
    • remove

      public void remove(String group, String name)
      Description copied from interface: StyleSource
      Removes a specific style from a style group.

      This method removes the style definition for the specified group and name. If the style does not exist, this method has no effect.

      Specified by:
      remove in interface StyleSource
      Parameters:
      group - the style group name (must not be null)
      name - the style name to remove (must not be null)
    • clear

      public void clear()
      Description copied from interface: StyleSource
      Clears all style definitions from this source.

      This method removes all style groups and their associated styles from the source. After calling this method, the source will be empty.

      Specified by:
      clear in interface StyleSource
    • groups

      public Iterable<String> groups()
      Description copied from interface: StyleSource
      Returns the names of all configured style groups.

      This method returns an iterable of all style group names that have been configured in this source. If no groups have been configured, an empty iterable is returned.

      Specified by:
      groups in interface StyleSource
      Returns:
      an immutable iterable of style group names (never null)
    • styles

      public Map<String,String> styles(String group)
      Description copied from interface: StyleSource
      Returns all configured styles for the given style group.

      This method returns a map of style names to style definitions for the specified group. If the group does not exist or has no styles, an empty map is returned.

      Specified by:
      styles in interface StyleSource
      Parameters:
      group - the style group name (must not be null)
      Returns:
      an immutable map of style names to style definitions (never null)