com.ibm.util
Class Utility

java.lang.Object
  |
  +--com.ibm.util.Utility

public final class Utility
extends java.lang.Object


Field Summary
static java.lang.String LINE_SEPARATOR
           
 
Constructor Summary
Utility()
           
 
Method Summary
static java.lang.StringBuffer appendNumber(java.lang.StringBuffer result, int n)
          Append a number to the given StringBuffer in the radix 10 generating at least one digit.
static java.lang.StringBuffer appendNumber(java.lang.StringBuffer result, int n, int radix, int minDigits)
          Append a number to the given StringBuffer in the given radix.
static boolean arrayEquals(double[] source, java.lang.Object target)
          Convenience utility to compare two double[]s Ought to be in System
static boolean arrayEquals(int[] source, java.lang.Object target)
          Convenience utility to compare two int[]s Ought to be in System
static boolean arrayEquals(java.lang.Object[] source, java.lang.Object target)
          Convenience utility to compare two Object[]s.
static boolean arrayEquals(java.lang.Object source, java.lang.Object target)
          Convenience utility to compare two Object[]s Ought to be in System
static boolean arrayRegionMatches(double[] source, int sourceStart, double[] target, int targetStart, int len)
          Convenience utility to compare two arrays of doubles.
static boolean arrayRegionMatches(int[] source, int sourceStart, int[] target, int targetStart, int len)
          Convenience utility to compare two int[]s.
static boolean arrayRegionMatches(java.lang.Object[] source, int sourceStart, java.lang.Object[] target, int targetStart, int len)
          Convenience utility to compare two Object[]s Ought to be in System.
static java.lang.String arrayToRLEString(byte[] a)
          Construct a string representing a byte array.
static java.lang.String arrayToRLEString(char[] a)
          Construct a string representing a char array.
static java.lang.String arrayToRLEString(int[] a)
          Construct a string representing an int array.
static java.lang.String arrayToRLEString(short[] a)
          Construct a string representing a short array.
static java.lang.String escape(java.lang.String s)
          Convert characters outside the range U+0020 to U+007F to Unicode escapes, and convert backslash to a double backslash.
static boolean escapeUnprintable(java.lang.StringBuffer result, int c)
          Escape unprintable characters using uxxxx notation for U+0000 to U+FFFF and Uxxxxxxxx for U+10000 and above.
static java.lang.String formatForSource(java.lang.String s)
          Format a String for representation in a source file.
static java.lang.String formatInput(ReplaceableString input, Transliterator.Position pos)
          For debugging purposes; format the given text in the form aaa{bbb|ccc|ddd}eee, where the {} indicate the context start and limit, and the || indicate the start and limit.
static java.lang.String formatInput(Replaceable input, Transliterator.Position pos)
          Convenience method.
static java.lang.StringBuffer formatInput(java.lang.StringBuffer appendTo, ReplaceableString input, Transliterator.Position pos)
          For debugging purposes; format the given text in the form aaa{bbb|ccc|ddd}eee, where the {} indicate the context start and limit, and the || indicate the start and limit.
static java.lang.StringBuffer formatInput(java.lang.StringBuffer appendTo, Replaceable input, Transliterator.Position pos)
          Convenience method.
static void getChars(java.lang.StringBuffer src, int srcBegin, int srcEnd, char[] dst, int dstBegin)
          Similar to StringBuffer.getChars, version 1.3.
static java.lang.String hex(char ch)
          Convert a char to 4 hex uppercase digits.
static java.lang.StringBuffer hex(char ch, java.lang.StringBuffer output)
          Convert a char to 4 hex uppercase digits.
static java.lang.String hex(int ch, int width)
          Convert a integer to size width (minimum) hex uppercase digits.
static java.lang.StringBuffer hex(int ch, int width, java.lang.StringBuffer output)
          Convert a integer to size width hex uppercase digits.
static java.lang.String hex(java.lang.String s)
          Convert a string to comma-separated groups of 4 hex uppercase digits.
static java.lang.String hex(java.lang.StringBuffer s)
          Convert a string to comma-separated groups of 4 hex uppercase digits.
static java.lang.StringBuffer hex(java.lang.String s, java.lang.StringBuffer result)
          Convert a string to comma-separated groups of 4 hex uppercase digits.
static boolean isUnprintable(int c)
          Return true if the character is NOT printable ASCII.
static int lookup(java.lang.String source, java.lang.String[] target)
          Look up a given string in a string array.
static boolean objectEquals(java.lang.Object source, java.lang.Object target)
          Convenience utility.
static int parseInteger(java.lang.String rule, int[] pos, int limit)
          Parse an integer at pos, either of the form \d+ or of the form 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex, or octal format.
static int parsePattern(java.lang.String rule, int pos, int limit, java.lang.String pattern, int[] parsedInts)
          Parse a pattern string starting at offset pos.
static int quotedIndexOf(java.lang.String text, int start, int limit, java.lang.String setOfChars)
          Returns the index of the first character in a set, ignoring quoted text.
static byte[] RLEStringToByteArray(java.lang.String s)
          Construct an array of bytes from a run-length encoded string.
static char[] RLEStringToCharArray(java.lang.String s)
          Construct an array of shorts from a run-length encoded string.
static int[] RLEStringToIntArray(java.lang.String s)
          Construct an array of ints from a run-length encoded string.
static short[] RLEStringToShortArray(java.lang.String s)
          Construct an array of shorts from a run-length encoded string.
static int skipWhitespace(java.lang.String str, int pos)
          Skip over a sequence of zero or more white space characters at pos.
static void split(java.lang.String s, char divider, java.lang.String[] output)
          Split a string into pieces based on the given divider character
static java.lang.StringBuffer trim(java.lang.StringBuffer b)
          Trim whitespace from ends of a StringBuffer.
static java.lang.String unescape(java.lang.String s)
          Convert all escapes in a given string using unescapeAt().
static int unescapeAt(java.lang.String s, int[] offset16)
          Convert an escape to a 32-bit code point value.
static java.lang.String unescapeLeniently(java.lang.String s)
          Convert all escapes in a given string using unescapeAt().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static java.lang.String LINE_SEPARATOR
Constructor Detail

Utility

public Utility()
Method Detail

arrayEquals

public static final boolean arrayEquals(java.lang.Object[] source,
                                        java.lang.Object target)
Convenience utility to compare two Object[]s. Ought to be in System

arrayEquals

public static final boolean arrayEquals(int[] source,
                                        java.lang.Object target)
Convenience utility to compare two int[]s Ought to be in System

arrayEquals

public static final boolean arrayEquals(double[] source,
                                        java.lang.Object target)
Convenience utility to compare two double[]s Ought to be in System

arrayEquals

public static final boolean arrayEquals(java.lang.Object source,
                                        java.lang.Object target)
Convenience utility to compare two Object[]s Ought to be in System

arrayRegionMatches

public static final boolean arrayRegionMatches(java.lang.Object[] source,
                                               int sourceStart,
                                               java.lang.Object[] target,
                                               int targetStart,
                                               int len)
Convenience utility to compare two Object[]s Ought to be in System.
Parameters:
len - the length to compare. The start indices and start+len must be valid.

arrayRegionMatches

public static final boolean arrayRegionMatches(int[] source,
                                               int sourceStart,
                                               int[] target,
                                               int targetStart,
                                               int len)
Convenience utility to compare two int[]s.
Parameters:
len - the length to compare. The start indices and start+len must be valid. Ought to be in System

arrayRegionMatches

public static final boolean arrayRegionMatches(double[] source,
                                               int sourceStart,
                                               double[] target,
                                               int targetStart,
                                               int len)
Convenience utility to compare two arrays of doubles.
Parameters:
len - the length to compare. The start indices and start+len must be valid. Ought to be in System

objectEquals

public static final boolean objectEquals(java.lang.Object source,
                                         java.lang.Object target)
Convenience utility. Does null checks on objects, then calls equals.

arrayToRLEString

public static final java.lang.String arrayToRLEString(int[] a)
Construct a string representing an int array. Use run-length encoding. A character represents itself, unless it is the ESCAPE character. Then the following notations are possible: ESCAPE ESCAPE ESCAPE literal ESCAPE n c n instances of character c Since an encoded run occupies 3 characters, we only encode runs of 4 or more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. If we encounter a run where n == ESCAPE, we represent this as: c ESCAPE n-1 c The ESCAPE value is chosen so as not to collide with commonly seen values.

arrayToRLEString

public static final java.lang.String arrayToRLEString(short[] a)
Construct a string representing a short array. Use run-length encoding. A character represents itself, unless it is the ESCAPE character. Then the following notations are possible: ESCAPE ESCAPE ESCAPE literal ESCAPE n c n instances of character c Since an encoded run occupies 3 characters, we only encode runs of 4 or more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. If we encounter a run where n == ESCAPE, we represent this as: c ESCAPE n-1 c The ESCAPE value is chosen so as not to collide with commonly seen values.

arrayToRLEString

public static final java.lang.String arrayToRLEString(char[] a)
Construct a string representing a char array. Use run-length encoding. A character represents itself, unless it is the ESCAPE character. Then the following notations are possible: ESCAPE ESCAPE ESCAPE literal ESCAPE n c n instances of character c Since an encoded run occupies 3 characters, we only encode runs of 4 or more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. If we encounter a run where n == ESCAPE, we represent this as: c ESCAPE n-1 c The ESCAPE value is chosen so as not to collide with commonly seen values.

arrayToRLEString

public static final java.lang.String arrayToRLEString(byte[] a)
Construct a string representing a byte array. Use run-length encoding. Two bytes are packed into a single char, with a single extra zero byte at the end if needed. A byte represents itself, unless it is the ESCAPE_BYTE. Then the following notations are possible: ESCAPE_BYTE ESCAPE_BYTE ESCAPE_BYTE literal ESCAPE_BYTE n b n instances of byte b Since an encoded run occupies 3 bytes, we only encode runs of 4 or more bytes. Thus we have n > 0 and n != ESCAPE_BYTE and n <= 0xFF. If we encounter a run where n == ESCAPE_BYTE, we represent this as: b ESCAPE_BYTE n-1 b The ESCAPE_BYTE value is chosen so as not to collide with commonly seen values.

RLEStringToIntArray

public static final int[] RLEStringToIntArray(java.lang.String s)
Construct an array of ints from a run-length encoded string.

RLEStringToShortArray

public static final short[] RLEStringToShortArray(java.lang.String s)
Construct an array of shorts from a run-length encoded string.

RLEStringToCharArray

public static final char[] RLEStringToCharArray(java.lang.String s)
Construct an array of shorts from a run-length encoded string.

RLEStringToByteArray

public static final byte[] RLEStringToByteArray(java.lang.String s)
Construct an array of bytes from a run-length encoded string.

formatForSource

public static final java.lang.String formatForSource(java.lang.String s)
Format a String for representation in a source file. This includes breaking it into lines and escaping characters using octal notation when necessary (control characters and double quotes).

escape

public static final java.lang.String escape(java.lang.String s)
Convert characters outside the range U+0020 to U+007F to Unicode escapes, and convert backslash to a double backslash.

unescapeAt

public static int unescapeAt(java.lang.String s,
                             int[] offset16)
Convert an escape to a 32-bit code point value. We attempt to parallel the icu4c unesacpeAt() function.
Parameters:
offset16 - an array containing offset to the character after the backslash. Upon return offset16[0] will be updated to point after the escape sequence.
Returns:
character value from 0 to 10FFFF, or -1 on error.

unescape

public static java.lang.String unescape(java.lang.String s)
Convert all escapes in a given string using unescapeAt().
Throws:
java.lang.IllegalArgumentException - if an invalid escape is seen.

unescapeLeniently

public static java.lang.String unescapeLeniently(java.lang.String s)
Convert all escapes in a given string using unescapeAt(). Leave invalid escape sequences unchanged.

hex

public static java.lang.String hex(char ch)
Convert a char to 4 hex uppercase digits. E.g., hex('a') => "0041".

hex

public static java.lang.String hex(java.lang.String s)
Convert a string to comma-separated groups of 4 hex uppercase digits. E.g., hex('ab') => "0041,0042".

hex

public static java.lang.String hex(java.lang.StringBuffer s)
Convert a string to comma-separated groups of 4 hex uppercase digits. E.g., hex('ab') => "0041,0042".

hex

public static java.lang.StringBuffer hex(char ch,
                                         java.lang.StringBuffer output)
Convert a char to 4 hex uppercase digits. E.g., hex('a') => "0041". Append the output to the given StringBuffer.

hex

public static java.lang.StringBuffer hex(int ch,
                                         int width,
                                         java.lang.StringBuffer output)
Convert a integer to size width hex uppercase digits. E.g., hex('a', 4, str) => "0041". Append the output to the given StringBuffer. If width is too small to fit, nothing will be appended to output.

hex

public static java.lang.String hex(int ch,
                                   int width)
Convert a integer to size width (minimum) hex uppercase digits. E.g., hex('a', 4, str) => "0041". If the integer requires more than width digits, more will be used.

hex

public static java.lang.StringBuffer hex(java.lang.String s,
                                         java.lang.StringBuffer result)
Convert a string to comma-separated groups of 4 hex uppercase digits. E.g., hex('ab') => "0041,0042". Append the output to the given StringBuffer.

split

public static void split(java.lang.String s,
                         char divider,
                         java.lang.String[] output)
Split a string into pieces based on the given divider character
Parameters:
s - the string to split
divider - the character on which to split. Occurrences of this character are not included in the output
output - an array to receive the substrings between instances of divider. It must be large enough on entry to accomodate all output. Adjacent instances of the divider character will place empty strings into output. Before returning, output is padded out with empty strings.

lookup

public static int lookup(java.lang.String source,
                         java.lang.String[] target)
Look up a given string in a string array. Returns the index at which the first occurrence of the string was found in the array, or -1 if it was not found.
Parameters:
source - the string to search for
target - the array of zero or more strings in which to look for source
Returns:
the index of target at which source first occurs, or -1 if not found

skipWhitespace

public static int skipWhitespace(java.lang.String str,
                                 int pos)
Skip over a sequence of zero or more white space characters at pos. Return the index of the first non-white-space character at or after pos, or str.length(), if there is none.

parsePattern

public static int parsePattern(java.lang.String rule,
                               int pos,
                               int limit,
                               java.lang.String pattern,
                               int[] parsedInts)
Parse a pattern string starting at offset pos. Keywords are matched case-insensitively. Spaces may be skipped and may be optional or required. Integer values may be parsed, and if they are, they will be returned in the given array. If successful, the offset of the next non-space character is returned. On failure, -1 is returned.
Parameters:
pattern - must only contain lowercase characters, which will match their uppercase equivalents as well. A space character matches one or more required spaces. A '~' character matches zero or more optional spaces. A '#' character matches an integer and stores it in parsedInts, which the caller must ensure has enough capacity.
parsedInts - array to receive parsed integers. Caller must ensure that parsedInts.length is >= the number of '#' signs in 'pattern'.
Returns:
the position after the last character parsed, or -1 if the parse failed

parseInteger

public static int parseInteger(java.lang.String rule,
                               int[] pos,
                               int limit)
Parse an integer at pos, either of the form \d+ or of the form 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex, or octal format.
Parameters:
pos - INPUT-OUTPUT parameter. On input, the first character to parse. On output, the character after the last parsed character.

trim

public static java.lang.StringBuffer trim(java.lang.StringBuffer b)
Trim whitespace from ends of a StringBuffer.

appendNumber

public static java.lang.StringBuffer appendNumber(java.lang.StringBuffer result,
                                                  int n)
Append a number to the given StringBuffer in the radix 10 generating at least one digit.

appendNumber

public static java.lang.StringBuffer appendNumber(java.lang.StringBuffer result,
                                                  int n,
                                                  int radix,
                                                  int minDigits)
Append a number to the given StringBuffer in the given radix. Standard digits '0'-'9' are used and letters 'A'-'Z' for radices 11 through 36.
Parameters:
result - the digits of the number are appended here
n - the number to be converted to digits; may be negative. If negative, a '-' is prepended to the digits.
radix - a radix from 2 to 36 inclusive.
minDigits - the minimum number of digits, not including any '-', to produce. Values less than 2 have no effect. One digit is always emitted regardless of this parameter.
Returns:
a reference to result

isUnprintable

public static boolean isUnprintable(int c)
Return true if the character is NOT printable ASCII. The tab, newline and linefeed characters are considered unprintable.

escapeUnprintable

public static boolean escapeUnprintable(java.lang.StringBuffer result,
                                        int c)
Escape unprintable characters using uxxxx notation for U+0000 to U+FFFF and Uxxxxxxxx for U+10000 and above. If the character is printable ASCII, then do nothing and return FALSE. Otherwise, append the escaped notation and return TRUE.

quotedIndexOf

public static int quotedIndexOf(java.lang.String text,
                                int start,
                                int limit,
                                java.lang.String setOfChars)
Returns the index of the first character in a set, ignoring quoted text. For example, in the string "abc'hide'h", the 'h' in "hide" will not be found by a search for "h". Unlike String.indexOf(), this method searches not for a single character, but for any character of the string setOfChars.
Parameters:
text - text to be searched
start - the beginning index, inclusive; 0 <= start <= limit.
limit - the ending index, exclusive; start <= limit <= text.length().
setOfChars - string with one or more distinct characters
Returns:
Offset of the first character in setOfChars found, or -1 if not found.
See Also:
String.indexOf(int)

formatInput

public static java.lang.String formatInput(ReplaceableString input,
                                           Transliterator.Position pos)
For debugging purposes; format the given text in the form aaa{bbb|ccc|ddd}eee, where the {} indicate the context start and limit, and the || indicate the start and limit.

formatInput

public static java.lang.StringBuffer formatInput(java.lang.StringBuffer appendTo,
                                                 ReplaceableString input,
                                                 Transliterator.Position pos)
For debugging purposes; format the given text in the form aaa{bbb|ccc|ddd}eee, where the {} indicate the context start and limit, and the || indicate the start and limit.

formatInput

public static java.lang.String formatInput(Replaceable input,
                                           Transliterator.Position pos)
Convenience method.

formatInput

public static java.lang.StringBuffer formatInput(java.lang.StringBuffer appendTo,
                                                 Replaceable input,
                                                 Transliterator.Position pos)
Convenience method.

getChars

public static void getChars(java.lang.StringBuffer src,
                            int srcBegin,
                            int srcEnd,
                            char[] dst,
                            int dstBegin)
Similar to StringBuffer.getChars, version 1.3. Since JDK 1.2 implements StringBuffer.getChars differently, this method is here to provide consistent results. To be removed after JDK 1.2 ceased to be the reference platform.
Parameters:
src - source string buffer
srcBegin - offset to the start of the src to retrieve from
srcEnd - offset to the end of the src to retrieve from
dst - char array to store the retrieved chars
dstBegin - offset to the start of the destination char array to store the retrieved chars


Copyright (c) 2001 IBM Corporation and others.