You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2016/05/21 07:45:48 UTC

svn commit: r1744872 - /directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/Strings.java

Author: elecharny
Date: Sat May 21 07:45:48 2016
New Revision: 1744872

URL: http://svn.apache.org/viewvc?rev=1744872&view=rev
Log:
Removed some Sonar warnings

Modified:
    directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/Strings.java

Modified: directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/Strings.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/Strings.java?rev=1744872&r1=1744871&r2=1744872&view=diff
==============================================================================
--- directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/Strings.java (original)
+++ directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/Strings.java Sat May 21 07:45:48 2016
@@ -208,6 +208,27 @@ public final class Strings
             0, 0, 0, 0, 0, 0, 0, 0
     };
 
+    /** The ASCI chars */
+    private static final byte[] UTF8 = new byte[]
+        {   
+            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
+            0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 
+            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 
+            0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 
+            0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 
+            0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 
+            0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 
+            0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 
+            0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 
+            0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 
+            0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 
+            0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 
+            0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 
+            0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 
+            0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 
+            0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F }
+    ;
+
     /** An empty byte array */
     public static final byte[] EMPTY_BYTES = new byte[0];
     
@@ -236,7 +257,7 @@ public final class Strings
             return "";
         }
 
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
 
         for ( int i = 0; i < buffer.length; i++ )
         {
@@ -301,8 +322,9 @@ public final class Strings
         }
 
         char[] str = new char[buffer.length << 1];
+        int pos = 0;
 
-        for ( int i = 0, pos = 0; i < buffer.length; i++ )
+        for ( int i = 0; i < buffer.length; i++ )
         {
             str[pos++] = ( char ) ( HEX_CHAR[( buffer[i] & 0x00F0 ) >> 4] );
             str[pos++] = ( char ) ( HEX_CHAR[buffer[i] & 0x000F] );
@@ -379,14 +401,14 @@ public final class Strings
             }
         }
 
-        return ( pos == 0 ? "" : new String( newbuf, 0, ( wsSeen ? pos - 1 : pos ) ) );
+        return pos == 0 ? "" : new String( newbuf, 0, wsSeen ? pos - 1 : pos );
     }
 
 
     /**
      * This does the same thing as a trim but we also lowercase the string while
      * performing the deep trim within the same buffer. This saves us from
-     * having to create multiple String and StringBuffer objects and is much
+     * having to create multiple String and StringBuilder objects and is much
      * more efficient.
      *
      * @see Strings#deepTrim( String )
@@ -472,8 +494,6 @@ public final class Strings
      */
     public static String centerTrunc( String str, int head, int tail )
     {
-        StringBuffer buf = null;
-
         // Return as-is if String is smaller than or equal to the head plus the
         // tail plus the number of characters added to the trunc representation
         // plus the number of digits in the string length.
@@ -482,11 +502,12 @@ public final class Strings
             return str;
         }
 
-        buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
         buf.append( '[' ).append( str.length() ).append( "][" );
         buf.append( str.substring( 0, head ) ).append( "..." );
         buf.append( str.substring( str.length() - tail ) );
         buf.append( ']' );
+        
         return buf.toString();
     }
 
@@ -499,7 +520,7 @@ public final class Strings
      */
     public static String toHexString( byte[] res )
     {
-        StringBuffer buf = new StringBuffer( res.length << 1 );
+        StringBuilder buf = new StringBuilder( res.length << 1 );
 
         for ( int ii = 0; ii < res.length; ii++ )
         {
@@ -553,7 +574,7 @@ public final class Strings
     public static String formatHtml( String source, boolean replaceNl, boolean replaceTag,
         boolean replaceQuote )
     {
-        StringBuffer buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
         int len = source.length();
 
         for ( int ii = 0; ii < len; ii++ )
@@ -678,7 +699,7 @@ public final class Strings
         }
         else
         {
-            return ( byteArray[index] == car );
+            return byteArray[index] == car;
         }
     }
 
@@ -701,7 +722,7 @@ public final class Strings
         }
         else
         {
-            return ( charArray[index] == car );
+            return charArray[index] == car;
         }
     }
 
@@ -733,16 +754,6 @@ public final class Strings
         }
     }
 
-    private static final byte[] UTF8 = new byte[]
-        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
-            0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
-            0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
-            0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40,
-            0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52,
-            0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64,
-            0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
-            0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F };
-
 
     /**
      * Return an UTF-8 encoded String
@@ -1021,7 +1032,7 @@ public final class Strings
      */
     public static String trim( String str )
     {
-        return ( isEmpty( str ) ? "" : str.trim() );
+        return isEmpty( str ) ? "" : str.trim();
     }
 
 
@@ -1104,7 +1115,7 @@ public final class Strings
             start++;
         }
 
-        return ( start == 0 ? str : str.substring( start ) );
+        return start == 0 ? str : str.substring( start );
     }
 
 
@@ -1288,7 +1299,7 @@ public final class Strings
             end--;
         }
 
-        return ( end == length ? str : str.substring( 0, end ) );
+        return end == length ? str : str.substring( 0, end );
     }
 
 
@@ -1331,7 +1342,7 @@ public final class Strings
             end--;
         }
 
-        return ( end == length ? str : str.substring( 0, end ) );
+        return end == length ? str : str.substring( 0, end );
     }
 
 
@@ -1406,7 +1417,7 @@ public final class Strings
             pos.end--;
         }
 
-        return ( pos.end == string.length() ? string : string.substring( 0, pos.end ) );
+        return pos.end == string.length() ? string : string.substring( 0, pos.end );
     }
 
 
@@ -1698,7 +1709,7 @@ public final class Strings
      */
     public static String listToString( List<?> list )
     {
-        if ( ( list == null ) || ( list.size() == 0 ) )
+        if ( ( list == null ) || list.isEmpty() )
         {
             return "";
         }
@@ -1732,7 +1743,7 @@ public final class Strings
      */
     public static String setToString( Set<?> set )
     {
-        if ( ( set == null ) || ( set.size() == 0 ) )
+        if ( ( set == null ) || set.isEmpty() )
         {
             return "";
         }
@@ -1767,12 +1778,12 @@ public final class Strings
      */
     public static String listToString( List<?> list, String tabs )
     {
-        if ( ( list == null ) || ( list.size() == 0 ) )
+        if ( ( list == null ) || list.isEmpty() )
         {
             return "";
         }
 
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
 
         for ( Object elem : list )
         {
@@ -1799,7 +1810,7 @@ public final class Strings
             return "";
         }
 
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
         boolean isFirst = true;
 
         for ( Map.Entry<?, ?> entry : map.entrySet() )
@@ -1836,7 +1847,7 @@ public final class Strings
             return "";
         }
 
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
 
         for ( Map.Entry<?, ?> entry : map.entrySet() )
         {
@@ -1857,7 +1868,7 @@ public final class Strings
      *
      * @param value The String to lowercase
      * @return The lowercase string
-     * @deprecated Use {@link #toLowerCaseAscii(String)}
+     * @Deprecated Use {@link #toLowerCaseAscii(String)}
      */
     public static String toLowerCase( String value )
     {
@@ -1934,7 +1945,7 @@ public final class Strings
      *
      * @param value The String to uppercase
      * @return The uppercase string
-     * @deprecated Use {@link toUpperCaseAscii(String)}
+     * @Deprecated Use {@link toUpperCaseAscii(String)}
      */
     public static String toUpperCase( String value )
     {
@@ -2194,7 +2205,7 @@ public final class Strings
         }
 
         char[] hex = encodeHex( bytes );
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
         sb.append( hex, 0, 8 );
         sb.append( '-' );
         sb.append( hex, 8, 4 );
@@ -2205,7 +2216,7 @@ public final class Strings
         sb.append( '-' );
         sb.append( hex, 20, 12 );
 
-        return Strings.toLowerCase( sb.toString() );
+        return Strings.toLowerCaseAscii( sb.toString() );
     }
 
 
@@ -2339,7 +2350,7 @@ public final class Strings
      * @return the parsed value.
      * @throws NumberFormatException If we don't have a number
      */
-    public static int parseInt( String value ) throws NumberFormatException
+    public static int parseInt( String value )
     {
         long res = 0;