First, many thanks for the cleanup work.

One thing to bear in mind for the loop changes is that in some cases the underlying collections may be changing as the loops progress. In theory, unit tests should pick up any problems introduced, but we should look carefully at this.

Phil

On 12/4/20 6:15 AM, ebo...@apache.org wrote:
This is an automated email from the ASF dual-hosted git repository.

ebourg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git

commit 309e204345a7b5181c883b6aa2d8497313f625ef
Author: Emmanuel Bourg <ebo...@apache.org>
AuthorDate: Fri Dec 4 13:32:19 2020 +0100

     Use for-each loops
---
  .../java/org/apache/commons/dbcp2/DelegatingConnection.java  |  5 +----
  .../dbcp2/datasources/InstanceKeyDataSourceFactory.java      | 12 +++---------
  2 files changed, 4 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java 
b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
index bdceb41..e7aeb61 100644
--- a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
+++ b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
@@ -36,7 +36,6 @@ import java.sql.Statement;
  import java.sql.Struct;
  import java.util.ArrayList;
  import java.util.Collections;
-import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Properties;
@@ -620,9 +619,7 @@ public class DelegatingConnection<C extends Connection> 
extends AbandonedTrace i
          final List<AbandonedTrace> traces = getTrace();
          if (traces != null && !traces.isEmpty()) {
              final List<Exception> thrownList = new ArrayList<>();
-            final Iterator<AbandonedTrace> traceIter = traces.iterator();
-            while (traceIter.hasNext()) {
-                final Object trace = traceIter.next();
+            for (Object trace : traces) {
                  if (trace instanceof Statement) {
                      try {
                          ((Statement) trace).close();
diff --git 
a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
 
b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
index f6ee923..8be3df3 100644
--- 
a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
+++ 
b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
@@ -21,7 +21,6 @@ import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.util.ArrayList;
  import java.util.Hashtable;
-import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Map.Entry;
@@ -47,9 +46,7 @@ abstract class InstanceKeyDataSourceFactory implements 
ObjectFactory {
static synchronized String registerNewInstance(final InstanceKeyDataSource ds) {
          int max = 0;
-        final Iterator<String> iterator = instanceMap.keySet().iterator();
-        while (iterator.hasNext()) {
-            final String s = iterator.next();
+        for (String s : instanceMap.keySet()) {
              if (s != null) {
                  try {
                      max = Math.max(max, Integer.parseInt(s));
@@ -84,13 +81,10 @@ abstract class InstanceKeyDataSourceFactory implements 
ObjectFactory {
      public static void closeAll() throws Exception {
          // Get iterator to loop over all instances of this data source.
          final List<Throwable> exceptionList = new 
ArrayList<>(instanceMap.size());
-        final Iterator<Entry<String, InstanceKeyDataSource>> instanceIterator 
= instanceMap.entrySet().iterator();
-        while (instanceIterator.hasNext()) {
+        for (Entry<String, InstanceKeyDataSource> next : 
instanceMap.entrySet()) {
              // Bullet-proof to avoid anything else but problems from 
InstanceKeyDataSource#close().
-            final Entry<String, InstanceKeyDataSource> next = 
instanceIterator.next();
              if (next != null) {
-                @SuppressWarnings("resource")
-                final InstanceKeyDataSource value = next.getValue();
+                @SuppressWarnings("resource") final InstanceKeyDataSource 
value = next.getValue();
                  if (value != null) {
                      try {
                          value.close();



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to