001package votorola.g.lang; // Copyright 2008, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. 002 003 004/** StringBuilder utilities. 005 */ 006public @ThreadSafe final class StringBuilderX 007{ 008 009 private StringBuilderX() {} 010 011 012 013 /** Clears the string builder. 014 * 015 * @return the same string builder. 016 */ 017 public static StringBuilder clear( final StringBuilder b ) 018 { 019 b.delete( 0, b.length() ); 020 return b; 021 } 022 023 024 025 /** Collapses contiguous runs of whitespace to a single space each, and trims all 026 * whitepace from the ends. 027 * 028 * @return the same string builder. 029 */ 030 public static StringBuilder collapseAndTrim( final StringBuilder b ) 031 { 032 boolean wasWhitespaceLast = true; // thus to trim all trailing spaces 033 for( int c = b.length() - 1; c >= 0; --c ) 034 { 035 final char ch = b.charAt( c ); 036 final boolean isWhitespace = Character.isWhitespace( ch ); 037 if( isWhitespace ) 038 { 039 if( wasWhitespaceLast ) b.deleteCharAt( c ); // collapse all whitespace 040 else if( ch != ' ' ) b.setCharAt( c, ' ' ); // to single space char 041 } 042 wasWhitespaceLast = isWhitespace; 043 } 044 if( b.length() > 0 && wasWhitespaceLast ) b.deleteCharAt( 0 ); // trim leading space, if any (can only be one left) 045 046 return b; 047 } 048 049 050 051 /** Trims all whitepace from the ends. 052 * 053 * @return the same string builder. 054 */ 055 public static StringBuilder trim( final StringBuilder b ) 056 { 057 for( ;; ) 058 { 059 final int length = b.length(); 060 if( length == 0 ) return b; 061 062 final char ch = b.charAt( 0 ); 063 if( !Character.isWhitespace( ch )) 064 { 065 if( length == 1 ) return b; 066 067 break; 068 } 069 070 b.deleteCharAt( 0 ); 071 } 072 073 for( ;; ) 074 { 075 final int last = b.length() - 1; 076 final char ch = b.charAt( last ); 077 if( !Character.isWhitespace( ch )) break; 078 079 b.deleteCharAt( last ); 080 if( last == 0 ) break; 081 } 082 083 return b; 084 } 085 086 087 088}