Package io.openems.common.utils
Class StringUtils
java.lang.Object
io.openems.common.utils.StringUtils
Provides static helper functions for string manipulation.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Convert the first letter of a string to Upper-Case.static boolean
containsWithNullCheck
(String string, String value) If the given string is null return false, otherwise result ofString.contains(CharSequence)
is returned.static String
definedOrElse
(String original, String alternative) Returns the 'alternative' if 'original' is null or blank.static boolean
matchesFloatPattern
(String string) Checks if the given string matches an Float pattern, i.e.static boolean
matchesIntegerPattern
(String string) Checks if the given string matches an Integer pattern, i.e.static int
matchWildcard
(String source, String pattern) Match two Strings, considering wildcards.static OptionalInt
parseNumberFromName
(String name) Parses the number of an Edge from its name string.static String
Causes this character sequence to be replaced by the reverse of the sequence.static String
toShortString
(com.google.gson.JsonElement j, int length) Shortens aJsonElement
string representation to a given length.static String
toShortString
(String s, int length) Shortens a string to a given length.
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
toShortString
Shortens a string to a given length.Example: converts a string "hello world" to "hello w..."
- Parameters:
s
- the stringlength
- the target string length- Returns:
- the shortened string
-
toShortString
Shortens aJsonElement
string representation to a given length.Example: converts a "{ 'foo': 'bar' }" to "{ 'foo': '..."
- Parameters:
j
- theJsonElement
length
- the target string length- Returns:
- the shortened string
-
capitalizeFirstLetter
Convert the first letter of a string to Upper-Case.Example: converts "hello world" to "Hello world"
- Parameters:
s
- the string- Returns:
- the converted string
-
matchWildcard
Match two Strings, considering wildcards.- if
Object.equals(Object)
is true -> return 0 - if 'pattern' matches 'source' -> return value > 1; bigger values represent a better match
- if both Strings do not match -> return -1
Implementation note: only one wildcard is considered. Either the entire string is "*" or the wildcard is at the beginning or at the end of the pattern String. The the JUnit test for details.
- Parameters:
source
- the String to be evaluatedpattern
- the pattern String, i.e. "meter*"- Returns:
- an integer value representing the degree of matching
- if
-
matchesIntegerPattern
Checks if the given string matches an Integer pattern, i.e. if could be parsed to Integer/Long.- Parameters:
string
- a string- Returns:
- true if it matches Integer
-
matchesFloatPattern
Checks if the given string matches an Float pattern, i.e. if could be parsed to Float/Double.- Parameters:
string
- a string- Returns:
- true if it matches Float
-
reverse
Causes this character sequence to be replaced by the reverse of the sequence.- Parameters:
string
- to be reversed.- Returns:
- reversed String.
-
containsWithNullCheck
If the given string is null return false, otherwise result ofString.contains(CharSequence)
is returned.- Parameters:
string
- the string to checkvalue
- the sequence to search for- Returns:
- true if string is not null and string contains value, otherwise false
-
definedOrElse
Returns the 'alternative' if 'original' is null or blank.- Parameters:
original
- the original value, can be null, empty or filled with white-space onlyalternative
- the alternative value- Returns:
- either the 'defined' value (not null, not empty, not only white-space), alternatively the 'orElse' value
-
parseNumberFromName
Parses the number of an Edge from its name string.e.g. translates "edge0" to "0".
- Parameters:
name
- the edge name- Returns:
- the number or empty optional if there is no number in the name or if the name is null
-