Class Text

java.lang.Object
com.globalmentor.text.Text

public class Text extends Object
Constants and utilities for text.
Author:
Garret Wilson
  • Field Details

  • Constructor Details

    • Text

      public Text()
  • Method Details

    • compare

      public static int compare(String string1, String string2, Collator collator)
      Compares two strings for order in ascending order using the specified collator. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical strings are always considered equal. This method functions exactly as if the two strings were compared using Collator.compare(String, String), except:
      • Identical strings are recognized as such without delegating to the actual Collator.compare(String, String) method.
      • This method allows null arguments, considering a null string to be lower than a non-null string.
      This method matches the semantics of Collator.compare(String, String), except that this method allows null arguments.
      Parameters:
      string1 - The first string to be compared, or null if the string is not available.
      string2 - The second string to be compared, or null if the string is not available.
      collator - The collator used to perform comparisons.
      Returns:
      A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
      Throws:
      NullPointerException - if the given collator is null.
      ClassCastException - if the arguments' types prevent them from being compared.
      See Also:
    • compare

      public static int compare(String string1, String string2, Collator collator, SortOrder sortOrder)
      Compares two strings for order using the specified collator with the specified sort order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical strings are always considered equal. This method functions exactly as if the two strings were compared using Collator.compare(String, String), except:
      • Identical strings are recognized as such without delegating to the actual Collator.compare(String, String) method.
      • This method allows null arguments, considering a null string to be lower than a non-null string.
      Parameters:
      string1 - The first string to be compared, or null if the string is not available.
      string2 - The second string to be compared, or null if the string is not available.
      collator - The collator used to perform comparisons.
      sortOrder - The order in which to perform comparisons.
      Returns:
      A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
      Throws:
      NullPointerException - if the given collator and/or sort order is null.
      ClassCastException - if the arguments' types prevent them from being compared.
      See Also:
    • createControlString

      public static final String createControlString(String string)
      Creates a control string according to ECMA-48, "Control Functions for Coded Character Sets", Section 5.6, "Control strings". A control string begins with the Start of String control character (U+0098) and ends with a String Terminator control character (U+009C). ECMA-48 publication is also approved as ISO/IEC 6429.
      Parameters:
      string - The string from which a control string will be created.
      Returns:
      An ECMA-48 control string with the given string as its content.
      Throws:
      NullPointerException - if the given string is null.
      See Also:
    • isText

      public static boolean isText(MediaType mediaType)
      Determines if the given media type is one representing text in some form.

      Text media types include:

      • text/*
      • application/xml
      • application/*+xml
      • application/xml-external-parsed-entity
      • application/*+xml-external-parsed-entity (not formally defined)
      Parameters:
      mediaType - The media type of a resource, or null for no media type.
      Returns:
      true if the given media type is one of several text media types.
    • recode

      public static String recode(String string, String oldEncoding, String newEncoding) throws UnsupportedEncodingException
      Re-encodes the given string to the new encoding (such as UTF-8), assuming the string was encoded from an array of bytes using the old encoding (e.g. ISO-8859-1).
      Parameters:
      string - The string to recode.
      oldEncoding - The encoding used to create the string originally.
      newEncoding - The new encoding to use when creating the string.
      Returns:
      The a string created from encoding the characters in the specified new encoding.
      Throws:
      UnsupportedEncodingException - Thrown if either the old encoding or the new encoding is not supported.
    • normalizeEol

      public static CharSequence normalizeEol(@Nonnull CharSequence charSequence)
      Normalizes end-of-line sequences in the character sequence to LF. The following sequences are normalized:
      • CR
      • LF
      • CRLF
      Implementation Specification:
      This implementation delegates to normalizeEol(CharSequence, CharSequence), This implementation recognizes an input sequence that require no change, and simply returns that character sequence unchanged.
      Parameters:
      charSequence - The character sequence to normalize.
      Returns:
      A character sequence, which may be mutable, with the ends of lines normalized to LF.
      Throws:
      NullPointerException - if the given character sequence and/or EOL characters is null.
      See Also:
    • normalizeEol

      public static CharSequence normalizeEol(@Nonnull CharSequence charSequence, @Nonnull CharSequence normalEol)
      Normalizes end-of-line sequences in the character sequence to a given sequence. The following sequences are normalized to the provided EOL:
      • CR
      • LF
      • CRLF
      Implementation Specification:
      This implementation recognizes an input sequence that require no change if the normalization EOL sequence is a single character that is not CR, and simply returns that character sequence unchanged.
      Parameters:
      charSequence - The character sequence to normalize.
      normalEol - The end of line characters to which to normalize the ends of lines.
      Returns:
      A character sequence, which may be mutable, with the ends of lines normalized to the given end-of-line character sequence.
      Throws:
      NullPointerException - if the given character sequence and/or EOL characters is null.