*/
public class StringUtil
{
- /**
- * Empty String
- */
- public static final String EMPTY_STRING = "";
-
- /**
- * Textual string for <code>null</code>
- */
- public static final String NULL_STRING = "<<null>>";
-
- /**
- * Textual string for empty byte array
- */
- public static final String EMPTY_BYTES_STRING = "<<EMPTY BYTES>>";
-
- /**
- * Line separator string
- */
- public static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
-
- /**
- * TAB string
- */
- public static final String TAB = "\t";
-
- /* Dump Format */
- protected static char CONTROL_CHARS_SHOWER = '.';
-
- /**
- * Hexa decimal conversion map
- */
- protected static final char[] HEXA_CHARS = new char[] {
- '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
- };
- /**
- * The number how many bytes is in int
- */
- protected static final int N_INT_BY_BYTE = 4;
-
- /**
- * The number how many characters is print out in a line
- */
- protected static final int WIDTH_PER_LINE = 16;
-
- /**
- * Character to express for control( Human can't control character and control character break log format )
- */
- protected static char TWO_BYTES_CHARS_SHOWER = '?';
+ /**
+ * Empty String
+ */
+ public static final String EMPTY_STRING = "";
+
+ /**
+ * Textual string for <code>null</code>
+ */
+ public static final String NULL_STRING = "<<null>>";
+
+ /**
+ * Textual string for empty byte array
+ */
+ public static final String EMPTY_BYTES_STRING = "<<EMPTY BYTES>>";
+
+ /**
+ * Line separator string
+ */
+ public static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
+
+ /**
+ * TAB string
+ */
+ public static final String TAB = "\t";
+
+ /* Dump Format */
+ protected static char CONTROL_CHARS_SHOWER = '.';
+
+ /**
+ * Hexa decimal conversion map
+ */
+ protected static final char[] HEXA_CHARS = new char[] {
+ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
+ };
+ /**
+ * The number how many bytes is in int
+ */
+ protected static final int N_INT_BY_BYTE = 4;
+
+ /**
+ * The number how many characters is print out in a line
+ */
+ protected static final int WIDTH_PER_LINE = 16;
+
+ /**
+ * Character to express for control( Human can't control character and control character break log format )
+ */
+ protected static char TWO_BYTES_CHARS_SHOWER = '?';
/**
* Return meaningful value of <code>str</code>
public static
String
trimToNull(
- final String str
+ final String str
)
{
return trimToNull(str, null);
* @see #isEmpty(CharSequence)
*/
public static String trimToNull(String str, String def) {
- final String meaningful = getMeaningful( str );
- return isEmpty( meaningful )?def:meaningful;
+ final String meaningful = getMeaningful( str );
+ return isEmpty( meaningful )?def:meaningful;
}
/**
public static
InputStream
toInputStream(
- final String src
+ final String src
)
{
if ( null == src )
*/
public static
String[] split(
- final String str,
- final String delimiters
+ final String str,
+ final String delimiters
)
{
final List<String> list = new ArrayList<String>();
* @return
*/
public static String getOnlyNumerics(
- final String str
+ final String str
)
{
if ( null == str )
final StringBuilder sb = new StringBuilder();
ArrayUtil.iterate(
- ArrayUtil.convertToWrapper( str.toCharArray() ),
- new IteratingRunner<Character>()
- {
- public void run( Character arg )
- {
- if (Character.isDigit( arg )) {
- sb.append( arg );
- }
- }
- }
+ ArrayUtil.convertToWrapper( str.toCharArray() ),
+ new IteratingRunner<Character>()
+ {
+ public void run( Character arg )
+ {
+ if (Character.isDigit( arg )) {
+ sb.append( arg );
+ }
+ }
+ }
);
return sb.toString();
return sb.toString();
}
public static String asString(InputStream is) throws IOException{
- StringBuffer out = new StringBuffer();
- byte[] b = new byte[4096];
-
- for (int n; (n = is.read(b)) != -1;)
- out.append(new String(b,0,n));
- return out.toString();
+ StringBuffer out = new StringBuffer();
+ byte[] b = new byte[4096];
+
+ for (int n; (n = is.read(b)) != -1;)
+ out.append(new String(b,0,n));
+ return out.toString();
}
public static String removeEnd(String str, String remove) {
}
/* Logging */
- /**
- * </p>
- * Append hexa value for <code>ch</code> to <code>buffer</code>
- *
- * Hexa value must be two digit. Pad 0 if hexa value has 1 digit.
- * </p>
- *
- * @param buffer {@link StringBuilder} to write
- * @param ch decimal integer
- */
- public static
- void
- appendHexa(
- final StringBuilder buffer,
- final int ch
- )
- {
- if ( ch < 16 )
- {
- // if 1 digit containing hexadecimal
- buffer.append( '0' );
- buffer.append( HEXA_CHARS[( 0x0f & ( ch ) )] );
- }
- else
- {
- // if 2 digit containing hexadecimal
- buffer.append( HEXA_CHARS[( 0x0f & ( ch >> 4 ) )] );
- buffer.append( HEXA_CHARS[( 0x0f & ( ch ) )] );
- }
- }
-
- /**
- * Add end seperation at logging line's end
- *
- * @param hexPart hex decimal part
- * @param textPart text part
- * @param ret all part
- */
- protected static
- void
- lineEnd(
- final StringBuilder hexPart,
- final StringBuilder textPart,
- final StringBuilder ret
- )
- {
- // append separator of area
- hexPart.append( " |" );
-
- // append separator of termination
- textPart.append( "|\n" );
-
- // concatencate two area
- ret.append( hexPart );
- ret.append( textPart );
-
- // clean each area
- hexPart.delete( 0, hexPart.capacity() );
- textPart.delete( 0, textPart.capacity() );
- }
-
- /**
- * <p>
- * Convert <code>data</code> in hexadecimal format for logging or readibility
- *
- * Output layout<br>
- * <table>
- * <tr><td>Address( or offset )</td><td>byte character</td><td>hexadecimal</td></tr>
- * </table>
- *
- * Replace control character to '.'<br>
- * </p>
- *
- * <code>
- * Logger logger = ...
- * byte[] dump = ...
- * logger.debug( StringUtil.text2hexa( dump ) );
- * </code>
- *
- * @param data byte array to convert
- *
- * @return converted textual string
- *
- * @see #text2hexa(byte[], int, int)
- */
- public static
- String
- text2hexa(
- final byte[] data
- )
- {
- if ( null == data )
- {
- return NULL_STRING;
- }
- return text2hexa( data, 0, data.length );
- }
-
- /**
- * <p>
- * Convert data from <code>offset</code> to <code>offset</code> + <code>length</code> in hexadecimal format.
- *
- * Output layout<br>
- * <table>
- * <tr><td>Address( or offset )</td><td>byte character</td><td>hexadecimal</td></tr>
- * </table>
- *
- * Replace control character to '.'<br>
- * </p>
- *
- * @param data byte array to convert
- * @param offset start position to convert
- * @param length length to convert
- *
- * @return converted textual string
- */
- public static
- String
- text2hexa(
- final byte[] data,
- final int offset,
- final int length
- )
- {
-
- if ( null == data )
- {
- return NULL_STRING;
- }
- if ( data.length <= 0 )
- {
- return EMPTY_BYTES_STRING;
- }
-
- final ByteArrayInputStream reader = new ByteArrayInputStream( data, offset, length );
- final StringBuilder ret = new StringBuilder();
- final StringBuilder hexPart = new StringBuilder();
- final StringBuilder textPart = new StringBuilder();
-
- int address = 0;
- int ch = -1;
- int printByte = 0;
- int cnt = 0;
-
- // fill white space( ' ' ) in address title
- hexPart.append( " " );
-
- // make horizontal ruler
- for ( int i = 0, n = WIDTH_PER_LINE / 4 ; i < n ; i++ )
- {
- hexPart.append( "+-------" );
- textPart.append( "+---" );
- }
-
- lineEnd( hexPart, textPart, ret );
-
- while ( 0 <= ( ch = reader.read() ) )
- {
- if ( 0 == cnt )
- {
- // calculate and print out start address
- for ( int i = N_INT_BY_BYTE - 1 ; i >= 0 ; i-- )
- {
- printByte = 0xFF & ( address >> ( 8 * i ) );
- appendHexa( hexPart, printByte );
- }
- hexPart.append( " " );
- address += WIDTH_PER_LINE;
- }
-
- appendHexa( hexPart, ch );
- if ( ( ch & 0x80 ) != 0 || ch < 32 )
- { // if ch is control character
- // print out replaced character
- textPart.append( CONTROL_CHARS_SHOWER );
- }
- else
- {
- textPart.append( (char) ch );
- }
- cnt++;
-
- if ( WIDTH_PER_LINE == cnt )
- {
- lineEnd( hexPart, textPart, ret );
- cnt = 0;
- }
- } // END of while ( 0 <= (ch = reader.read() ) )
-
- // fill white space( ' ' ) in remaining area
- if ( 0 != cnt )
- {
- for ( ; cnt < WIDTH_PER_LINE ; ++cnt )
- {
- hexPart.append( " " );
- textPart.append( ' ' );
- }
- lineEnd( hexPart, textPart, ret );
- }
-
- return ret.toString();
- }
-
-
- /* Check & Verification */
- /**
- * Check that <code>str</code> is <code>null</code> or empty string textually
- *
- * @param str string to check
- *
- * @return <code>true</code> if <code>str</code> is null or empty string or string containing only white space
- *
- * @see StringUtil#hasText( CharSequence )
- */
- public static
- boolean
- isEmpty(
- final CharSequence str
- )
- {
- if ( null == str )
- {
- return true;
- }
-
- for ( int i=0, n=str.length() ; i<n ; ++i )
- {
- if ( Character.isWhitespace( str.charAt( i ) ) )
- {
- continue;
- }
- return false;
- }
- return true;
- }
-
- /**
- * Get length of string
- *
- * Return <code>0</code> if <code>str</code> is <code>null</code>
- *
- * @param str string whose length is checked
- *
- * @return length of <code>str</code>
- */
- public static
- int
- size(
- final CharSequence str
- )
- {
- if ( null == str )
- {
- return 0;
- }
- return str.length();
- }
-
- /**
- * <p>
- * Check that <code>str</code> contain any character
- * </p>
- * @param str string to check
- *
- * @return <code>true</code> if <code>str</code> is not <code>null</code> nor empty string
- *
- * @see #size(CharSequence)
- */
- public static
- boolean
- hasLength(
- final CharSequence str
- )
- {
- return 0 < size( str );
- }
-
- /**
- * Check that <code>str</code> has textual character
- *
- * @param str string to check
- *
- * @return <code>true</code> if <code>str</code> has textual character
- *
- * @see isEmpty( CharSequence )
- */
- public static
- boolean
- hasText(
- final CharSequence str
- )
- {
- return !isEmpty( str );
- }
-
- /* Conversion */
- /**
- * Check <code>strs</code> and return non-null string
- *
- * Return empty string if all of element in <code>strs</code>
- *
- * @param strs strings to check
- *
- * @return non-null in <code>strs</code>
- *
- * see #ObjectUtil{@link #nvl(String...)}
- */
- public static
- String
- nvl(
- final String... strs
- )
- {
- String val = ObjectUtil.nvl( strs );
- if ( null == val )
- {
- return "";
- }
- return val;
- }
-
-
- /**
- * Trim leading whitespaqce
- *
- * @param str string to trim
- *
- * @return trimmed string
- */
- public static
- String
- trimLeading(
- final String str
- )
- {
- if( !hasLength( str ) )
- {
- return str;
- }
-
- final char[] chs = str.toCharArray();
- for ( int i=0, n=chs.length ; i<n ; ++i )
- {
- if ( !Character.isWhitespace( chs[i] ) )
- {
- return new String( chs, i, str.length() - i );
- }
- }
- return EMPTY_STRING;
- }
-
- /**
- * Trim trailing white space
- *
- * @param str string to trim
- *
- * @return trimmed string
- */
- public static
- String
- trimTrailing(
- final String str
- )
- {
- if( !hasLength( str ) )
- {
- return str;
- }
-
- final char[] chs = str.toCharArray();
- for ( int i=chs.length-1, j=chs.length ; 0<j ; --i, --j )
- {
- if ( !Character.isWhitespace( chs[i] ) )
- {
- return new String( chs, 0, j );
- }
- }
- return EMPTY_STRING;
- }
- /**
- * Remove white spaces at leading and trailing part of <code>str</code>
- *
- * Return empty string if <code>str</code> is <code>null</code>
- *
- * @param str string to convert
- *
- * @return converted string
- *
- * @see StringUtil#trimLeading(String)
- * @see StringUtil#trimTrailing(String)
- */
- public static
- String
- trim(
- final String str
- ) {
- return trimTrailing( trimLeading( str ) );
- }
-
- /**
- * Convert <code>str</code> to meaningful for search
- *
- * @param str string to convert
- *
- * @return converted string
- */
- public static
- String
- getMeaningful(
- final String str
- ) {
- if ( null == str )
- {
- return EMPTY_STRING;
- }
- return trim( str ).toLowerCase();
- }
-
- /**
- * Mask <code>str</code> with <code>maskingStr</code>
- *
- * Replace <code>str</code> to <code>maskingStr</code> with <code>str</code>'s length
- *
- * @param str string to mask
- * @param maskingStr string to mask with
- *
- * @return masked string
- */
- public static
- String
- mask(
- final String str,
- final String maskingStr
- ) {
- if ( null == str )
- {
- return "null";
- }
-
- final StringBuilder buffer = new StringBuilder();
- for ( int i=0, n=str.length() ; i<n ; ++i )
- {
- buffer.append( maskingStr );
- }
- return buffer.toString();
- }
-
- /* Analysis & Extract */
- /**
- * Extract <code>targetIndex</code> string from strings
- *
- * where <code>str</code> is splitted by <code>delimeter</code>
- *
- * @param str string with <code>delimeter</code>
- * @param delimeter what split <code>str</code> with
- * @param escaper escape character for <code>delimter</code>
- * @param targetIndex index from fragments
- *
- * @return extracted string
- */
- public static
- String
- getParamater(
- final String str,
- final char delimeter,
- final int escaper,
- final int targetIndex
- )
- throws IOException
- {
- try
- {
- if ( null == str )
- {
- return null;
- }
- final StringReader reader = new StringReader( str );
- StringWriter writer = null;
- int ch = 0;
- final int ST_NORMAL = 0;
- final int ST_ESCAPE = 1;
- int status = ST_NORMAL;
- int index = 0;
- if ( 0 == targetIndex ) writer = new StringWriter();
- while ( 0 <= ( ch = reader.read() ) ) {
- if ( ST_ESCAPE == status ) {
- status = ST_NORMAL;
- } else {
- if ( escaper == ch ) {
- status = ST_ESCAPE;
- continue;
- } else if ( delimeter == ch ) {
- if ( index == targetIndex ) return writer.toString();
- ++index;
- if ( index == targetIndex ) writer = new StringWriter();
- continue;
- }
- }
- if ( null != writer ) writer.write( (int) ch );
- }
- if ( index == targetIndex ) return writer.toString();
- return null;
- }
- catch ( final IOException e )
- {
- // Never throw
- throw new IllegalStateException( e );
- }
- }
-
- /**
- * Return substring starting <code>indexFromLast</code> from last
- *
- * @param str original string
- * @param indexFromLast starting index from last
- *
- * @return substring
- */
- public static
- String
- lastSubstring(
- final String str,
- final int indexFromLast
- )
- {
- if ( null == str )
- {
- return null;
- }
-
- final int length = str.length();
-
- if ( length < indexFromLast )
- {
- return str;
- }
-
- return str.substring( length - indexFromLast );
- }
-
- /**
- * Extract last fragment in strings split <code>str</code> with <code>separator</code>
- *
- * @param str origin string
- * @param separator string for separation
- *
- * @return last fragment string
- */
- public static
- String
- getLastSegment(
- final String str,
- final String separator
- )
- {
- if ( null == str )
- {
- return EMPTY_STRING;
- }
-
- final int index = str.lastIndexOf( separator );
- if ( index < 0 )
- {
- return str;
- }
-
- return str.substring( index + separator.length() );
- }
-
- /**
- * Extract last fragment in string and return remaining string with <code>separator</code>
- *
- * @param str origin string
- * @param separator string for separation
- *
- * @return remaining string
- */
- public static
- String
- removeLastSegment(
- final String str,
- final String separator
- )
- {
- if ( null == str )
- {
- return EMPTY_STRING;
- }
-
- final int index = str.lastIndexOf( separator );
-
- if ( index < 0 )
- {
- return EMPTY_STRING;
- }
-
- return str.substring( 0, index );
-
- }
-
-
- /**
- * Make <code>n</code> repeated <code>symbol</code> string
- *
- * @param symbol composing string
- * @param n repeat count
- *
- * @return made string
- */
- public static
- String
- multiply(
- final String symbol,
- final int n
- )
- {
- if ( null == symbol ) {
- return EMPTY_STRING;
- }
-
- final StringBuilder buffer = new StringBuilder();
- for ( int i=0 ; i<n ; ++i )
- {
- buffer.append( symbol );
- }
- return buffer.toString();
- }
-
- /**
- * Check that <code>str</code> contain white spaces
- *
- * @param str string to check
- * @return <code>true</code> if <code>str</code> contain white spaces
- */
- public static
- boolean
- containsWhitespace(
- final CharSequence str
- )
- {
- final int nChar = size( str );
-
- for ( int i= 0; i< nChar; ++i )
- {
- if ( Character.isWhitespace( str.charAt( i ) ) )
- {
- return true;
- }
- }
- return false;
- }
-
-
-
- /**
- * Trim leading <code>leadingCharacter</code> from <code>str</code>
- *
- * @param str string to trim
- * @param leadingCharacter character to trim with
- *
- * @return trimmed string
- */
- public static
- String
- trimLeadingCharacter(
- final String str,
- final char leadingCharacter
- )
- {
- if( !hasLength( str ) )
- {
- return str;
- }
-
- final char[] chs = str.toCharArray();
- for ( int i=0, n=chs.length ; i<n ; ++i )
- {
- if ( leadingCharacter != chs[i] )
- {
- return new String( chs, i, str.length() - i );
- }
- }
- return EMPTY_STRING;
- }
-
- /**
- * Trim trailing <code>trailingCharacter</code> from <code>str</code>
- *
- * @param str string to trim
- * @param trailingCharacter character to trim with
- *
- * @return trimmed string
- */
- public static
- String
- trimTrailingCharacter(
- final String str,
- final char trailingCharacter
- )
- {
- if( !hasLength( str ) )
- {
- return str;
- }
-
- final char[] chs = str.toCharArray();
- for ( int i=chs.length-1, j=chs.length ; 0<j ; --i, --j )
- {
- if ( trailingCharacter != chs[i] )
- {
- return new String( chs, 0, j );
- }
- }
- return EMPTY_STRING;
- }
-
- /**
- * Trim <code>str</code> with <code>character</code>
- *
- * @param str string to trim
- * @param character character to trim with
- *
- * @return trimmed string
- */
- public static
- String
- trimCharacter(
- final String str,
- final char character
- )
- {
- return trimTrailingCharacter( trimLeadingCharacter( str, character ), character );
- }
-
-
- /**
- * Extract word at <coode>index</code> column in <code>doc</code>
- *
- * @param doc full string
- * @param index column index
- *
- * @return word at <code>index</code> column
- */
- public static
- String
- getWord(
- final String doc,
- final int index
- )
- {
- final StringBuilder buffer = new StringBuilder();
-
- int position = index;
-
- if ( doc.length() < index )
- {
- return "";
- }
-
- for ( int i = position - 1 ; 0 <= i ; --i )
- {
-
- int ch = doc.charAt( i );
-
- if ( Character.isWhitespace( ch ) )
- {
- break;
- }
- buffer.append( (char) ch );
- }
- buffer.reverse();
-
- for ( int i = position , n = doc.length() ; i < n ; ++i )
- {
- int ch = doc.charAt( i );
- if ( Character.isWhitespace( ch ) )
- {
- break;
- }
- buffer.append( (char) ch );
- }
-
- return buffer.toString();
- }
-
- /**
- * Extract word in front of <coode>index</code> column in <code>doc</code>
- *
- * @param doc full string
- * @param index column index
- *
- * @return word in front of <code>index</code> column
- */
- public static
- String
- getPreviousWord(
- final String doc,
- final int index
- )
- {
- final StringBuilder buffer = new StringBuilder();
-
- if ( index < 0 || doc.length() < index )
- {
- Logger.debug( "Index[{0}] is out of bound :[0:{1}]", index, doc.length() );
- return "";
- }
-
- int ch = 0;
- boolean bNeedWord = false;
- if ( index < doc.length() )
- {
- ch = doc.charAt( index );
- bNeedWord = Character.isWhitespace( ch );
- }
- Logger.debug( "Character at index :'{0}'", (char ) ch );
- for ( int i = index - 1 ; 0 <= i ; --i )
- {
- ch = doc.charAt( i );
-
- if ( Character.isWhitespace( ch ) )
- {
- if ( bNeedWord )
- {
- continue;
- }
- Logger.debug( "Meet space at column {0}", i );
- break;
- }
- bNeedWord = false;
- buffer.append( (char) ch );
- }
-
- return buffer.reverse().toString();
- }
+ /**
+ * </p>
+ * Append hexa value for <code>ch</code> to <code>buffer</code>
+ *
+ * Hexa value must be two digit. Pad 0 if hexa value has 1 digit.
+ * </p>
+ *
+ * @param buffer {@link StringBuilder} to write
+ * @param ch decimal integer
+ */
+ public static
+ void
+ appendHexa(
+ final StringBuilder buffer,
+ final int ch
+ )
+ {
+ if ( ch < 16 )
+ {
+ // if 1 digit containing hexadecimal
+ buffer.append( '0' );
+ buffer.append( HEXA_CHARS[( 0x0f & ( ch ) )] );
+ }
+ else
+ {
+ // if 2 digit containing hexadecimal
+ buffer.append( HEXA_CHARS[( 0x0f & ( ch >> 4 ) )] );
+ buffer.append( HEXA_CHARS[( 0x0f & ( ch ) )] );
+ }
+ }
+
+ /**
+ * Add end seperation at logging line's end
+ *
+ * @param hexPart hex decimal part
+ * @param textPart text part
+ * @param ret all part
+ */
+ protected static
+ void
+ lineEnd(
+ final StringBuilder hexPart,
+ final StringBuilder textPart,
+ final StringBuilder ret
+ )
+ {
+ // append separator of area
+ hexPart.append( " |" );
+
+ // append separator of termination
+ textPart.append( "|\n" );
+
+ // concatencate two area
+ ret.append( hexPart );
+ ret.append( textPart );
+
+ // clean each area
+ hexPart.delete( 0, hexPart.capacity() );
+ textPart.delete( 0, textPart.capacity() );
+ }
+
+ /**
+ * <p>
+ * Convert <code>data</code> in hexadecimal format for logging or readibility
+ *
+ * Output layout<br>
+ * <table>
+ * <tr><td>Address( or offset )</td><td>byte character</td><td>hexadecimal</td></tr>
+ * </table>
+ *
+ * Replace control character to '.'<br>
+ * </p>
+ *
+ * <code>
+ * Logger logger = ...
+ * byte[] dump = ...
+ * logger.debug( StringUtil.text2hexa( dump ) );
+ * </code>
+ *
+ * @param data byte array to convert
+ *
+ * @return converted textual string
+ *
+ * @see #text2hexa(byte[], int, int)
+ */
+ public static
+ String
+ text2hexa(
+ final byte[] data
+ )
+ {
+ if ( null == data )
+ {
+ return NULL_STRING;
+ }
+ return text2hexa( data, 0, data.length );
+ }
+
+ /**
+ * <p>
+ * Convert data from <code>offset</code> to <code>offset</code> + <code>length</code> in hexadecimal format.
+ *
+ * Output layout<br>
+ * <table>
+ * <tr><td>Address( or offset )</td><td>byte character</td><td>hexadecimal</td></tr>
+ * </table>
+ *
+ * Replace control character to '.'<br>
+ * </p>
+ *
+ * @param data byte array to convert
+ * @param offset start position to convert
+ * @param length length to convert
+ *
+ * @return converted textual string
+ */
+ public static
+ String
+ text2hexa(
+ final byte[] data,
+ final int offset,
+ final int length
+ )
+ {
+
+ if ( null == data )
+ {
+ return NULL_STRING;
+ }
+ if ( data.length <= 0 )
+ {
+ return EMPTY_BYTES_STRING;
+ }
+
+ final ByteArrayInputStream reader = new ByteArrayInputStream( data, offset, length );
+ final StringBuilder ret = new StringBuilder();
+ final StringBuilder hexPart = new StringBuilder();
+ final StringBuilder textPart = new StringBuilder();
+
+ int address = 0;
+ int ch = -1;
+ int printByte = 0;
+ int cnt = 0;
+
+ // fill white space( ' ' ) in address title
+ hexPart.append( " " );
+
+ // make horizontal ruler
+ for ( int i = 0, n = WIDTH_PER_LINE / 4 ; i < n ; i++ )
+ {
+ hexPart.append( "+-------" );
+ textPart.append( "+---" );
+ }
+
+ lineEnd( hexPart, textPart, ret );
+
+ while ( 0 <= ( ch = reader.read() ) )
+ {
+ if ( 0 == cnt )
+ {
+ // calculate and print out start address
+ for ( int i = N_INT_BY_BYTE - 1 ; i >= 0 ; i-- )
+ {
+ printByte = 0xFF & ( address >> ( 8 * i ) );
+ appendHexa( hexPart, printByte );
+ }
+ hexPart.append( " " );
+ address += WIDTH_PER_LINE;
+ }
+
+ appendHexa( hexPart, ch );
+ if ( ( ch & 0x80 ) != 0 || ch < 32 )
+ { // if ch is control character
+ // print out replaced character
+ textPart.append( CONTROL_CHARS_SHOWER );
+ }
+ else
+ {
+ textPart.append( (char) ch );
+ }
+ cnt++;
+
+ if ( WIDTH_PER_LINE == cnt )
+ {
+ lineEnd( hexPart, textPart, ret );
+ cnt = 0;
+ }
+ } // END of while ( 0 <= (ch = reader.read() ) )
+
+ // fill white space( ' ' ) in remaining area
+ if ( 0 != cnt )
+ {
+ for ( ; cnt < WIDTH_PER_LINE ; ++cnt )
+ {
+ hexPart.append( " " );
+ textPart.append( ' ' );
+ }
+ lineEnd( hexPart, textPart, ret );
+ }
+
+ return ret.toString();
+ }
+
+
+ /* Check & Verification */
+ /**
+ * Check that <code>str</code> is <code>null</code> or empty string textually
+ *
+ * @param str string to check
+ *
+ * @return <code>true</code> if <code>str</code> is null or empty string or string containing only white space
+ *
+ * @see StringUtil#hasText( CharSequence )
+ */
+ public static
+ boolean
+ isEmpty(
+ final CharSequence str
+ )
+ {
+ if ( null == str )
+ {
+ return true;
+ }
+
+ for ( int i=0, n=str.length() ; i<n ; ++i )
+ {
+ if ( Character.isWhitespace( str.charAt( i ) ) )
+ {
+ continue;
+ }
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Get length of string
+ *
+ * Return <code>0</code> if <code>str</code> is <code>null</code>
+ *
+ * @param str string whose length is checked
+ *
+ * @return length of <code>str</code>
+ */
+ public static
+ int
+ size(
+ final CharSequence str
+ )
+ {
+ if ( null == str )
+ {
+ return 0;
+ }
+ return str.length();
+ }
+
+ /**
+ * <p>
+ * Check that <code>str</code> contain any character
+ * </p>
+ * @param str string to check
+ *
+ * @return <code>true</code> if <code>str</code> is not <code>null</code> nor empty string
+ *
+ * @see #size(CharSequence)
+ */
+ public static
+ boolean
+ hasLength(
+ final CharSequence str
+ )
+ {
+ return 0 < size( str );
+ }
+
+ /**
+ * Check that <code>str</code> has textual character
+ *
+ * @param str string to check
+ *
+ * @return <code>true</code> if <code>str</code> has textual character
+ *
+ * @see isEmpty( CharSequence )
+ */
+ public static
+ boolean
+ hasText(
+ final CharSequence str
+ )
+ {
+ return !isEmpty( str );
+ }
+
+ /* Conversion */
+ /**
+ * Check <code>strs</code> and return non-null string
+ *
+ * Return empty string if all of element in <code>strs</code>
+ *
+ * @param strs strings to check
+ *
+ * @return non-null in <code>strs</code>
+ *
+ * see #ObjectUtil{@link #nvl(String...)}
+ */
+ public static
+ String
+ nvl(
+ final String... strs
+ )
+ {
+ String val = ObjectUtil.nvl( strs );
+ if ( null == val )
+ {
+ return "";
+ }
+ return val;
+ }
+
+ /**
+ * Trim leading whitespaqce
+ *
+ * @param str string to trim
+ *
+ * @return trimmed string
+ */
+ public static
+ String
+ trimLeading(
+ final String str
+ )
+ {
+ if( !hasLength( str ) )
+ {
+ return str;
+ }
+
+ final char[] chs = str.toCharArray();
+ for ( int i=0, n=chs.length ; i<n ; ++i )
+ {
+ if ( !Character.isWhitespace( chs[i] ) )
+ {
+ return new String( chs, i, str.length() - i );
+ }
+ }
+ return EMPTY_STRING;
+ }
+
+ /**
+ * Trim trailing white space
+ *
+ * @param str string to trim
+ *
+ * @return trimmed string
+ */
+ public static
+ String
+ trimTrailing(
+ final String str
+ )
+ {
+ if( !hasLength( str ) )
+ {
+ return str;
+ }
+
+ final char[] chs = str.toCharArray();
+ for ( int i=chs.length-1, j=chs.length ; 0<j ; --i, --j )
+ {
+ if ( !Character.isWhitespace( chs[i] ) )
+ {
+ return new String( chs, 0, j );
+ }
+ }
+ return EMPTY_STRING;
+ }
+ /**
+ * Remove white spaces at leading and trailing part of <code>str</code>
+ *
+ * Return empty string if <code>str</code> is <code>null</code>
+ *
+ * @param str string to convert
+ *
+ * @return converted string
+ *
+ * @see StringUtil#trimLeading(String)
+ * @see StringUtil#trimTrailing(String)
+ */
+ public static
+ String
+ trim(
+ final String str
+ ) {
+ return trimTrailing( trimLeading( str ) );
+ }
+
+ /**
+ * Convert <code>str</code> to meaningful for search
+ *
+ * @param str string to convert
+ *
+ * @return converted string
+ */
+ public static
+ String
+ getMeaningful(
+ final String str
+ ) {
+ if ( null == str )
+ {
+ return EMPTY_STRING;
+ }
+ return trim( str ).toLowerCase();
+ }
+
+ /**
+ * Mask <code>str</code> with <code>maskingStr</code>
+ *
+ * Replace <code>str</code> to <code>maskingStr</code> with <code>str</code>'s length
+ *
+ * @param str string to mask
+ * @param maskingStr string to mask with
+ *
+ * @return masked string
+ */
+ public static
+ String
+ mask(
+ final String str,
+ final String maskingStr
+ ) {
+ if ( null == str )
+ {
+ return "null";
+ }
+
+ final StringBuilder buffer = new StringBuilder();
+ for ( int i=0, n=str.length() ; i<n ; ++i )
+ {
+ buffer.append( maskingStr );
+ }
+ return buffer.toString();
+ }
+
+ /* Analysis & Extract */
+ /**
+ * Extract <code>targetIndex</code> string from strings
+ *
+ * where <code>str</code> is splitted by <code>delimeter</code>
+ *
+ * @param str string with <code>delimeter</code>
+ * @param delimeter what split <code>str</code> with
+ * @param escaper escape character for <code>delimter</code>
+ * @param targetIndex index from fragments
+ *
+ * @return extracted string
+ */
+ public static
+ String
+ getParamater(
+ final String str,
+ final char delimeter,
+ final int escaper,
+ final int targetIndex
+ )
+ throws IOException
+ {
+ try
+ {
+ if ( null == str )
+ {
+ return null;
+ }
+ final StringReader reader = new StringReader( str );
+ StringWriter writer = null;
+ int ch = 0;
+ final int ST_NORMAL = 0;
+ final int ST_ESCAPE = 1;
+ int status = ST_NORMAL;
+ int index = 0;
+ if ( 0 == targetIndex ) writer = new StringWriter();
+ while ( 0 <= ( ch = reader.read() ) ) {
+ if ( ST_ESCAPE == status ) {
+ status = ST_NORMAL;
+ } else {
+ if ( escaper == ch ) {
+ status = ST_ESCAPE;
+ continue;
+ } else if ( delimeter == ch ) {
+ if ( index == targetIndex ) return writer.toString();
+ ++index;
+ if ( index == targetIndex ) writer = new StringWriter();
+ continue;
+ }
+ }
+ if ( null != writer ) writer.write( (int) ch );
+ }
+ if ( index == targetIndex ) return writer.toString();
+ return null;
+ }
+ catch ( final IOException e )
+ {
+ // Never throw
+ throw new IllegalStateException( e );
+ }
+ }
+
+ /**
+ * Return substring starting <code>indexFromLast</code> from last
+ *
+ * @param str original string
+ * @param indexFromLast starting index from last
+ *
+ * @return substring
+ */
+ public static
+ String
+ lastSubstring(
+ final String str,
+ final int indexFromLast
+ )
+ {
+ if ( null == str )
+ {
+ return null;
+ }
+
+ final int length = str.length();
+
+ if ( length < indexFromLast )
+ {
+ return str;
+ }
+
+ return str.substring( length - indexFromLast );
+ }
+
+ /**
+ * Extract last fragment in strings split <code>str</code> with <code>separator</code>
+ *
+ * @param str origin string
+ * @param separator string for separation
+ *
+ * @return last fragment string
+ */
+ public static
+ String
+ getLastSegment(
+ final String str,
+ final String separator
+ )
+ {
+ if ( null == str )
+ {
+ return EMPTY_STRING;
+ }
+
+ final int index = str.lastIndexOf( separator );
+ if ( index < 0 )
+ {
+ return str;
+ }
+
+ return str.substring( index + separator.length() );
+ }
+
+ /**
+ * Extract last fragment in string and return remaining string with <code>separator</code>
+ *
+ * @param str origin string
+ * @param separator string for separation
+ *
+ * @return remaining string
+ */
+ public static
+ String
+ removeLastSegment(
+ final String str,
+ final String separator
+ )
+ {
+ if ( null == str )
+ {
+ return EMPTY_STRING;
+ }
+
+ final int index = str.lastIndexOf( separator );
+
+ if ( index < 0 )
+ {
+ return EMPTY_STRING;
+ }
+
+ return str.substring( 0, index );
+
+ }
+
+ /**
+ * Make <code>n</code> repeated <code>symbol</code> string
+ *
+ * @param symbol composing string
+ * @param n repeat count
+ *
+ * @return made string
+ */
+ public static
+ String
+ multiply(
+ final String symbol,
+ final int n
+ )
+ {
+ if ( null == symbol ) {
+ return EMPTY_STRING;
+ }
+
+ final StringBuilder buffer = new StringBuilder();
+ for ( int i=0 ; i<n ; ++i )
+ {
+ buffer.append( symbol );
+ }
+ return buffer.toString();
+ }
+
+ /**
+ * Check that <code>str</code> contain white spaces
+ *
+ * @param str string to check
+ * @return <code>true</code> if <code>str</code> contain white spaces
+ */
+ public static
+ boolean
+ containsWhitespace(
+ final CharSequence str
+ )
+ {
+ final int nChar = size( str );
+
+ for ( int i= 0; i< nChar; ++i )
+ {
+ if ( Character.isWhitespace( str.charAt( i ) ) )
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+ /**
+ * Trim leading <code>leadingCharacter</code> from <code>str</code>
+ *
+ * @param str string to trim
+ * @param leadingCharacter character to trim with
+ *
+ * @return trimmed string
+ */
+ public static
+ String
+ trimLeadingCharacter(
+ final String str,
+ final char leadingCharacter
+ )
+ {
+ if( !hasLength( str ) )
+ {
+ return str;
+ }
+
+ final char[] chs = str.toCharArray();
+ for ( int i=0, n=chs.length ; i<n ; ++i )
+ {
+ if ( leadingCharacter != chs[i] )
+ {
+ return new String( chs, i, str.length() - i );
+ }
+ }
+ return EMPTY_STRING;
+ }
+
+ /**
+ * Trim trailing <code>trailingCharacter</code> from <code>str</code>
+ *
+ * @param str string to trim
+ * @param trailingCharacter character to trim with
+ *
+ * @return trimmed string
+ */
+ public static
+ String
+ trimTrailingCharacter(
+ final String str,
+ final char trailingCharacter
+ )
+ {
+ if( !hasLength( str ) )
+ {
+ return str;
+ }
+
+ final char[] chs = str.toCharArray();
+ for ( int i=chs.length-1, j=chs.length ; 0<j ; --i, --j )
+ {
+ if ( trailingCharacter != chs[i] )
+ {
+ return new String( chs, 0, j );
+ }
+ }
+ return EMPTY_STRING;
+ }
+
+ /**
+ * Trim <code>str</code> with <code>character</code>
+ *
+ * @param str string to trim
+ * @param character character to trim with
+ *
+ * @return trimmed string
+ */
+ public static
+ String
+ trimCharacter(
+ final String str,
+ final char character
+ )
+ {
+ return trimTrailingCharacter( trimLeadingCharacter( str, character ), character );
+ }
+
+ /**
+ * Extract word at <coode>index</code> column in <code>doc</code>
+ *
+ * @param doc full string
+ * @param index column index
+ *
+ * @return word at <code>index</code> column
+ */
+ public static
+ String
+ getWord(
+ final String doc,
+ final int index
+ )
+ {
+ final StringBuilder buffer = new StringBuilder();
+
+ int position = index;
+
+ if ( doc.length() < index )
+ {
+ return "";
+ }
+
+ for ( int i = position - 1 ; 0 <= i ; --i )
+ {
+
+ int ch = doc.charAt( i );
+
+ if ( Character.isWhitespace( ch ) )
+ {
+ break;
+ }
+ buffer.append( (char) ch );
+ }
+ buffer.reverse();
+
+ for ( int i = position , n = doc.length() ; i < n ; ++i )
+ {
+ int ch = doc.charAt( i );
+ if ( Character.isWhitespace( ch ) )
+ {
+ break;
+ }
+ buffer.append( (char) ch );
+ }
+
+ return buffer.toString();
+ }
+
+ /**
+ * Extract word in front of <coode>index</code> column in <code>doc</code>
+ *
+ * @param doc full string
+ * @param index column index
+ *
+ * @return word in front of <code>index</code> column
+ */
+ public static
+ String
+ getPreviousWord(
+ final String doc,
+ final int index
+ )
+ {
+ final StringBuilder buffer = new StringBuilder();
+
+ if ( index < 0 || doc.length() < index )
+ {
+ Logger.debug( "Index[{0}] is out of bound :[0:{1}]", index, doc.length() );
+ return "";
+ }
+
+ int ch = 0;
+ boolean bNeedWord = false;
+ if ( index < doc.length() )
+ {
+ ch = doc.charAt( index );
+ bNeedWord = Character.isWhitespace( ch );
+ }
+ Logger.debug( "Character at index :'{0}'", (char ) ch );
+ for ( int i = index - 1 ; 0 <= i ; --i )
+ {
+ ch = doc.charAt( i );
+
+ if ( Character.isWhitespace( ch ) )
+ {
+ if ( bNeedWord )
+ {
+ continue;
+ }
+ Logger.debug( "Meet space at column {0}", i );
+ break;
+ }
+ bNeedWord = false;
+ buffer.append( (char) ch );
+ }
+
+ return buffer.reverse().toString();
+ }
+
+ /**
+ * Enum to String array
+ *
+ * @param values enum values
+ *
+ * @return string array
+ */
+ public static <T extends Enum<T>> String[] enumNameToStringArray(T[] values) {
+ int i = 0;
+ String[] result = new String[values.length];
+ for (T value: values) {
+ result[i++] = value.name();
+ }
+ return result;
+ }
+
}
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class
StringUtilTest
{
- /**
+ /**
* @throws Exception in case of failure in test
- *
- * @see StringUtil#nvl(String...)
- */
- @Test
- public
- void
- test_nvl()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "", new String[] { null, null } },
- new Object[] { "Hello", new String[] { "Hello", "World" } },
- new Object[] { "World", new String[] { null, "World" } },
- new Object[] { "hello", new String[] { null, "hello" } },
- new Object[] { "hello", new String[] { "hello", "world" } },
- new Object[] { "hello", new String[] { null, null, "hello", "world" } },
-
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String expected = (String) TEST_CASE[0];
- final String[] input = (String[]) TEST_CASE[1];
- assertEquals( expected, StringUtil.nvl( input ) );
- }
- }
+ *
+ * @see StringUtil#nvl(String...)
+ */
+ @Test
+ public
+ void
+ test_nvl()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "", new String[] { null, null } },
+ new Object[] { "Hello", new String[] { "Hello", "World" } },
+ new Object[] { "World", new String[] { null, "World" } },
+ new Object[] { "hello", new String[] { null, "hello" } },
+ new Object[] { "hello", new String[] { "hello", "world" } },
+ new Object[] { "hello", new String[] { null, null, "hello", "world" } },
+ };
- /**
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String expected = (String) TEST_CASE[0];
+ final String[] input = (String[]) TEST_CASE[1];
+ assertEquals( expected, StringUtil.nvl( input ) );
+ }
+ }
+
+ /**
+ * @throws Exception in case of failure in test
+ *
+ * @see {@link StringUtil#trimLeadingWhitespace(String)}
+ */
+ @Test
+ public void
+ test_trimLeadingWhitespace()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "", "" },
+ new Object[] { "", " " },
+ new Object[] { null, null },
+ new Object[] { "Hello", " Hello" },
+ new Object[] { "H ", " H " },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String expected = (String) TEST_CASE[0];
+ final String input = (String) TEST_CASE[1];
+ assertEquals( expected, StringUtil.trimLeading( input ) );
+ }
+ }
+
+ /**
+ * @throws Exception in case of failure in test
+ *
+ * @see {@link StringUtil#trimLeadingWhitespace(String)}
+ */
+ @Test
+ public void
+ test_trimTrailingWhitespace()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "", "" },
+ new Object[] { "", " " },
+ new Object[] { null, null },
+ new Object[] { "Hello", "Hello " },
+ new Object[] { " H", " H " },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String expected = (String) TEST_CASE[0];
+ final String input = (String) TEST_CASE[1];
+ assertEquals( expected, StringUtil.trimTrailing( input ) );
+ }
+ }
+
+ /**
+ * @throws Exception in case of failure in test
+ *
+ * @see {@link StringUtil#trimLeadingCharacter(String, char)}
+ */
+ @Test
+ public void
+ test_trimLeadingCharacter()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "", "", '-' },
+ new Object[] { " ", " ", '-' },
+ new Object[] { null, null, '-' },
+ new Object[] { "Hello ", "---Hello ", '-' },
+ new Object[] { " H ", " H ", '-' },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String expected = (String) TEST_CASE[0];
+ final String input = (String) TEST_CASE[1];
+ final char trimChar = (Character) TEST_CASE[2];
+ assertEquals(
+ expected,
+ StringUtil.trimLeadingCharacter( input, trimChar )
+ );
+ }
+ }
+
+ /**
+ * @throws Exception in case of failure in test
+ *
+ * @see {@link StringUtil#trimTrailingCharacter(String, char)
+ */
+ @Test
+ public void
+ test_trimTrailingCharacter()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "", "", '-' },
+ new Object[] { " ", " ", '-' },
+ new Object[] { null, null, '-' },
+ new Object[] { "Hello", "Hello---", '-' },
+ new Object[] { " H-- ", " H-- ", '-' },
+ new Object[] { " H", " H--", '-' },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String expected = (String) TEST_CASE[0];
+ final String input = (String) TEST_CASE[1];
+ final char trimChar = (Character) TEST_CASE[2];
+ assertEquals(
+ expected,
+ StringUtil.trimTrailingCharacter( input, trimChar )
+ );
+ }
+ }
+
+ /**
* @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#trimLeadingWhitespace(String)}
- */
- @Test
- public void
- test_trimLeadingWhitespace()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "", "" },
- new Object[] { "", " " },
- new Object[] { null, null },
- new Object[] { "Hello", " Hello" },
- new Object[] { "H ", " H " },
-
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String expected = (String) TEST_CASE[0];
- final String input = (String) TEST_CASE[1];
- assertEquals( expected, StringUtil.trimLeading( input ) );
- }
- }
-
- /**
+ *
+ * @see {@link StringUtil#trimCharacter(String, char)}
+ */
+ @Test
+ public
+ void
+ test_trimCharacter()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "", "", '-' },
+ new Object[] { " ", " ", '-' },
+ new Object[] { null, null, '-' },
+ new Object[] { "Hello", "Hello---", '-' },
+ new Object[] { " H-- ", " H-- ", '-' },
+ new Object[] { " H", " H--", '-' },
+ new Object[] { " H", "-- H--", '-' },
+ new Object[] { "H", "--H--", '-' },
+ new Object[] { " - H", "-- - H--", '-' },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String expected = (String) TEST_CASE[0];
+ final String input = (String) TEST_CASE[1];
+ final char trimChar = (Character) TEST_CASE[2];
+ assertEquals(
+ expected,
+ StringUtil.trimCharacter( input, trimChar )
+ );
+ }
+
+ }
+
+ /**
* @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#trimLeadingWhitespace(String)}
- */
- @Test
- public void
- test_trimTrailingWhitespace()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "", "" },
- new Object[] { "", " " },
- new Object[] { null, null },
- new Object[] { "Hello", "Hello " },
- new Object[] { " H", " H " },
-
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String expected = (String) TEST_CASE[0];
- final String input = (String) TEST_CASE[1];
- assertEquals( expected, StringUtil.trimTrailing( input ) );
- }
- }
-
- /**
+ *
+ * @see StringUtil#getWord(String, int)
+ */
+ @Test
+ public void
+ test_getWord()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "I am a boy", 0, "I" },
+ new Object[] { "I am a boy", 1, "I" },
+ new Object[] { "I am a boy", 2, "am" },
+ new Object[] { "I am a boy", 3, "am" },
+ new Object[] { "I am a boy", 4, "am" },
+ new Object[] { "I am a boy", 5, "a" },
+ new Object[] { "I am a boy", 6, "a" },
+ new Object[] { "I am a boy", 7, "boy" },
+ new Object[] { "I am a boy", 8, "boy" },
+ new Object[] { "I am a boy", 9, "boy" },
+ new Object[] { "I am a boy", 10, "boy" },
+ new Object[] { "I am a boy", 11, "" },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String doc = (String) TEST_CASE[0];
+ final int index = (Integer) TEST_CASE[1];
+ final String expected = (String) TEST_CASE[2];
+ assertEquals(
+ expected,
+ StringUtil.getWord( doc, index )
+ );
+ }
+ }
+
+ /**
* @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#trimLeadingCharacter(String, char)}
- */
- @Test
- public void
- test_trimLeadingCharacter()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "", "", '-' },
- new Object[] { " ", " ", '-' },
- new Object[] { null, null, '-' },
- new Object[] { "Hello ", "---Hello ", '-' },
- new Object[] { " H ", " H ", '-' },
-
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String expected = (String) TEST_CASE[0];
- final String input = (String) TEST_CASE[1];
- final char trimChar = (Character) TEST_CASE[2];
- assertEquals(
- expected,
- StringUtil.trimLeadingCharacter( input, trimChar )
- );
- }
- }
+ *
+ * @see StringUtil#getPreviousWord(String, int)
+ */
+ @Test
+ public void
+ test_getPreviousWord()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "I am a boy ", 0, "" },
+ new Object[] { "I am a boy ", 1, "I" },
+ new Object[] { "I am a boy ", 2, "" },
+ new Object[] { "I am a boy ", 3, "a" },
+ new Object[] { "I am a boy ", 4, "am" },
+ new Object[] { "I am a boy ", 5, "" },
+ new Object[] { "I am a boy ", 6, "a" },
+ new Object[] { "I am a boy ", 7, "" },
+ new Object[] { "I am a boy ", 8, "b" },
+ new Object[] { "I am a boy ", 9, "bo" },
+ new Object[] { "I am a boy ", 10, "boy" },
+ new Object[] { "I am a boy ", 11, "" },
+ new Object[] { "I am a boy ", 12, "" },
+ new Object[] { "#{\ntest", 2, "#{" },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String doc = (String) TEST_CASE[0];
+ final int index = (Integer) TEST_CASE[1];
+ final String expected = (String) TEST_CASE[2];
+ assertEquals(
+ expected,
+ StringUtil.getPreviousWord( doc, index )
+ );
+ }
+ }
- /**
+ /**
+ * Test {@link StringUtil#getLastSegment(String, String)}
+ *
* @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#trimTrailingCharacter(String, char)
- */
- @Test
- public void
- test_trimTrailingCharacter()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "", "", '-' },
- new Object[] { " ", " ", '-' },
- new Object[] { null, null, '-' },
- new Object[] { "Hello", "Hello---", '-' },
- new Object[] { " H-- ", " H-- ", '-' },
- new Object[] { " H", " H--", '-' },
-
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String expected = (String) TEST_CASE[0];
- final String input = (String) TEST_CASE[1];
- final char trimChar = (Character) TEST_CASE[2];
- assertEquals(
- expected,
- StringUtil.trimTrailingCharacter( input, trimChar )
- );
- }
- }
-
-
- /**
+ *
+ * @see {@link StringUtil#getLastSegment(String, String)}
+ */
+ @Test
+ public
+ void
+ test_getLastSegement()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { null, " ", "" },
+ new Object[] { "I am a boy", " ", "boy" },
+ new Object[] { "I am a boy", " ", "boy" },
+ new Object[] { "I am a boy ", " ", "" },
+ new Object[] { "a,b,c,d", ",", "d" },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String doc = (String) TEST_CASE[0];
+ final String separator = (String) TEST_CASE[1];
+ final String expected = (String) TEST_CASE[2];
+ assertEquals(
+ expected,
+ StringUtil.getLastSegment( doc, separator )
+ );
+ }
+ }
+
+ /**
+ * Test {@link StringUtil#getLastSegment(String, String)}
+ *
* @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#trimCharacter(String, char)}
- */
- @Test
- public
- void
- test_trimCharacter()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "", "", '-' },
- new Object[] { " ", " ", '-' },
- new Object[] { null, null, '-' },
- new Object[] { "Hello", "Hello---", '-' },
- new Object[] { " H-- ", " H-- ", '-' },
- new Object[] { " H", " H--", '-' },
- new Object[] { " H", "-- H--", '-' },
- new Object[] { "H", "--H--", '-' },
- new Object[] { " - H", "-- - H--", '-' },
-
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String expected = (String) TEST_CASE[0];
- final String input = (String) TEST_CASE[1];
- final char trimChar = (Character) TEST_CASE[2];
- assertEquals(
- expected,
- StringUtil.trimCharacter( input, trimChar )
- );
- }
-
- }
+ *
+ * @see {@link StringUtil#getLastSegment(String, String)}
+ */
+ @Test
+ public
+ void
+ test_removeLastSegement()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { null, " ", "" },
+ new Object[] { "I am a boy", " ", "I am a" },
+ new Object[] { "I am a boy", " ", "I am a " }, // FIXME ?
+ new Object[] { "I am a boy ", " ", "I am a boy" },
+ new Object[] { "a,b,c,d", ",", "a,b,c" },
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String doc = (String) TEST_CASE[0];
+ final String separator = (String) TEST_CASE[1];
+ final String expected = (String) TEST_CASE[2];
+ assertEquals(
+ expected,
+ StringUtil.removeLastSegment( doc, separator )
+ );
+ }
+ }
- /**
+ /**
+ * Test {@link StringUtil#split(String, String)}
+ *
* @throws Exception in case of failure in test
- *
- * @see StringUtil#getWord(String, int)
- */
- @Test
- public void
- test_getWord()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "I am a boy", 0, "I" },
- new Object[] { "I am a boy", 1, "I" },
- new Object[] { "I am a boy", 2, "am" },
- new Object[] { "I am a boy", 3, "am" },
- new Object[] { "I am a boy", 4, "am" },
- new Object[] { "I am a boy", 5, "a" },
- new Object[] { "I am a boy", 6, "a" },
- new Object[] { "I am a boy", 7, "boy" },
- new Object[] { "I am a boy", 8, "boy" },
- new Object[] { "I am a boy", 9, "boy" },
- new Object[] { "I am a boy", 10, "boy" },
- new Object[] { "I am a boy", 11, "" },
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String doc = (String) TEST_CASE[0];
- final int index = (Integer) TEST_CASE[1];
- final String expected = (String) TEST_CASE[2];
- assertEquals(
- expected,
- StringUtil.getWord( doc, index )
- );
- }
- }
+ *
+ * @see {@link StringUtil#split(String, String)}
+ */
+ @Test
+ public
+ void
+ test_split()
+ throws Exception
+ {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { "aa.bbb.ccc", ".", new String[] { "aa", "bbb", "ccc" } }
+ };
+
+ for ( final Object[] TEST_CASE : TEST_CASES )
+ {
+ final String input = (String) TEST_CASE[0];
+ final String delimiters = (String) TEST_CASE[1];
+ final String[] expected = (String[]) TEST_CASE[2];
- /**
+ assertArrayEquals( expected, StringUtil.split( input, delimiters ) );
+ }
+ }
+
+ /**
+ * Test {@link StringUtil#enumNameToStringArray(T[] values)}
+ *
* @throws Exception in case of failure in test
- *
- * @see StringUtil#getPreviousWord(String, int)
- */
- @Test
- public void
- test_getPreviousWord()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "I am a boy ", 0, "" },
- new Object[] { "I am a boy ", 1, "I" },
- new Object[] { "I am a boy ", 2, "" },
- new Object[] { "I am a boy ", 3, "a" },
- new Object[] { "I am a boy ", 4, "am" },
- new Object[] { "I am a boy ", 5, "" },
- new Object[] { "I am a boy ", 6, "a" },
- new Object[] { "I am a boy ", 7, "" },
- new Object[] { "I am a boy ", 8, "b" },
- new Object[] { "I am a boy ", 9, "bo" },
- new Object[] { "I am a boy ", 10, "boy" },
- new Object[] { "I am a boy ", 11, "" },
- new Object[] { "I am a boy ", 12, "" },
- new Object[] { "#{\ntest", 2, "#{" },
-
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String doc = (String) TEST_CASE[0];
- final int index = (Integer) TEST_CASE[1];
- final String expected = (String) TEST_CASE[2];
- assertEquals(
- expected,
- StringUtil.getPreviousWord( doc, index )
- );
- }
- }
-
- /**
- * Test {@link StringUtil#getLastSegment(String, String)}
- *
- * @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#getLastSegment(String, String)}
- */
- @Test
- public
- void
- test_getLastSegement()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { null, " ", "" },
- new Object[] { "I am a boy", " ", "boy" },
- new Object[] { "I am a boy", " ", "boy" },
- new Object[] { "I am a boy ", " ", "" },
- new Object[] { "a,b,c,d", ",", "d" },
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String doc = (String) TEST_CASE[0];
- final String separator = (String) TEST_CASE[1];
- final String expected = (String) TEST_CASE[2];
- assertEquals(
- expected,
- StringUtil.getLastSegment( doc, separator )
- );
- }
- }
-
- /**
- * Test {@link StringUtil#getLastSegment(String, String)}
- *
- * @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#getLastSegment(String, String)}
- */
- @Test
- public
- void
- test_removeLastSegement()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { null, " ", "" },
- new Object[] { "I am a boy", " ", "I am a" },
- new Object[] { "I am a boy", " ", "I am a " }, // FIXME ?
- new Object[] { "I am a boy ", " ", "I am a boy" },
- new Object[] { "a,b,c,d", ",", "a,b,c" },
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String doc = (String) TEST_CASE[0];
- final String separator = (String) TEST_CASE[1];
- final String expected = (String) TEST_CASE[2];
- assertEquals(
- expected,
- StringUtil.removeLastSegment( doc, separator )
- );
- }
- }
-
-
- /**
- * Test {@link StringUtil#split(String, String)}
- *
- * @throws Exception in case of failure in test
- *
- * @see {@link StringUtil#split(String, String)}
- */
- @Test
- public
- void
- test_split()
- throws Exception
- {
- final Object[][] TEST_CASES = new Object[][] {
- new Object[] { "aa.bbb.ccc", ".", new String[] { "aa", "bbb", "ccc" } }
- };
-
- for ( final Object[] TEST_CASE : TEST_CASES )
- {
- final String input = (String) TEST_CASE[0];
- final String delimiters = (String) TEST_CASE[1];
- final String[] expected = (String[]) TEST_CASE[2];
-
- assertArrayEquals( expected, StringUtil.split( input, delimiters ) );
- }
- }
+ *
+ * @see {@link StringUtil#enumNameToStringArray(T[] values)}
+ */
+ @Test
+ public void test_enumNameToStringArray() throws Exception {
+ TestEnumCase [] enums = TestEnumCase.values();
+ String [] values = StringUtil.enumNameToStringArray(enums);
+ assertTrue(enums.length == values.length);
+
+ StringBuilder enumBuilder = new StringBuilder();
+ for (TestEnumCase testEnumCase : enums) {
+ enumBuilder.append(testEnumCase.name());
+ }
+
+ StringBuilder utilBuilder = new StringBuilder();
+ for (String string : values) {
+ utilBuilder.append(string);
+ }
+ assertEquals(enumBuilder.toString(), utilBuilder.toString());
+ }
+ public enum TestEnumCase {
+ ABC("ABC"),
+ DEF("DEF"),
+ GHI("GHI"),
+ JKL("JKL");
+ private String value;
+ TestEnumCase(String v) {
+ value = v;
+ }
+ public String value() {
+ return value;
+ }
+ }
}