Skip to content

Commit 777ceda

Browse files
committed
Add org.apache.commons.pool2.PooledObject.getObject(PooledObject)
1 parent d3c075a commit 777ceda

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/main/java/org/apache/commons/pool3/PooledObject.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,37 @@
3434
public interface PooledObject<T> extends Comparable<PooledObject<T>> {
3535

3636
/**
37-
* Tests whether the given PooledObject is null <em>or</em> contains a null.
37+
* Gets the wrapped object or null.
3838
*
39-
* @param pooledObject the PooledObject to test.
40-
* @return whether the given PooledObject is null <em>or</em> contains a null.
39+
* @param <T> the type of object in the pool.
40+
* @param pooledObject the PooledObject to unwrap, may be null.
41+
* @return the wrapped object or null.
42+
* @since 2.13.0
43+
*/
44+
static <T> T getObject(final PooledObject<T> pooledObject) {
45+
return pooledObject != null ? pooledObject.getObject() : null;
46+
}
47+
48+
/**
49+
* Tests whether the given PooledObject is null <em>or</em> wraps a null.
50+
*
51+
* @param pooledObject the PooledObject to test, may be null.
52+
* @return whether the given PooledObject is null <em>or</em> wraps a null.
4153
* @since 2.12.0
4254
*/
4355
static boolean isNull(final PooledObject<?> pooledObject) {
44-
return pooledObject == null || pooledObject.getObject() == null;
56+
return getObject(pooledObject) == null;
4557
}
4658

4759
/**
4860
* Tests whether the given PooledObject isn't null <em>and</em> doesn't wraps a null.
4961
*
50-
* @param pooledObject the PooledObject to test.
62+
* @param pooledObject the PooledObject to test, may be null.
5163
* @return whether the given PooledObject isn't null <em>and</em> doesn't wraps a null.
5264
* @since 2.13.0
5365
*/
5466
static boolean nonNull(final PooledObject<?> pooledObject) {
55-
return pooledObject != null && pooledObject.getObject() != null;
67+
return getObject(pooledObject) != null;
5668
}
5769

5870
/**

src/test/java/org/apache/commons/pool3/PooledObjectTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.commons.pool3;
1919

2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
2123
import static org.junit.jupiter.api.Assertions.assertTrue;
2224

2325
import org.apache.commons.pool3.impl.DefaultPooledObject;
@@ -28,6 +30,13 @@
2830
*/
2931
public class PooledObjectTest {
3032

33+
@Test
34+
void testGetObject() {
35+
assertNull(PooledObject.getObject(null));
36+
assertNull(PooledObject.getObject(new DefaultPooledObject<>(null)));
37+
assertNotNull(PooledObject.getObject(new DefaultPooledObject<>("a")));
38+
}
39+
3140
@Test
3241
void testIsNull() {
3342
assertTrue(PooledObject.isNull(null));

0 commit comments

Comments
 (0)