Small refactoring of borrowObject() to reduce code duplication.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/pool/trunk@1735162 
13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/commons-pool/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-pool/commit/564334a3
Tree: http://git-wip-us.apache.org/repos/asf/commons-pool/tree/564334a3
Diff: http://git-wip-us.apache.org/repos/asf/commons-pool/diff/564334a3

Branch: refs/heads/master
Commit: 564334a3b2a0c9716c7ada87b9bd790fb44f747e
Parents: fa819eb
Author: Mark Thomas <ma...@apache.org>
Authored: Tue Mar 15 20:27:16 2016 +0000
Committer: Mark Thomas <ma...@apache.org>
Committed: Tue Mar 15 20:27:16 2016 +0000

----------------------------------------------------------------------
 src/changes/changes.xml                         |  3 ++
 .../pool2/impl/GenericKeyedObjectPool.java      | 30 +++++++-------------
 .../commons/pool2/impl/GenericObjectPool.java   | 30 +++++++-------------
 3 files changed, 23 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-pool/blob/564334a3/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a1293f7..30573e4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,9 @@ The <action> type attribute can be add,update,fix,remove.
       threads try to borrow an object at the same time and the factory fails to
       create any objects. 
     </action>
+    <action dev="markt" issue="POOL-280" tyoe="update" due-to="Jacopo 
Cappellato">
+      Small refactoring of borrowObject() to reduce code duplication.
+    </action>
   </release>
   <release version="2.4.2" date="2015-08-01" description=
  "This is a patch release, including bug fixes only.">

http://git-wip-us.apache.org/repos/asf/commons-pool/blob/564334a3/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java 
b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
index 697ae05..8e1f88f 100644
--- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
@@ -350,14 +350,14 @@ public class GenericKeyedObjectPool<K,T> extends 
BaseGenericObjectPool<T>
         try {
             while (p == null) {
                 create = false;
-                if (blockWhenExhausted) {
-                    p = objectDeque.getIdleObjects().pollFirst();
-                    if (p == null) {
-                        p = create(key);
-                        if (p != null) {
-                            create = true;
-                        }
+                p = objectDeque.getIdleObjects().pollFirst();
+                if (p == null) {
+                    p = create(key);
+                    if (p != null) {
+                        create = true;
                     }
+                }
+                if (blockWhenExhausted) {
                     if (p == null) {
                         if (borrowMaxWaitMillis < 0) {
                             p = objectDeque.getIdleObjects().takeFirst();
@@ -370,23 +370,13 @@ public class GenericKeyedObjectPool<K,T> extends 
BaseGenericObjectPool<T>
                         throw new NoSuchElementException(
                                 "Timeout waiting for idle object");
                     }
-                    if (!p.allocate()) {
-                        p = null;
-                    }
                 } else {
-                    p = objectDeque.getIdleObjects().pollFirst();
-                    if (p == null) {
-                        p = create(key);
-                        if (p != null) {
-                            create = true;
-                        }
-                    }
                     if (p == null) {
                         throw new NoSuchElementException("Pool exhausted");
                     }
-                    if (!p.allocate()) {
-                        p = null;
-                    }
+                }
+                if (!p.allocate()) {
+                    p = null;
                 }
 
                 if (p != null) {

http://git-wip-us.apache.org/repos/asf/commons-pool/blob/564334a3/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java 
b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
index b5ef9bd..7a3ee80 100644
--- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
@@ -428,14 +428,14 @@ public class GenericObjectPool<T> extends 
BaseGenericObjectPool<T>
 
         while (p == null) {
             create = false;
-            if (blockWhenExhausted) {
-                p = idleObjects.pollFirst();
-                if (p == null) {
-                    p = create();
-                    if (p != null) {
-                        create = true;
-                    }
+            p = idleObjects.pollFirst();
+            if (p == null) {
+                p = create();
+                if (p != null) {
+                    create = true;
                 }
+            }
+            if (blockWhenExhausted) {
                 if (p == null) {
                     if (borrowMaxWaitMillis < 0) {
                         p = idleObjects.takeFirst();
@@ -448,23 +448,13 @@ public class GenericObjectPool<T> extends 
BaseGenericObjectPool<T>
                     throw new NoSuchElementException(
                             "Timeout waiting for idle object");
                 }
-                if (!p.allocate()) {
-                    p = null;
-                }
             } else {
-                p = idleObjects.pollFirst();
-                if (p == null) {
-                    p = create();
-                    if (p != null) {
-                        create = true;
-                    }
-                }
                 if (p == null) {
                     throw new NoSuchElementException("Pool exhausted");
                 }
-                if (!p.allocate()) {
-                    p = null;
-                }
+            }
+            if (!p.allocate()) {
+                p = null;
             }
 
             if (p != null) {

Reply via email to