Skip to content

Commit 79f493b

Browse files
committed
Add org.apache.commons.pool2.PooledObject.nonNull(PooledObject)
1 parent 03b29cd commit 79f493b

File tree

7 files changed

+62
-7
lines changed

7 files changed

+62
-7
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ static boolean isNull(final PooledObject<?> pooledObject) {
4444
return pooledObject == null || pooledObject.getObject() == null;
4545
}
4646

47+
/**
48+
* Tests whether the given PooledObject isn't null <em>and</em> doesn't wraps a null.
49+
*
50+
* @param pooledObject the PooledObject to test.
51+
* @return whether the given PooledObject isn't null <em>and</em> doesn't wraps a null.
52+
* @since 2.13.0
53+
*/
54+
static boolean nonNull(final PooledObject<?> pooledObject) {
55+
return pooledObject != null && pooledObject.getObject() != null;
56+
}
57+
4758
/**
4859
* Allocates the object.
4960
*

src/main/java/org/apache/commons/pool3/impl/GenericKeyedObjectPool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public GenericKeyedObjectPool(final KeyedPooledObjectFactory<K, T, E> factory,
299299
* @throws E If the associated factory fails to passivate the object
300300
*/
301301
private void addIdleObject(final K key, final PooledObject<T> p) throws E {
302-
if (!PooledObject.isNull(p)) {
302+
if (PooledObject.nonNull(p)) {
303303
factory.passivateObject(key, p);
304304
final LinkedBlockingDeque<PooledObject<T>> idleObjects = poolMap.get(key).getIdleObjects();
305305
if (getLifo()) {
@@ -436,7 +436,7 @@ public T borrowObject(final K key, final long borrowMaxWaitMillis) throws E {
436436
p = objectDeque.getIdleObjects().pollFirst();
437437
if (p == null) {
438438
p = create(key);
439-
if (!PooledObject.isNull(p)) {
439+
if (PooledObject.nonNull(p)) {
440440
create = true;
441441
}
442442
}

src/main/java/org/apache/commons/pool3/impl/GenericObjectPool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public GenericObjectPool(final PooledObjectFactory<T, E> factory,
193193
* @throws E If the factory fails to passivate the object
194194
*/
195195
private void addIdleObject(final PooledObject<T> p) throws E {
196-
if (!PooledObject.isNull(p)) {
196+
if (PooledObject.nonNull(p)) {
197197
factory.passivateObject(p);
198198
if (getLifo()) {
199199
idleObjects.addFirst(p);
@@ -303,7 +303,7 @@ public T borrowObject(final Duration maxWaitDuration) throws E {
303303
p = idleObjects.pollFirst();
304304
if (p == null) {
305305
p = create(remainingWaitDuration);
306-
if (!PooledObject.isNull(p)) {
306+
if (PooledObject.nonNull(p)) {
307307
create = true;
308308
}
309309
}

src/main/java/org/apache/commons/pool3/impl/ResilientPooledObjectFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public PooledObject<T> makeObject() throws E {
413413
final MakeEvent makeEvent = new MakeEvent();
414414
try {
415415
final PooledObject<T> obj = factory.makeObject();
416-
makeEvent.setSuccess(!PooledObject.isNull(obj));
416+
makeEvent.setSuccess(PooledObject.nonNull(obj));
417417
return obj;
418418
} catch (final Throwable t) {
419419
makeEvent.setSuccess(false);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.pool3;
19+
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import org.apache.commons.pool3.impl.DefaultPooledObject;
24+
import org.junit.jupiter.api.Test;
25+
26+
/**
27+
* Tests {@link PooledObject}.
28+
*/
29+
public class PooledObjectTest {
30+
31+
@Test
32+
void testIsNull() {
33+
assertTrue(PooledObject.isNull(null));
34+
assertTrue(PooledObject.isNull(new DefaultPooledObject<>(null)));
35+
assertFalse(PooledObject.isNull(new DefaultPooledObject<>("a")));
36+
}
37+
38+
@Test
39+
void testNonNull() {
40+
assertFalse(PooledObject.nonNull(null));
41+
assertFalse(PooledObject.nonNull(new DefaultPooledObject<>(null)));
42+
assertTrue(PooledObject.nonNull(new DefaultPooledObject<>("a")));
43+
}
44+
}

src/test/java/org/apache/commons/pool3/pool407/AbstractKeyedPool407Factory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class AbstractKeyedPool407Factory extends BaseKeyedPooledObjectF
3030
@Override
3131
public boolean validateObject(final String key, final PooledObject<KeyedPool407Fixture> p) {
3232
// TODO Should this be enough even if wrap() does throw and returns a DefaultPooledObject wrapping a null?
33-
return !PooledObject.isNull(p);
33+
return PooledObject.nonNull(p);
3434
}
3535

3636
}

src/test/java/org/apache/commons/pool3/pool407/AbstractPool407Factory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract class AbstractPool407Factory extends BasePooledObjectFactory<Poo
4343
@Override
4444
public boolean validateObject(final PooledObject<Pool407Fixture> p) {
4545
// TODO Should this be enough even if wrap() does throw and returns a DefaultPooledObject wrapping a null?
46-
return !PooledObject.isNull(p);
46+
return PooledObject.isNull(p);
4747
}
4848

4949
}

0 commit comments

Comments
 (0)