paulk-asert commented on code in PR #2215:
URL: https://github.com/apache/groovy/pull/2215#discussion_r2076520962


##########
src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java:
##########
@@ -1877,6 +1878,77 @@ public static StringBuilder leftShift(final 
StringBuilder self, final Object val
         return self;
     }
 
+    
//--------------------------------------------------------------------------
+    // make StringBuilder behaves like a stack of chars
+
+    /**
+     * Since StringBuilder is a basic dynamic container of chars, sometimes it 
should
+     * behave like a stack of chars at the same time.
+     * stack's methods should have: push(), peek(), pop(), size(), isEmpty()
+     * <pre class="groovyTestCase">
+     * def st = new StringBuilder()
+     * // stack in cases:
+     * st.push('B')
+     * assert st.toString() == 'B'
+     * st.push('a')
+     * assert st.toString() == 'Ba'
+     * st.push('r')
+     * assert st.toString() == 'Bar'
+     * assert st.size() == 3
+     * assert st.isEmpty() == false
+     * // stack out cases:
+     * assert st.peek() == 'r' as char
+     * assert st.pop() == 'r' as char
+     * assert st.toString() == 'Ba'
+     * assert st.peek() == 'a' as char
+     * assert st.pop() == 'a' as char
+     * assert st.toString() == 'B'
+     * assert st.size() == 1
+     * assert st.isEmpty() == false
+     * assert st.pop() == 'B' as char
+     * assert st.isEmpty() == true
+     * </pre>
+     *
+     * @param self a StringBuilder obj
+     * @param ch   a char to push in
+     * @return the StringBuilder obj self
+     * @since 5.0.0
+     */
+    public static StringBuilder push(final StringBuilder self, final char ch) {
+        Objects.requireNonNull(self);
+        self.append(ch);
+        return self;
+    }
+
+    public static StringBuilder push(final StringBuilder self, final String 
ch) {

Review Comment:
   This and the next method could be replaced by a single CharSequence version.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@groovy.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to