Class ConditionalStringJoiner

java.lang.Object
megamek.common.util.ConditionalStringJoiner

public class ConditionalStringJoiner extends Object
A utility class for conditionally joining strings with a delimiter.
  • Constructor Details

    • ConditionalStringJoiner

      public ConditionalStringJoiner()
      Creates a ConditionalStringJoiner.
    • ConditionalStringJoiner

      @Deprecated(since="0.51.0", forRemoval=true) public ConditionalStringJoiner(String delimiter)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Creates a ConditionalStringJoiner with the specified delimiter.
      Parameters:
      delimiter - the delimiter to use when joining strings
    • ConditionalStringJoiner

      @Deprecated(since="0.51.0", forRemoval=true) public ConditionalStringJoiner(int capacity)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Creates a ConditionalStringJoiner with the specified initial capacity.
      Parameters:
      capacity - the initial capacity of the ConditionalStringJoiner
    • ConditionalStringJoiner

      public ConditionalStringJoiner(String delimiter, int capacity)
      Creates a ConditionalStringJoiner with the specified delimiter and initial capacity.
      Parameters:
      delimiter - the delimiter to use when joining strings
      capacity - the initial capacity of the ConditionalStringJoiner
  • Method Details

    • add

      public ConditionalStringJoiner add(boolean condition, Supplier<String> messageSupplier)
      Adds a string to the joiner if the condition is true. Uses a supplier to get the string value, this way it is not evaluated unless the condition is true.
           
           ConditionalStringJoiner conditionalStringJoiner = new ConditionalStringJoiner();
           // This will only add the string if speed > 5
           conditionalStringJoiner.add(speed > 5,
               () -> I18n.getFormattedText("common.speedWarning", calculateDamage())
           );
           // partial string if speed > 5
           //  >> "Risk of taking 75 damage if you fail PSR"
      
           // This will add the string if isCriticalHit is true
           conditionalStringJoiner.add(isCriticalHit, () -> I18n.getText("common.criticalHit"));
           String result = conditionalStringJoiner.toString();
      
           // The result will be a string like this:
           //  >> "Risk of taking 75 damage if you fail PSR, this unit has a critical hit"
           
       
      Parameters:
      condition - the condition to check
      messageSupplier - the supplier that provides the string to add if the condition is true
      Returns:
      this instance for method chaining
    • add

      public ConditionalStringJoiner add(boolean condition, String message)
      Adds a string to the joiner if the condition is true.
           
           ConditionalStringJoiner conditionalStringJoiner = new ConditionalStringJoiner();
           // This will only add the string if speed > 5
           conditionalStringJoiner.add(speed > 5, I18n.getFormattedText("common.speedWarning", calculateDamage()));
           // partial string if speed > 5
           //  >> "Risk of taking 75 damage if you fail PSR"
      
           // This will add the string if isCriticalHit is true
           conditionalStringJoiner.add(isCriticalHit, I18n.getText("common.criticalHit"));
           String result = conditionalStringJoiner.toString();
      
           // The result will be a string like this:
           //  >> "Risk of taking 75 damage if you fail PSR, this unit has a critical hit"
           
       
      Parameters:
      condition - the condition to check
      message - the string to add if the condition is true
      Returns:
      this instance for method chaining
    • add

      public ConditionalStringJoiner add(String message)
      Adds a string to the joiner. No condition is checked.
           
           ConditionalStringJoiner conditionalStringJoiner = new ConditionalStringJoiner();
           conditionalStringJoiner.add(I18n.getFormattedText("common.speedWarning", calculateDamage()));
           //  >> "Risk of taking 75 damage if you fail PSR"
      
           conditionalStringJoiner.add(I18n.getText("common.criticalHit"));
           String result = conditionalStringJoiner.toString();
      
           // The result will be a string like this:
           //  >> "Risk of taking 75 damage if you fail PSR, this unit has a critical hit"
           
       
      Parameters:
      message - the string to add
      Returns:
      this instance for method chaining
    • toString

      public String toString()
      Overrides:
      toString in class Object