Class Strings

java.lang.Object
org.apache.sis.util.Static
org.apache.sis.internal.util.Strings

public final class Strings extends Static
Miscellaneous utilities which should not be put in public API. Most of those methods are for Object.toString() implementations.
Since:
0.3
Version:
1.2
  • Field Details

    • CONTINUATION_MARK

      public static final char CONTINUATION_MARK
      The character to write at the beginning of lines that are continuation of a single log record. This constant is defined here only for a little bit more uniform toString() in SIS.
      See Also:
    • CONTINUATION_END

      public static final char CONTINUATION_END
      The character to write at the beginning of lines that are continuation of a single log record. This constant is defined here only for a little bit more uniform toString() in SIS.
      See Also:
    • CONTINUATION_ITEM

      public static final String CONTINUATION_ITEM
      Characters for a new item in a block illustrated by CONTINUATION_MARK. This constant is defined here only for a little bit more uniform toString() in SIS.
      See Also:
  • Constructor Details

    • Strings

      private Strings()
      Do not allow instantiation of this class.
  • Method Details

    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(String a, String b)
      Returns whether the given strings are equal, ignoring case. This method accepts null arguments.
      Parameters:
      a - first string.
      b - another string to be compared with a.
      Returns:
      whether the given strings are equal, ignoring case.
      See Also:
    • orEmpty

      public static String orEmpty(String text)
      Returns the given text if non-null, or the empty string otherwise.
      Parameters:
      text - text or null.
      Returns:
      given text or empty string (never null).
    • trimOrNull

      public static String trimOrNull(String text)
      Trims the leading and trailing spaces of the given string. If the string is null, empty or contains only spaces, then this method returns null.

      Note that this method strips white spaces too, including no-break spaces. In some cases this is not wanted, for example if the text is a programmatic identifier (maybe the developer really wanted no-break spaces). To preserve no-break spaces, the following can be used instead:

      Parameters:
      text - the text to trim, or null.
      Returns:
      the trimmed text, or null if the given text was null or blank.
      Since:
      1.1
    • appendUnicodeIdentifier

      public static boolean appendUnicodeIdentifier(StringBuilder appendTo, char separator, String text, String accepted, boolean toLowerCase)
      Appends to the given buffer only the characters that are valid for a Unicode identifier. The given separator character is append before the given text only if the buffer is not empty and at least one text character is valid.

      Relationship with gml:id

      This method may be invoked for building gml:id values. Strictly speaking this is not appropriate since the xs:ID type defines valid identifiers as containing only letters, digits, underscores, hyphens, and periods. This differ from Unicode identifier in two ways:
      • Unicode identifiers accept Japanese or Chinese ideograms for instance, which are considered as letters.
      • Unicode identifiers do not accept the '-' and ':' characters. However, this restriction fits well our need, since those characters are typical values for the separator argument.
      • Note that '_' is valid both in xs:ID and Unicode identifier.
      Parameters:
      appendTo - the buffer where to append the valid characters.
      separator - the separator to append before the valid characters, or 0 if none.
      text - the text from which to get the valid character to append in the given buffer.
      accepted - additional characters to accept (e.g. "-."), or an empty string if none.
      toLowerCase - true for converting the characters to lower case.
      Returns:
      true if at least one character has been added to the buffer.
    • toIndexed

      public static String toIndexed(String name, int index)
      Appends "[index]" to the given name. This is used for formatting error messages.
      Parameters:
      name - the variable name to which to append "[index]".
      index - value to write between brackets.
      Returns:
      "name[index]".
    • bracket

      public static String bracket(String name, Object index)
      Formats "name[index]".
      Parameters:
      name - the variable name to which to append "[index]".
      index - value to write between brackets.
      Returns:
      "name[index]".
    • bracket

      public static String bracket(Class<?> type, Object index)
      Formats "classname[index]".
      Parameters:
      type - the type to which to append "[index]".
      index - value to write between brackets.
      Returns:
      "classname[index]".
    • range

      public static String range(Class<?> type, Object lower, Object upper)
      Formats "classname[lower … upper]".
      Parameters:
      type - the type to which to append "[lower … upper]".
      lower - first value to write between brackets.
      upper - second value to write between brackets.
      Returns:
      "classname[lower … upper]".
    • toUpperCase

      public static String toUpperCase(String text, Characters.Filter filter, boolean all)
      Returns a string with the same content than the given string, but in upper case and containing only the filtered characters. If the given string already matches the criterion, then it is returned unchanged without creation of any temporary object.

      This method is useful before call to an Enum.valueOf(String) method, for making the search a little bit more tolerant.

      This method is not in public API because conversion to upper-cases should be locale-dependent.

      Parameters:
      text - the text to filter.
      filter - the filter to apply.
      all - true for making all the string in upper-cases, or false for changing only the first character.
      Returns:
      the filtered text.
    • insertLineInLeftMargin

      public static void insertLineInLeftMargin(StringBuilder buffer, String lineSeparator)
      Inserts a continuation character after each line separator except the last one. The intent is to show that a block of lines are part of the same element. The characters are the same than MonolineFormatter.
      Parameters:
      buffer - the buffer where to insert a continuation character in the left margin.
      lineSeparator - the line separator.
    • singleLine

      public static String singleLine(String delimiter, CharSequence text)
      Concatenates a potentially multi-lines text to a single line. White spaces at the beginning and end of each line are removed.
      Parameters:
      delimiter - the separator to insert between lines.
      text - the multi-lines text to convert to single line, or null.
      Returns:
      the the text on a single line text, or null if none.
    • toString

      public static String toString(Class<?> classe, Object... properties)
      Returns a string representation of an instance of the given class having the given properties. This is a convenience method for implementation of Object.toString() methods that are used mostly for debugging purpose.

      The content is specified by (key=value) pairs. If a value is null, the whole entry is omitted. If a key is null, the value is written without the "key=" part. The latter happens typically when the first value is the object name.

      Parameters:
      classe - the class to format.
      properties - the (key=value) pairs.
      Returns:
      a string representation of an instance of the given class having the given properties.
    • append

      private static void append(Object value, StringBuilder buffer)
      Appends the given value in the given buffer, using quotes if the value is a character sequence.
    • formatTo

      public static void formatTo(Formatter formatter, int flags, int width, int precision, String value)
      Formats the given character sequence to the given formatter. This method takes in account the FormattableFlags.UPPERCASE and FormattableFlags.LEFT_JUSTIFY flags.
      Parameters:
      formatter - the formatter in which to format the value.
      flags - the formatting flags.
      width - minimal number of characters to write, padding with ' ' if necessary.
      precision - number of characters to keep before truncation, or -1 if no limit.
      value - the text to format.