commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r826969 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/text/StrBuilder.java test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
Date Tue, 20 Oct 2009 06:10:11 GMT
Author: bayard
Date: Tue Oct 20 06:10:10 2009
New Revision: 826969

URL: http://svn.apache.org/viewvc?rev=826969&view=rev
Log:
Applying Robert Scholte's patch from LANG-422, adding a appendSeparator with an alternative
default separator if the StrBuilder is currently empty

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java?rev=826969&r1=826968&r2=826969&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java Tue Oct
20 06:10:10 2009
@@ -68,6 +68,7 @@
  * the interface. 
  *
  * @author Stephen Colebourne
+ * @author Robert Scholte
  * @since 2.2
  * @version $Id$
  */
@@ -1096,8 +1097,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;
     }
@@ -1129,6 +1162,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/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java?rev=826969&r1=826968&r2=826969&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
(original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
Tue Oct 20 06:10:10 2009
@@ -993,6 +993,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() {
@@ -1004,6 +1026,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() {
@@ -1356,5 +1390,4 @@
         sb.insert(0, (char[]) null, 0, 0);
         assertEquals("nullnullfoonullbarbaz", sb.toString());
     }
-
 }



Mime
View raw message