Brooklyn

brooklyn.util.text
[Java] Class Strings

java.lang.Object
  brooklyn.util.text.Strings

public class Strings

Field Summary
static CharMatcher IS_JAVA_IDENTIFIER_PART

A CharMatcher that matches valid Java identifier characters.

static java.lang.String VALID_NON_ALPHANUM_FILE_CHARS

Valid non alphanumeric characters for filenames.

 
Method Summary
static void checkNonEmpty(java.lang.CharSequence s)

throws IllegalArgument if string not empty; cf. guava Preconditions.checkXxxx

static void checkNonEmpty(java.lang.CharSequence s, java.lang.String message)

throws IllegalArgument if string not empty; cf. guava Preconditions.checkXxxx

static java.lang.String[] combineArrays(java.lang.String[]... arrays)

static boolean containsLiteral(java.lang.CharSequence input, java.lang.CharSequence fragment)

static boolean containsLiteralIgnoreCase(java.lang.CharSequence input, java.lang.CharSequence fragment)

static java.lang.String getLastWord(java.lang.String s)

static boolean isBlank(java.lang.CharSequence s)

Checks if the given string is empty or only consists of whitespace.

static boolean isEmpty(java.lang.CharSequence s)

Checks if the given string is null or is an empty string.

static boolean isLowerCase(java.lang.String s)

static boolean isNonBlank(java.lang.CharSequence s)

The inverse of isBlank(CharSequence).

static boolean isNonEmpty(java.lang.CharSequence s)

The inverse of isEmpty(CharSequence).

static java.lang.String join(java.lang.Iterable list, java.lang.String seperator)

@deprecated use com.google.common.base.Joiner

static java.lang.String join(java.lang.Object[] list, java.lang.String seperator)

@deprecated use com.google.common.base.Joiner

static java.lang.String[] makeArray(java.lang.String prefix, int count)

returns e.g. { "prefix01", ..., "prefix96" }; see more functional NumericRangeGlobExpander for "prefix{01-96}"

static java.lang.String makePaddedString(java.lang.String base, int len, java.lang.String left_pad, java.lang.String right_pad)

pads the string with "pad" at the left up to len; no padding if base longer than len

static java.lang.String makeRandomId(int l)

@see Identifiers#makeRandomId(int)#makeRandomId(int)

static java.lang.String makeRealString(double x, int maxlen, int prec, int leftPadLen)

creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part); switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)

static java.lang.String makeRealString(double x, int maxlen, int prec, int leftPadLen, double skipDecimalThreshhold, boolean useEForSmallNumbers)

creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part); switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)

static java.lang.String makeRealStringNearZero(double x, int maxlen, int prec, int leftPadLen)

creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part); switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)

static java.lang.String makeRepeated(char c, int length)

static java.lang.String makeSizeString(long sizeInBytes)

returns a size string using defaults from ByteSizeStrings, e.g. 23.5mb

static java.lang.String makeTimeString(long utcMillis)

static java.lang.String makeValidFilename(java.lang.String s)

Returns a valid filename based on the input.

static java.lang.String makeValidJavaName(java.lang.String s)

Returns a valid Java identifier name based on the input.

static java.lang.String makeValidUniqueJavaName(java.lang.String s)

Returns a unique valid java identifier name based on the input.

static java.lang.String makeZeroPaddedString(int i, int len)

pads the string with 0's at the left up to len; no padding if i longer than len

static java.lang.String maxlen(java.lang.String s, int maxlen)

returns up to maxlen characters from the start of s

static java.lang.String removeAllFromEnd(java.lang.String string, java.lang.String... suffixes)

as removeFromEnd, but repeats until all such suffixes are gone

static java.lang.String removeAllFromStart(java.lang.String string, java.lang.String... prefixes)

as removeFromStart, but repeats until all such suffixes are gone

static java.lang.String removeFromEnd(java.lang.String string, java.lang.String... suffixes)

removes the first suffix in the list which is present at the end of string and returns that string; ignores subsequent suffixes if a matching one is found; returns the original string if no suffixes are at the end

static java.lang.String removeFromStart(java.lang.String string, java.lang.String... prefixes)

removes the first prefix in the list which is present at the start of string and returns that string; ignores subsequent prefixes if a matching one is found; returns the original string if no prefixes match

static java.lang.String replaceAll(java.lang.String source, java.util.Map replacements)

replaces all key->value entries from the replacement map in source (non-regex)

static java.lang.String replaceAll(java.lang.String source, java.lang.String pattern, java.lang.String replacement)

NON-REGEX replaceAll - replaces all instances in source, of the given pattern, with the given replacement (not interpreting any arguments as regular expressions)

static java.lang.String replaceAllNonRegex(java.lang.String source, java.lang.String pattern, java.lang.String replacement)

NON-REGEX replacement -- explicit method name for reabaility, doing same as Strings.replaceAll

static java.lang.String replaceAllRegex(java.lang.String source, java.lang.String pattern, java.lang.String replacement)

REGEX replacement -- explicit method name for reabaility, doing same as String.replaceAll

static java.lang.String reverse(java.lang.String name)

static StringShortener shortener()

returns a configurable shortener

static java.lang.String toInitialCapOnly(java.lang.String value)

static java.lang.String toString(java.lang.Object o)

returns toString of the object if it is not null, otherwise null

static void trimAll(java.lang.String[] s)

static java.lang.String trimEnd(java.lang.String s)

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

IS_JAVA_IDENTIFIER_PART

public static final CharMatcher IS_JAVA_IDENTIFIER_PART
A CharMatcher that matches valid Java identifier characters.
See Also:
java.lang.Character#isJavaIdentifierPart(char)


VALID_NON_ALPHANUM_FILE_CHARS

public static final java.lang.String VALID_NON_ALPHANUM_FILE_CHARS
Valid non alphanumeric characters for filenames.


 
Method Detail

checkNonEmpty

public static void checkNonEmpty(java.lang.CharSequence s)
throws IllegalArgument if string not empty; cf. guava Preconditions.checkXxxx


checkNonEmpty

public static void checkNonEmpty(java.lang.CharSequence s, java.lang.String message)
throws IllegalArgument if string not empty; cf. guava Preconditions.checkXxxx


combineArrays

public static java.lang.String[] combineArrays(java.lang.String[]... arrays)


containsLiteral

public static boolean containsLiteral(java.lang.CharSequence input, java.lang.CharSequence fragment)


containsLiteralIgnoreCase

public static boolean containsLiteralIgnoreCase(java.lang.CharSequence input, java.lang.CharSequence fragment)


getLastWord

public static java.lang.String getLastWord(java.lang.String s)


isBlank

public static boolean isBlank(java.lang.CharSequence s)
Checks if the given string is empty or only consists of whitespace.
Parameters:
s - the String to check
Returns:
true if blank, empty or null, false otherwise.
See Also:
isEmpty(CharSequence)
isNonEmpty(CharSequence)
isNonBlank(CharSequence)


isEmpty

public static boolean isEmpty(java.lang.CharSequence s)
Checks if the given string is null or is an empty string. Useful for pre-String.isEmpty. And useful for StringBuilder etc.
Parameters:
s - the String to check
Returns:
true if empty or null, false otherwise.
See Also:
isNonEmpty(CharSequence)
isBlank(CharSequence)
isNonBlank(CharSequence)


isLowerCase

public static boolean isLowerCase(java.lang.String s)


isNonBlank

public static boolean isNonBlank(java.lang.CharSequence s)
The inverse of isBlank(CharSequence).
Parameters:
s - the String to check
Returns:
true if non blank, false otherwise.
See Also:
isEmpty(CharSequence)
isNonEmpty(CharSequence)
isBlank(CharSequence)


isNonEmpty

public static boolean isNonEmpty(java.lang.CharSequence s)
The inverse of isEmpty(CharSequence).
Parameters:
s - the String to check
Returns:
true if non empty, false otherwise.
See Also:
isEmpty(CharSequence)
isBlank(CharSequence)
isNonBlank(CharSequence)


join

ated public
public static java.lang.String join(java.lang.Iterable list, java.lang.String seperator)
deprecated:
use com.google.common.base.Joiner


join

ated public
public static java.lang.String join(java.lang.Object[] list, java.lang.String seperator)
deprecated:
use com.google.common.base.Joiner


makeArray

public static java.lang.String[] makeArray(java.lang.String prefix, int count)
returns e.g. { "prefix01", ..., "prefix96" }; see more functional NumericRangeGlobExpander for "prefix{01-96}"


makePaddedString

public static java.lang.String makePaddedString(java.lang.String base, int len, java.lang.String left_pad, java.lang.String right_pad)
pads the string with "pad" at the left up to len; no padding if base longer than len


makeRandomId

public static java.lang.String makeRandomId(int l)
See Also:
Identifiers#makeRandomId(int)#makeRandomId(int)#makeRandomId(int)


makeRealString

public static java.lang.String makeRealString(double x, int maxlen, int prec, int leftPadLen)
creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part); switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)
Parameters:
x - number to use
maxlen - maximum length for the numeric string, if possible (-1 to suppress)
prec - number of digits accuracy desired (more kept for integers)
leftPadLen - will add spaces at left if necessary to make string this long (-1 to suppress) [probably not usef]
Returns:
such a string


makeRealString

public static java.lang.String makeRealString(double x, int maxlen, int prec, int leftPadLen, double skipDecimalThreshhold, boolean useEForSmallNumbers)
creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part); switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)
Parameters:
x - number to use
maxlen - maximum length for the numeric string, if possible (-1 to suppress)
prec - number of digits accuracy desired (more kept for integers)
leftPadLen - will add spaces at left if necessary to make string this long (-1 to suppress) [probably not usef]
skipDecimalThreshhold - if positive it will not add a decimal part if the fractional part is less than this threshhold (but for a value 3.00001 it would show zeroes, e.g. with 3 precision and positive threshhold <= 0.00001 it would show 3.00); if zero or negative then decimal digits are always shown
useEForSmallNumbers - whether to use E notation for numbers near zero
Returns:
such a string


makeRealStringNearZero

public static java.lang.String makeRealStringNearZero(double x, int maxlen, int prec, int leftPadLen)
creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part); switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)
Parameters:
x - number to use
maxlen - maximum length for the numeric string, if possible (-1 to suppress)
prec - number of digits accuracy desired (more kept for integers)
leftPadLen - will add spaces at left if necessary to make string this long (-1 to suppress) [probably not usef]
Returns:
such a string


makeRepeated

public static java.lang.String makeRepeated(char c, int length)


makeSizeString

public static java.lang.String makeSizeString(long sizeInBytes)
returns a size string using defaults from ByteSizeStrings, e.g. 23.5mb


makeTimeString

public static java.lang.String makeTimeString(long utcMillis)


makeValidFilename

public static java.lang.String makeValidFilename(java.lang.String s)
Returns a valid filename based on the input. A valid filename starts with the first alphanumeric character, then include all alphanumeric characters plus those in VALID_NON_ALPHANUM_FILE_CHARS, with any runs of invalid characters being replaced by {
literal:
_}.
throws:
NullPointerException if the input string is null.
throws:
IllegalArgumentException if the input string is blank.


makeValidJavaName

public static java.lang.String makeValidJavaName(java.lang.String s)
Returns a valid Java identifier name based on the input. Removes certain characterss (like apostrophe), replaces one or more invalid characterss with {
literal:
_}, and prepends {@literal _} if the first character is only valid as an identifier part (not start).

The result is usually unique to s, though this isn't guaranteed, for example if all characters are invalid. For a unique identifier use makeValidUniqueJavaName(String).

See Also:
makeValidUniqueJavaName(String)


makeValidUniqueJavaName

public static java.lang.String makeValidUniqueJavaName(java.lang.String s)
Returns a unique valid java identifier name based on the input. Translated as per makeValidJavaName(String) but with java.lang.String#hashCode() appended where necessary to guarantee uniqueness.
See Also:
makeValidJavaName(String)


makeZeroPaddedString

public static java.lang.String makeZeroPaddedString(int i, int len)
pads the string with 0's at the left up to len; no padding if i longer than len


maxlen

public static java.lang.String maxlen(java.lang.String s, int maxlen)
returns up to maxlen characters from the start of s


removeAllFromEnd

public static java.lang.String removeAllFromEnd(java.lang.String string, java.lang.String... suffixes)
as removeFromEnd, but repeats until all such suffixes are gone


removeAllFromStart

public static java.lang.String removeAllFromStart(java.lang.String string, java.lang.String... prefixes)
as removeFromStart, but repeats until all such suffixes are gone


removeFromEnd

public static java.lang.String removeFromEnd(java.lang.String string, java.lang.String... suffixes)
removes the first suffix in the list which is present at the end of string and returns that string; ignores subsequent suffixes if a matching one is found; returns the original string if no suffixes are at the end


removeFromStart

public static java.lang.String removeFromStart(java.lang.String string, java.lang.String... prefixes)
removes the first prefix in the list which is present at the start of string and returns that string; ignores subsequent prefixes if a matching one is found; returns the original string if no prefixes match


replaceAll

@SuppressWarnings("rawtypes")
public static java.lang.String replaceAll(java.lang.String source, java.util.Map replacements)
replaces all key->value entries from the replacement map in source (non-regex)


replaceAll

public static java.lang.String replaceAll(java.lang.String source, java.lang.String pattern, java.lang.String replacement)
NON-REGEX replaceAll - replaces all instances in source, of the given pattern, with the given replacement (not interpreting any arguments as regular expressions)


replaceAllNonRegex

public static java.lang.String replaceAllNonRegex(java.lang.String source, java.lang.String pattern, java.lang.String replacement)
NON-REGEX replacement -- explicit method name for reabaility, doing same as Strings.replaceAll


replaceAllRegex

public static java.lang.String replaceAllRegex(java.lang.String source, java.lang.String pattern, java.lang.String replacement)
REGEX replacement -- explicit method name for reabaility, doing same as String.replaceAll


reverse

public static java.lang.String reverse(java.lang.String name)


shortener

public static StringShortener shortener()
returns a configurable shortener


toInitialCapOnly

public static java.lang.String toInitialCapOnly(java.lang.String value)


toString

public static java.lang.String toString(java.lang.Object o)
returns toString of the object if it is not null, otherwise null


trimAll

public static void trimAll(java.lang.String[] s)


trimEnd

public static java.lang.String trimEnd(java.lang.String s)


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.