commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r905162 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/text/StrBuilder.java test/java/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
Date Mon, 01 Feb 2010 01:02:44 GMT
Author: niallp
Date: Mon Feb  1 01:02:44 2010
New Revision: 905162

URL: http://svn.apache.org/viewvc?rev=905162&view=rev
Log:
Port LANG-422 to 2.x branch - add appendSeparator() methods with an alternative default separator
if the StrBuilder is currently empty

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrBuilder.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrBuilder.java?rev=905162&r1=905161&r2=905162&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrBuilder.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrBuilder.java
Mon Feb  1 01:02:44 2010
@@ -1066,8 +1066,40 @@
      * @since 2.3
      */
     public StrBuilder appendSeparator(String separator) {
-        if (separator != null && size() > 0) {
-            append(separator);
+        return appendSeparator(separator, null);
+    }
+
+    /**
+     * Appends one of both separators to the StrBuilder.
+     * If the builder is currently empty it will append the defaultIfEmpty-separator
+     * Otherwise it will append the standard-separator
+     * 
+     * Appending a null separator will have no effect.
+     * The separator is appended using {@link #append(String)}.
+     * <p>
+     * This method is for example useful for constructing queries
+     * <pre>
+     * StrBuilder whereClause = new StrBuilder();
+     * if(searchCommand.getPriority() != null) {
+     *  whereClause.appendSeparator(" and", " where");
+     *  whereClause.append(" priority = ?")
+     * }
+     * if(searchCommand.getComponent() != null) {
+     *  whereClause.appendSeparator(" and", " where");
+     *  whereClause.append(" component = ?")
+     * }
+     * selectClause.append(whereClause)
+     * </pre>
+     * 
+     * @param standard the separator if builder is not empty, null means no separator
+     * @param defaultIfEmpty the separator if builder is empty, null means no separator
+     * @return this, to enable chaining
+     * @since 3.0
+     */
+    public StrBuilder appendSeparator(String standard, String defaultIfEmpty) {
+        String str = isEmpty() ? defaultIfEmpty : standard;
+        if (str != null) {
+            append(str);
         }
         return this;
     }
@@ -1099,6 +1131,26 @@
     }
 
     /**
+     * Append one of both separators to the builder
+     * If the builder is currently empty it will append the defaultIfEmpty-separator
+     * Otherwise it will append the standard-separator
+     *
+     * The separator is appended using {@link #append(char)}.
+     * @param standard the separator if builder is not empty
+     * @param defaultIfEmpty the separator if builder is empty
+     * @return this, to enable chaining
+     * @since 3.0
+     */
+    public StrBuilder appendSeparator(char standard, char defaultIfEmpty) {
+        if (size() > 0) {
+            append(standard);
+        }
+        else {
+            append(defaultIfEmpty);
+        }
+        return this;
+    }
+    /**
      * Appends a separator to the builder if the loop index is greater than zero.
      * Appending a null separator will have no effect.
      * The separator is appended using {@link #append(String)}.

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java?rev=905162&r1=905161&r2=905162&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
Mon Feb  1 01:02:44 2010
@@ -970,6 +970,28 @@
         sb.appendSeparator(",");
         assertEquals("foo,", sb.toString());
     }
+    
+    //-----------------------------------------------------------------------
+    public void testAppendSeparator_String_String() {
+        StrBuilder sb = new StrBuilder();
+        final String startSeparator = "order by ";
+        final String standardSeparator = ",";
+        final String foo = "foo";
+        sb.appendSeparator(null, null);
+        assertEquals("", sb.toString());
+        sb.appendSeparator(standardSeparator, null);
+        assertEquals("", sb.toString());
+        sb.appendSeparator(standardSeparator, startSeparator); 
+        assertEquals(startSeparator, sb.toString());
+        sb.appendSeparator(null, null); 
+        assertEquals(startSeparator, sb.toString());
+        sb.appendSeparator(null, startSeparator); 
+        assertEquals(startSeparator, sb.toString());
+        sb.append(foo);
+        assertEquals(startSeparator + foo, sb.toString());
+        sb.appendSeparator(standardSeparator, startSeparator);
+        assertEquals(startSeparator + foo + standardSeparator, sb.toString());
+    }
 
     //-----------------------------------------------------------------------
     public void testAppendSeparator_char() {
@@ -981,6 +1003,18 @@
         sb.appendSeparator(',');
         assertEquals("foo,", sb.toString());
     }
+    public void testAppendSeparator_char_char() {
+        StrBuilder sb = new StrBuilder();
+        final char startSeparator = ':';
+        final char standardSeparator = ',';
+        final String foo = "foo";
+        sb.appendSeparator(standardSeparator, startSeparator);  // no effect
+        assertEquals(String.valueOf(startSeparator), sb.toString());
+        sb.append(foo);
+        assertEquals(String.valueOf(startSeparator) + foo, sb.toString());
+        sb.appendSeparator(standardSeparator, startSeparator);
+        assertEquals(String.valueOf(startSeparator) + foo + standardSeparator, sb.toString());
+    }
 
     //-----------------------------------------------------------------------
     public void testAppendSeparator_String_int() {
@@ -1333,5 +1367,4 @@
         sb.insert(0, (char[]) null, 0, 0);
         assertEquals("nullnullfoonullbarbaz", sb.toString());
     }
-
 }



Mime
View raw message