You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2005/02/26 23:16:18 UTC

svn commit: r155594 - in incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator: ArrayIterator.java AtomicObjectIterator.java EmptyIterator.java EnumerationIterator.java MapIterator.java ResultSetIterator.java

Author: ekoneil
Date: Sat Feb 26 14:16:15 2005
New Revision: 155594

URL: http://svn.apache.org/viewcvs?view=rev&rev=155594
Log:
Iterator factory cleanup; includes fix for:
 
  BEEHIVE-180 ResultSetIterator needs to support case insensitive comparison for Map.get(String)

BB: self
BVT: NetUI pass
  


Removed:
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/EmptyIterator.java
Modified:
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ArrayIterator.java
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/AtomicObjectIterator.java
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/EnumerationIterator.java
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/MapIterator.java
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ArrayIterator.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ArrayIterator.java?view=diff&r1=155593&r2=155594
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ArrayIterator.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ArrayIterator.java Sat Feb 26 14:16:15 2005
@@ -17,19 +17,13 @@
  */
 package org.apache.beehive.netui.util.iterator;
 
-// java imports
-
 import java.lang.reflect.Array;
-
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
-// internal imports
 import org.apache.beehive.netui.util.Bundle;
 import org.apache.beehive.netui.util.logging.Logger;
 
-// external imports
-
 /**
  * <code>ArrayIterator</code> provides an <code>Iterator</code> over a Java
  * array. <code>ArrayIterator</code> will return each element in the array
@@ -60,14 +54,14 @@
     private int _currentElement;
 
     public ArrayIterator(Object array) {
-        if(array != null) {
-            if(!array.getClass().isArray())
-            // throw new IllegalStateException("Can not create an array iterator from an object that is not an array");
-                throw new IllegalStateException(Bundle.getErrorString("ArrayIterator_notAnArray"));
+        if(array == null)
+            return;
 
-            _array = array;
-            _totalElements = Array.getLength(_array);
-        }
+        if(!array.getClass().isArray())
+            throw new IllegalStateException(Bundle.getErrorString("ArrayIterator_notAnArray"));
+
+        _array = array;
+        _totalElements = Array.getLength(_array);
     }
 
     public boolean hasNext() {
@@ -76,28 +70,18 @@
         else {
             if(_currentElement < _totalElements)
                 return true;
-            else
-                return false;
+            else return false;
         }
     }
 
     public Object next() {
-        if(logger.isDebugEnabled()) {
-            logger.debug("Current element: " + _currentElement);
-            logger.debug("Total elements: " + _totalElements);
-        }
-
         if(_currentElement >= _totalElements)
             throw new NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
 
         Object nextElement = null;
-
         try {
             nextElement = Array.get(_array, _currentElement);
         } catch(Exception e) {
-            if(logger.isDebugEnabled())
-                logger.debug("Caught exception: " + e.getClass().getName());
-
             throw new NoSuchElementException(Bundle.getErrorString("ArrayIterator_arrayError", new Object[]{_array.getClass().getName()}));
         }
 

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/AtomicObjectIterator.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/AtomicObjectIterator.java?view=diff&r1=155593&r2=155594
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/AtomicObjectIterator.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/AtomicObjectIterator.java Sat Feb 26 14:16:15 2005
@@ -17,16 +17,11 @@
  */
 package org.apache.beehive.netui.util.iterator;
 
-// java imports
-
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
-// internal imports
 import org.apache.beehive.netui.util.Bundle;
 
-// external imports
-
 /**
  */
 public class AtomicObjectIterator
@@ -57,8 +52,8 @@
         if(!_nextCalled && _object != null) {
             _nextCalled = true;
             return _object;
-        } else
-            throw new NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
+        }
+        else throw new NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
     }
 
     public void remove() {

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/EnumerationIterator.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/EnumerationIterator.java?view=diff&r1=155593&r2=155594
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/EnumerationIterator.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/EnumerationIterator.java Sat Feb 26 14:16:15 2005
@@ -17,20 +17,14 @@
  */
 package org.apache.beehive.netui.util.iterator;
 
-// java imports
-
 import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
-// internal imports
 import org.apache.beehive.netui.util.Bundle;
 
-// external imports
-
 /**
- * An {@link java.util.Iterator} implementation for rendering data
- * from an {@link java.util.Enumeration}.
+ * An {@link java.util.Iterator} implementation an {@link java.util.Enumeration}.
  */
 public class EnumerationIterator
     implements Iterator {

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/MapIterator.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/MapIterator.java?view=diff&r1=155593&r2=155594
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/MapIterator.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/MapIterator.java Sat Feb 26 14:16:15 2005
@@ -17,17 +17,12 @@
  */
 package org.apache.beehive.netui.util.iterator;
 
-// java imports
-
 import java.util.Iterator;
 import java.util.Map;
 import java.util.NoSuchElementException;
 
-// internal imports
 import org.apache.beehive.netui.util.Bundle;
 
-// external imports
-
 /**
  * <code>MapIterator</code> provides an <code>Iterator</code> over map values.
  * <code>MapIterator</code> iterates over the sequence of values stored in the
@@ -37,33 +32,32 @@
     implements Iterator {
 
     /**
-     * An iterator over the map's values
+     * An iterator for the map's values.
      */
     private Iterator _mapIterator = null;
 
     public MapIterator(Map map) {
-        if(map != null)
-            _mapIterator = map.values().iterator();
+        if(map == null)
+            return;
+
+        _mapIterator = map.values().iterator();
     }
 
     public boolean hasNext() {
         if(_mapIterator == null)
             return false;
-        else
-            return _mapIterator.hasNext();
+        else return _mapIterator.hasNext();
     }
 
     public Object next() {
         if(_mapIterator == null)
             throw new NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
-        else
-            return _mapIterator.next();
+        else return _mapIterator.next();
     }
 
     public void remove() {
         if(_mapIterator == null)
             throw new UnsupportedOperationException(Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported", new Object[]{this.getClass().getName()}));
-        else
-            _mapIterator.remove();
+        else _mapIterator.remove();
     }
 }

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java?view=diff&r1=155593&r2=155594
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java Sat Feb 26 14:16:15 2005
@@ -17,22 +17,18 @@
  */
 package org.apache.beehive.netui.util.iterator;
 
-// java imports
-
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
+import java.util.SortedMap;
+import java.util.TreeMap;
 
-// internal imports
 import org.apache.beehive.netui.util.Bundle;
 import org.apache.beehive.netui.util.logging.Logger;
 
-// external imports
-
 /**
  *
  */
@@ -41,93 +37,79 @@
 
     private static final Logger LOGGER = Logger.getInstance(ResultSetIterator.class);
 
-    private ResultSet _rs = null;
-    private boolean _calledHasNext = false;
-    private boolean _hasNext = false;
     private String[] _columnNames = null;
+    private ResultSet _rs = null;
 
     public ResultSetIterator(ResultSet rs) {
-        assert rs != null;
+        if(rs == null)
+            return;
+
         _rs = rs;
 
         try {
             // handle RSMD here to build a template map that can contain the data for each row
-            ResultSetMetaData _rsmd = _rs.getMetaData();
-            assert _rsmd != null;
+            ResultSetMetaData rsmd = _rs.getMetaData();
+            assert rsmd != null;
 
-            int cols = _rsmd.getColumnCount();
+            int cols = rsmd.getColumnCount();
             _columnNames = new String[cols];
             for(int i = 1; i <= cols; i++) {
-                String col = _rsmd.getColumnName(i);
-                if(LOGGER.isDebugEnabled()) LOGGER.debug("found column: " + col);
-                _columnNames[i - 1] = col;
+                _columnNames[i - 1] = rsmd.getColumnName(i);
+                LOGGER.trace("column[" + i + "]: " + _columnNames[i-1]);
             }
         } catch(SQLException sql) {
-            if(LOGGER.isErrorEnabled()) LOGGER.error("An exception occurred reading ResultSetMetaData from a ResultSet.  Cause: " + sql, sql);
-            throw new RuntimeException("An exception occurred reading ResultSetMetaData from a ResultSet.  Cause: " + sql, sql);
+            LOGGER.error("An exception occurred reading ResultSetMetaData from a ResultSet.  Cause: " + sql, sql);
+
+            throw new IllegalStateException("An exception occurred reading ResultSetMetaData from a ResultSet.  Cause: " + sql, sql);
         }
     }
 
     /**
-     * @throws SQLException if an exception occurs calling methods on the ResultSet
+     *
      */
     public boolean hasNext() {
-        if(!_calledHasNext) {
-            _calledHasNext = true;
-            try {
-                _hasNext = _rs.next();
-            } catch(SQLException sql) {
-                if(LOGGER.isErrorEnabled()) LOGGER.error("An exception occurred reading from a ResultSet.  Cause: " + sql, sql);
-                throw new RuntimeException("An exception occurred reading from a ResultSet.  Cause: " + sql, sql);
-            }
-        }
-
-        if(LOGGER.isDebugEnabled()) LOGGER.debug("hasNext: " + _hasNext);
+        if(_rs == null)
+            return false;
 
-        return _hasNext;
+        try {
+            return !_rs.isLast();
+        }
+        catch(SQLException sql) {
+            LOGGER.error("An exception occurred reading from the Iterator.  Cause: " + sql, sql);
+            throw new RuntimeException("An exception occurred reading from the Iterator.  Cause: " + sql, sql);
+        }
     }
 
     /**
      *
      */
     public Object next() {
-        if(_hasNext == false)
+        if(_rs == null)
             throw new NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
 
-        // when next() is called several times in succession, the ResultSet needs to be advanced
-        if(!_calledHasNext) {
-            try {
-                _hasNext = _rs.next();
-            } catch(SQLException sql) {
-                if(LOGGER.isErrorEnabled()) LOGGER.error("An exception occurred reading from a ResultSet.  Cause: " + sql, sql);
-                throw new RuntimeException("An exception occurred reading from a ResultSet.  Cause: " + sql, sql);
-            }
-
-            if(!_hasNext)
+        try {
+            if(_rs.isLast())
                 throw new NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
-        }
-
-        // called hasNext() next().  reset the flag
-        _calledHasNext = false;
 
-        HashMap row = new HashMap();
-        assert _columnNames != null;
-        for(int i = 0; i < _columnNames.length; i++) {
-            String key = (String)_columnNames[i];
-
-            try {
-                Object value = _rs.getObject(key);
-                row.put(key, value);
-            } catch(SQLException sql) {
-                if(LOGGER.isErrorEnabled()) LOGGER.error("An exception occurred reading from a ResultSet.  Cause: " + sql, sql);
-                throw new RuntimeException("An exception occurred reading from a ResultSet.  Cause: " + sql, sql);
+            /* not currently on the last row, should be safe to advance */
+            _rs.next();
+            SortedMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
+            for(int i = 1; i <= _columnNames.length; i++) {
+                Object value = _rs.getObject(i);
+                if(_rs.wasNull())
+                    value = null;
+                map.put(_columnNames[i], value);
             }
-        }
 
-        return row;
+            return map;
+        }
+        catch(SQLException sql) {
+            throw new RuntimeException("An exception occurred reading from the Iterator.  Cause: " + sql, sql);
+        }
     }
 
     public void remove() {
-        throw new UnsupportedOperationException(Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported", new Object[]{this.getClass().getName()}));
+        throw new UnsupportedOperationException(Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported",
+                                                new Object[]{this.getClass().getName()}));
     }
 }