You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/04/16 14:21:31 UTC

svn commit: r765592 - in /incubator/pivot/trunk: core/src/pivot/serialization/CSVSerializer.java core/src/pivot/serialization/CSVSerializerListener.java wtk/src/pivot/wtk/skin/BorderSkin.java

Author: gbrown
Date: Thu Apr 16 12:21:31 2009
New Revision: 765592

URL: http://svn.apache.org/viewvc?rev=765592&view=rev
Log:
Remove CSVSerializerListener interface - this imposed a "push" event model, which would preclude an abort of the operation by the caller (the new version uses a "pull" model using a stream iterator); remove old comment from BorderSkin.java.

Removed:
    incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializerListener.java
Modified:
    incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/skin/BorderSkin.java

Modified: incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java?rev=765592&r1=765591&r2=765592&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java (original)
+++ incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java Thu Apr 16 12:21:31 2009
@@ -26,6 +26,7 @@
 import java.io.Reader;
 import java.io.Writer;
 import java.nio.charset.Charset;
+import java.util.NoSuchElementException;
 
 import pivot.beans.BeanDictionary;
 import pivot.collections.ArrayList;
@@ -80,6 +81,47 @@
         }
     }
 
+    /**
+     * Allows a caller to retrieve the contents of a CSV stream iteratively.
+     *
+     * @author gbrown
+     */
+    public class StreamIterator {
+        private Reader reader;
+
+        private StreamIterator(Reader reader) throws IOException {
+            this.reader = reader;
+
+            // Move to the first character
+            c = reader.read();
+        }
+
+        public boolean hasNext() {
+            return (c != -1);
+        }
+
+        public Object next() throws IOException, SerializationException {
+            if (c == -1) {
+                throw new NoSuchElementException();
+            }
+
+            Object item = readItem(reader);
+            if (item != null) {
+                // Move to next line
+                while (c != -1
+                    && (c == '\r' || c == '\n')) {
+                    c = reader.read();
+                }
+            }
+
+            return item;
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+
     private Charset charset;
 
     private ArrayList<String> keys = new ArrayList<String>();
@@ -157,7 +199,10 @@
      * @param reader
      * The reader from which data will be read.
      *
-     * @see #readObject(Reader)
+     * @return
+     * A list containing the data read from the CSV file. The list items are
+     * instances of Dictionary<String, Object> populated by mapping columns in
+     * the CSV file to keys in the key sequence.
      */
     public List<?> readObject(Reader reader)
         throws IOException, SerializationException {
@@ -186,57 +231,32 @@
     }
 
     /**
-     * Reads values from a comma-separated value stream and notifies a listener
-     * as items are processed.
+     * Reads values from a comma-separated value stream.
      *
      * @param inputStream
      * The input stream from which data will be read.
      *
-     * @param listener
-     * The listener to notify.
-     *
-     * @see #readObject(Reader, CSVSerializerListener)
+     * @see #getStreamIterator(Reader)
      */
-    public void readObject(InputStream inputStream, CSVSerializerListener listener)
-        throws IOException, SerializationException {
+    public StreamIterator getStreamIterator(InputStream inputStream) throws IOException {
         Reader reader = new BufferedReader(new InputStreamReader(inputStream, charset),
             BUFFER_SIZE);
-        readObject(reader, listener);
+        return getStreamIterator(reader);
     }
 
     /**
-     * Reads values from a comma-separated value stream and notifies a listener
-     * as items are processed. Items are instances of Dictionary<String, Object>
-     * populated by mapping columns in the CSV file to keys in the key sequence.
+     * Reads values from a comma-separated value stream.
      *
      * @param reader
      * The reader from which data will be read.
      *
-     * @param listener
-     * The listener to notify.
+     * @return
+     * A stream iterator on the data read from the CSV file. The list items are
+     * instances of Dictionary<String, Object> populated by mapping columns in
+     * the CSV file to keys in the key sequence.
      */
-    public void readObject(Reader reader, CSVSerializerListener listener)
-        throws IOException, SerializationException {
-        // Move to the first character
-        c = reader.read();
-
-        while (c != -1) {
-            Object item = readItem(reader);
-            while (item != null) {
-                listener.itemRead(this, item);
-
-                // Move to next line
-                while (c != -1
-                    && (c == '\r' || c == '\n')) {
-                    c = reader.read();
-                }
-
-                // Read the next item
-                item = readItem(reader);
-            }
-        }
-
-        listener.allItemsRead(this);
+    public StreamIterator getStreamIterator(Reader reader) throws IOException {
+        return new StreamIterator(reader);
     }
 
     @SuppressWarnings("unchecked")

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/skin/BorderSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/skin/BorderSkin.java?rev=765592&r1=765591&r2=765592&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/skin/BorderSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/skin/BorderSkin.java Thu Apr 16 12:21:31 2009
@@ -39,8 +39,6 @@
 
 /**
  * Border skin.
- * <p>
- * TODO Draw title.
  *
  * @author gbrown
  */