commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1021864 - in /commons/proper/pool/trunk/src: java/org/apache/commons/pool/impl/ test/org/apache/commons/pool/
Date Tue, 12 Oct 2010 17:59:13 GMT
Author: ggregory
Date: Tue Oct 12 17:59:13 2010
New Revision: 1021864

URL: http://svn.apache.org/viewvc?rev=1021864&view=rev
Log:
Use Java 5's enhanced for loops (with Generics)

Modified:
    commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
    commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
    commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
    commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackKeyedObjectPool.java
    commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java
    commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java?rev=1021864&r1=1021863&r2=1021864&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
Tue Oct 12 17:59:13 2010
@@ -1317,14 +1317,13 @@ public class GenericKeyedObjectPool<K,V>
         // build sorted map of idle objects
         final Map<ObjectTimestampPair<V>, K> map = new TreeMap<ObjectTimestampPair<V>,
K>();
         synchronized (this) {
-            for (Iterator<K> keyiter = _poolMap.keySet().iterator(); keyiter.hasNext();)
{
-                final K key = keyiter.next();
+            for (K key : _poolMap.keySet()) {
                 final CursorableLinkedList<ObjectTimestampPair<V>> list = _poolMap.get(key).queue;
-                for (Iterator<ObjectTimestampPair<V>> it = list.iterator(); it.hasNext();)
{
+                for (ObjectTimestampPair<V> pair : list) {
                     // each item into the map uses the objectimestamppair object
                     // as the key.  It then gets sorted based on the timstamp field
                     // each value in the map is the parent list it belongs in.
-                    map.put(it.next(), key);
+                    map.put(pair, key);
                 }
             }
 
@@ -1401,24 +1400,22 @@ public class GenericKeyedObjectPool<K,V>
      * @param m Map containing keyed pools to clear
      * @param factory KeyedPoolableObjectFactory used to destroy the objects
      */
-    private void destroy(Map<K, List<ObjectTimestampPair<V>>> m, KeyedPoolableObjectFactory<K,V>
factory) {
-        for (Iterator<Map.Entry<K, List<ObjectTimestampPair<V>>>>
entries = m.entrySet().iterator(); entries.hasNext();) {
-            Map.Entry<K, List<ObjectTimestampPair<V>>> entry = entries.next();
+    private void destroy(Map<K, List<ObjectTimestampPair<V>>> m, KeyedPoolableObjectFactory<K,
V> factory) {
+        for (Map.Entry<K, List<ObjectTimestampPair<V>>> entry : m.entrySet())
{
             K key = entry.getKey();
             Collection<ObjectTimestampPair<V>> c = entry.getValue();
-            for (Iterator<ObjectTimestampPair<V>> it = c.iterator(); it.hasNext();)
{
+            for (ObjectTimestampPair<V> pair : c) {
                 try {
-                    factory.destroyObject(key,it.next().getValue());
-                } catch(Exception e) {
+                    factory.destroyObject(key, pair.getValue());
+                } catch (Exception e) {
                     // ignore error, keep destroying the rest
                 } finally {
-                    synchronized(this) {
+                    synchronized (this) {
                         _totalInternalProcessing--;
                         allocate();
                     }
                 }
             }
-
         }
     }
 
@@ -2005,9 +2002,7 @@ public class GenericKeyedObjectPool<K,V>
         StringBuffer buf = new StringBuffer();
         buf.append("Active: ").append(getNumActive()).append("\n");
         buf.append("Idle: ").append(getNumIdle()).append("\n");
-        Iterator<K> it = _poolMap.keySet().iterator();
-        while (it.hasNext()) {
-            K key = it.next();
+        for (K key : _poolMap.keySet()) {
             buf.append("\t").append(key).append(" ").append(_poolMap.get(key)).append("\n");
         }
         return buf.toString();

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericObjectPool.java?rev=1021864&r1=1021863&r2=1021864&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
Tue Oct 12 17:59:13 2010
@@ -1253,13 +1253,13 @@ public class GenericObjectPool<T> extend
      * @param factory PoolableConnectionFactory used to destroy the objects
      */
     private void destroy(Collection<ObjectTimestampPair<T>> c, PoolableObjectFactory<T>
factory) {
-        for (Iterator<ObjectTimestampPair<T>> it = c.iterator(); it.hasNext();)
{
+        for (ObjectTimestampPair<T> pair : c) {
             try {
-                factory.destroyObject(it.next().getValue());
-            } catch(Exception e) {
+                factory.destroyObject(pair.getValue());
+            } catch (Exception e) {
                 // ignore error, keep destroying the rest
             } finally {
-                synchronized(this) {
+                synchronized (this) {
                     _numInternalProcessing--;
                     allocate();
                 }
@@ -1605,11 +1605,9 @@ public class GenericObjectPool<T> extend
         buf.append("Active: ").append(getNumActive()).append("\n");
         buf.append("Idle: ").append(getNumIdle()).append("\n");
         buf.append("Idle Objects:\n");
-        Iterator<ObjectTimestampPair<T>> it = _pool.iterator();
         long time = System.currentTimeMillis();
-        while(it.hasNext()) {
-            ObjectTimestampPair<T> pair = it.next();
-            buf.append("\t").append(pair.getValue()).append("\t").append(time - pair.getTstamp()).append("\n");
+        for (ObjectTimestampPair<T> pair  : _pool) {
+            buf.append("\t").append(pair.getValue()).append("\t").append(time - pair.getTstamp()).append("\n");
           
         }
         return buf.toString();
     }

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java?rev=1021864&r1=1021863&r2=1021864&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
Tue Oct 12 17:59:13 2010
@@ -277,15 +277,14 @@ public class SoftReferenceObjectPool<T> 
      */
     @Override
     public synchronized void clear() {
-        if(null != _factory) {
-            Iterator<SoftReference<T>> iter = _pool.iterator();
-            while(iter.hasNext()) {
+        if (null != _factory) {
+            for (SoftReference<T> element : _pool) {
                 try {
-                    T obj = iter.next().get();
-                    if(null != obj) {
+                    T obj = element.get();
+                    if (null != obj) {
                         _factory.destroyObject(obj);
                     }
-                } catch(Exception e) {
+                } catch (Exception e) {
                     // ignore error, keep destroying the rest
                 }
             }

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackKeyedObjectPool.java?rev=1021864&r1=1021863&r2=1021864&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackKeyedObjectPool.java
Tue Oct 12 17:59:13 2010
@@ -378,11 +378,9 @@ public class StackKeyedObjectPool<K,V> e
      */
     @Override
     public synchronized void clear() {
-        Iterator<K> it = _pools.keySet().iterator();
-        while(it.hasNext()) {
-            K key = it.next();
+        for (K key : _pools.keySet()) {
             Stack<V> stack = _pools.get(key);
-            destroyStack(key,stack);
+            destroyStack(key,stack);            
         }
         _totIdle = 0;
         _pools.clear();
@@ -411,10 +409,9 @@ public class StackKeyedObjectPool<K,V> e
             return;
         } else {
             if(null != _factory) {
-                Iterator<V> it = stack.iterator();
-                while(it.hasNext()) {
+                for (V v : stack) {
                     try {
-                        _factory.destroyObject(key,it.next());
+                        _factory.destroyObject(key, v);
                     } catch(Exception e) {
                         // ignore error, keep destroying the rest
                     }
@@ -437,12 +434,10 @@ public class StackKeyedObjectPool<K,V> e
         StringBuffer buf = new StringBuffer();
         buf.append(getClass().getName());
         buf.append(" contains ").append(_pools.size()).append(" distinct pools: ");
-        Iterator<K> it = _pools.keySet().iterator();
-        while(it.hasNext()) {
-            Object key = it.next();
+        for (Object key : _pools.keySet()) {
             buf.append(" |").append(key).append("|=");
             Stack<V> s = _pools.get(key);
-            buf.append(s.size());
+            buf.append(s.size());            
         }
         return buf.toString();
     }

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java?rev=1021864&r1=1021863&r2=1021864&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java (original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java Tue
Oct 12 17:59:13 2010
@@ -251,13 +251,12 @@ public class StackObjectPool<T> extends 
     @Override
     public synchronized void clear() {
         if(null != _factory) {
-            Iterator<T> it = _pool.iterator();
-            while(it.hasNext()) {
+            for (T element : _pool) {
                 try {
-                    _factory.destroyObject(it.next());
+                    _factory.destroyObject(element);
                 } catch(Exception e) {
                     // ignore error, keep destroying the rest
-                }
+                }                
             }
         }
         _pool.clear();

Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java?rev=1021864&r1=1021863&r2=1021864&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java (original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java Tue Oct
12 17:59:13 2010
@@ -287,12 +287,10 @@ public class TestPoolUtils extends TestC
         assertEquals(2, op.getNumIdle());
         op.close();
         int makeObjectCount = 0;
-        final Iterator<String> iter = calledMethods.iterator();
-        while (iter.hasNext()) {
-            final String methodName = iter.next();
+        for (String methodName : calledMethods) {
             if ("makeObject".equals(methodName)) {
                 makeObjectCount++;
-            }
+            }            
         }
         assertEquals("makeObject should have been called two time", 2, makeObjectCount);
 
@@ -360,9 +358,7 @@ public class TestPoolUtils extends TestC
         assertEquals(2, kop.getNumIdle());
         kop.close();
         int makeObjectCount = 0;
-        final Iterator<String> iter = calledMethods.iterator();
-        while (iter.hasNext()) {
-            final String methodName = iter.next();
+        for (String methodName : calledMethods) {
             if ("makeObject".equals(methodName)) {
                 makeObjectCount++;
             }
@@ -422,9 +418,7 @@ public class TestPoolUtils extends TestC
                 final Map<String,TimerTask> tasks = PoolUtils.checkMinIdle(pool, keys,
1, CHECK_PERIOD); // checks minIdle immediately
 
                 Thread.sleep(CHECK_SLEEP_PERIOD); // will check CHECK_COUNT more times.
-                final Iterator<TimerTask> iter = tasks.values().iterator();
-                while (iter.hasNext()) {
-                    final TimerTask task = iter.next();
+                for (TimerTask task : tasks.values()) {
                     task.cancel();
                 }
 



Mime
View raw message