This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 777ceda7 Add 
org.apache.commons.pool2.PooledObject.getObject(PooledObject)
777ceda7 is described below

commit 777ceda7fab0c60ea75452b8973dda170f079afd
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Jun 11 09:14:47 2025 -0400

    Add org.apache.commons.pool2.PooledObject.getObject(PooledObject)
---
 .../org/apache/commons/pool3/PooledObject.java     | 24 ++++++++++++++++------
 .../org/apache/commons/pool3/PooledObjectTest.java |  9 ++++++++
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/commons/pool3/PooledObject.java 
b/src/main/java/org/apache/commons/pool3/PooledObject.java
index e3b89cc5..43dc282e 100644
--- a/src/main/java/org/apache/commons/pool3/PooledObject.java
+++ b/src/main/java/org/apache/commons/pool3/PooledObject.java
@@ -34,25 +34,37 @@ import java.util.Deque;
 public interface PooledObject<T> extends Comparable<PooledObject<T>> {
 
     /**
-     * Tests whether the given PooledObject is null <em>or</em> contains a 
null.
+     * Gets the wrapped object or null.
      *
-     * @param pooledObject the PooledObject to test.
-     * @return whether the given PooledObject is null <em>or</em> contains a 
null.
+     * @param <T> the type of object in the pool.
+     * @param pooledObject the PooledObject to unwrap, may be null.
+     * @return the wrapped object or null.
+     * @since 2.13.0
+     */
+    static <T> T getObject(final PooledObject<T> pooledObject) {
+        return pooledObject != null ? pooledObject.getObject() : null;
+    }
+
+    /**
+     * Tests whether the given PooledObject is null <em>or</em> wraps a null.
+     *
+     * @param pooledObject the PooledObject to test, may be null.
+     * @return whether the given PooledObject is null <em>or</em> wraps a null.
      * @since 2.12.0
      */
     static boolean isNull(final PooledObject<?> pooledObject) {
-        return pooledObject == null || pooledObject.getObject() == null;
+        return getObject(pooledObject) == null;
     }
 
     /**
      * Tests whether the given PooledObject isn't null <em>and</em> doesn't 
wraps a null.
      *
-     * @param pooledObject the PooledObject to test.
+     * @param pooledObject the PooledObject to test, may be null.
      * @return whether the given PooledObject isn't null <em>and</em> doesn't 
wraps a null.
      * @since 2.13.0
      */
     static boolean nonNull(final PooledObject<?> pooledObject) {
-        return pooledObject != null && pooledObject.getObject() != null;
+        return getObject(pooledObject) != null;
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/pool3/PooledObjectTest.java 
b/src/test/java/org/apache/commons/pool3/PooledObjectTest.java
index 81720119..4ba2fc62 100644
--- a/src/test/java/org/apache/commons/pool3/PooledObjectTest.java
+++ b/src/test/java/org/apache/commons/pool3/PooledObjectTest.java
@@ -18,6 +18,8 @@
 package org.apache.commons.pool3;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.apache.commons.pool3.impl.DefaultPooledObject;
@@ -28,6 +30,13 @@ import org.junit.jupiter.api.Test;
  */
 public class PooledObjectTest {
 
+    @Test
+    void testGetObject() {
+        assertNull(PooledObject.getObject(null));
+        assertNull(PooledObject.getObject(new DefaultPooledObject<>(null)));
+        assertNotNull(PooledObject.getObject(new DefaultPooledObject<>("a")));
+    }
+
     @Test
     void testIsNull() {
         assertTrue(PooledObject.isNull(null));

Reply via email to