You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2020/05/08 18:14:27 UTC

svn commit: r1877518 - in /pivot/trunk/core/src/org/apache/pivot/util: ListenerList.java StringUtils.java

Author: rwhitcomb
Date: Fri May  8 18:14:27 2020
New Revision: 1877518

URL: http://svn.apache.org/viewvc?rev=1877518&view=rev
Log:
PIVOT-1032: Fix style errors in ListenerList; make helper methods in StringUtils
to construct an array representation of an Iterable, and use that in ListenerList.toString().


Modified:
    pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java
    pivot/trunk/core/src/org/apache/pivot/util/StringUtils.java

Modified: pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java?rev=1877518&r1=1877517&r2=1877518&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java Fri May  8 18:14:27 2020
@@ -27,10 +27,16 @@ import org.apache.pivot.annotations.Unsu
  * thread safe. Subclasses that require thread-safe access should synchronize
  * method access appropriately. Callers must manually synchronize on the
  * listener list instance to ensure thread safety during iteration.
+ *
+ * @param <T> The listener type contained in this list.
  */
 public abstract class ListenerList<T> implements Iterable<T> {
 
-    // Iterator through the current array of elements
+    /**
+     * Iterator through the current array of elements.
+     *
+     * @param <T> The listener type to iterate over.
+     */
     private class NodeIterator implements Iterator<T> {
         private int index;
 
@@ -74,7 +80,7 @@ public abstract class ListenerList<T> im
      *
      * @param listener New listener to add to the list.
      */
-    public void add(T listener) {
+    public void add(final T listener) {
         if (indexOf(listener) >= 0) {
             System.err.println("Duplicate listener " + listener + " added to " + this);
             return;
@@ -94,7 +100,7 @@ public abstract class ListenerList<T> im
      * @param index The 0-based position in the list where to add the new listener.
      * @param listener New listener to add there.
      */
-    public void add(int index, T listener) {
+    public void add(final int index, final T listener) {
         Utils.checkZeroBasedIndex(index, last);
 
         if (indexOf(listener) >= 0) {
@@ -128,7 +134,7 @@ public abstract class ListenerList<T> im
      *
      * @param listener The listener to remove from the list.
      */
-    public void remove(T listener) {
+    public void remove(final T listener) {
         int index = indexOf(listener);
 
         if (index < 0) {
@@ -163,7 +169,7 @@ public abstract class ListenerList<T> im
      * @return <tt>true</tt> if the listener exists in the list; <tt>false</tt>,
      * otherwise.
      */
-    public boolean contains(T listener) {
+    public boolean contains(final T listener) {
         return indexOf(listener) >= 0;
     }
 
@@ -193,7 +199,7 @@ public abstract class ListenerList<T> im
      * @return The element at position <tt>index</tt>.
      * @throws IndexOutOfBoundsException if the index is out of range.
      */
-    public T get(int index) {
+    public T get(final int index) {
         Utils.checkZeroBasedIndex(index, last);
         return list[index];
     }
@@ -208,19 +214,9 @@ public abstract class ListenerList<T> im
         StringBuilder sb = new StringBuilder();
 
         sb.append(getClass().getName());
-        sb.append(" [");
-
-        int i = 0;
-        for (T item : this) {
-            if (i > 0) {
-                sb.append(", ");
-            }
-
-            sb.append(item);
-            i++;
-        }
+        sb.append(" ");
 
-        sb.append("]");
+        StringUtils.append(sb, this);
 
         return sb.toString();
     }

Modified: pivot/trunk/core/src/org/apache/pivot/util/StringUtils.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/StringUtils.java?rev=1877518&r1=1877517&r2=1877518&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/StringUtils.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/StringUtils.java Fri May  8 18:14:27 2020
@@ -170,5 +170,49 @@ public final class StringUtils {
         }
     }
 
+    /**
+     * Given an iterable list of items, construct a string representation of the list
+     * that looks like:
+     * <pre>[item1, item2, ...]</pre>.
+     *
+     * @param <T> The type of items in the list.
+     * @param list The iterable list of items.
+     * @return A string representation of the list.
+     */
+    public static <T> String toString(final Iterable<T> list) {
+        StringBuilder sb = new StringBuilder();
+        append(sb, list);
+        return sb.toString();
+    }
+
+    /**
+     * Given an iterable list of items, construct a string representation of the list
+     * that looks like:
+     * <pre>[item1, item2, ...]</pre>
+     * appending the results to the given string builder for further use.
+     *
+     * @param <T> The type of items in the list.
+     * @param sb The {@link StringBuilder} already in progress.
+     * @param list The iterable list of items.
+     * @return The input {@code StringBuilder} for further use.
+     */
+    public static <T> StringBuilder append(final StringBuilder sb, final Iterable<T> list) {
+        sb.append("[");
+
+        int i = 0;
+        for (T item : list) {
+            if (i > 0) {
+                sb.append(", ");
+            }
+
+            sb.append(item);
+            i++;
+        }
+
+        sb.append("]");
+
+        return sb;
+    }
+
 }