Package megamek.common.util
Class ConditionalStringJoiner
java.lang.Object
megamek.common.util.ConditionalStringJoiner
A utility class for conditionally joining strings with a delimiter.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a ConditionalStringJoiner.ConditionalStringJoiner(int capacity) Deprecated, for removal: This API element is subject to removal in a future version.ConditionalStringJoiner(String delimiter) Deprecated, for removal: This API element is subject to removal in a future version.ConditionalStringJoiner(String delimiter, int capacity) Creates a ConditionalStringJoiner with the specified delimiter and initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionAdds a string to the joiner if the condition is true.Adds a string to the joiner if the condition is true.Adds a string to the joiner.toString()
-
Constructor Details
-
ConditionalStringJoiner
public ConditionalStringJoiner()Creates a ConditionalStringJoiner. -
ConditionalStringJoiner
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, 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
Creates a ConditionalStringJoiner with the specified delimiter and initial capacity.- Parameters:
delimiter- the delimiter to use when joining stringscapacity- the initial capacity of the ConditionalStringJoiner
-
-
Method Details
-
add
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 checkmessageSupplier- the supplier that provides the string to add if the condition is true- Returns:
- this instance for method chaining
-
add
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 checkmessage- the string to add if the condition is true- Returns:
- this instance for method chaining
-
add
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
-