Skip to content

Commit

Permalink
* Chain Stream
Browse files Browse the repository at this point in the history
* Replace For loop with map.forEach
* Inline variable
  • Loading branch information
arturobernalg committed Jul 31, 2021
1 parent 74516b5 commit e5b5ac0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
Expand Up @@ -625,14 +625,13 @@ public void clearOldest() {
// build sorted map of idle objects
final Map<PooledObject<T>, K> map = new TreeMap<>();

poolMap.entrySet().forEach(entry -> {
final ObjectDeque<T> deque = entry.getValue();
poolMap.forEach((key, value) -> {
// Protect against possible NPE if key has been removed in another
// thread. Not worth locking the keys while this loop completes.
if (deque != null) {
if (value != null) {
// Each item into the map using the PooledObject object as the
// key. It then gets sorted based on the idle time
deque.getIdleObjects().forEach(p -> map.put(p, entry.getKey()));
value.getIdleObjects().forEach(p -> map.put(p, key));
}
});

Expand Down Expand Up @@ -1257,7 +1256,7 @@ public Map<String, Integer> getNumWaitersByKey() {
* {@code false}
*/
private boolean hasBorrowWaiters() {
return poolMap.values().stream().filter(deque -> deque != null && deque.getIdleObjects().hasTakeWaiters()).findFirst().isPresent();
return poolMap.values().stream().anyMatch(deque -> deque != null && deque.getIdleObjects().hasTakeWaiters());
}

/**
Expand Down Expand Up @@ -1330,11 +1329,9 @@ public void invalidateObject(final K key, final T obj, final DestroyMode destroy
public Map<String, List<DefaultPooledObjectInfo>> listAllObjects() {
final Map<String, List<DefaultPooledObjectInfo>> result = new HashMap<>();

poolMap.entrySet().forEach(entry -> {
final K k = entry.getKey();
final ObjectDeque<T> deque = entry.getValue();
if (deque != null) {
result.put(k.toString(), deque.getAllObjects().values().stream().map(DefaultPooledObjectInfo::new).collect(Collectors.toList()));
poolMap.forEach((k, value) -> {
if (value != null) {
result.put(k.toString(), value.getAllObjects().values().stream().map(DefaultPooledObjectInfo::new).collect(Collectors.toList()));
}
});
return result;
Expand Down Expand Up @@ -1408,16 +1405,16 @@ private ObjectDeque<T> register(final K k) {
*/
@SuppressWarnings("resource") // The PrintWriter is managed elsewhere
private void removeAbandoned(final AbandonedConfig abandonedConfig) {
poolMap.entrySet().forEach(pool -> {
poolMap.forEach((key, value) -> {
// Generate a list of abandoned objects to remove
final ArrayList<PooledObject<T>> remove = createRemoveList(abandonedConfig, pool.getValue().getAllObjects());
final ArrayList<PooledObject<T>> remove = createRemoveList(abandonedConfig, value.getAllObjects());
// Now remove the abandoned objects
remove.forEach(pooledObject -> {
if (abandonedConfig.getLogAbandoned()) {
pooledObject.printStackTrace(abandonedConfig.getLogWriter());
}
try {
invalidateObject(pool.getKey(), pooledObject.getObject(), DestroyMode.ABANDONED);
invalidateObject(key, pooledObject.getObject(), DestroyMode.ABANDONED);
} catch (final Exception e) {
swallowException(e);
}
Expand Down
Expand Up @@ -291,7 +291,7 @@ private void destroy(final PooledSoftReference<T> toDestroy) throws Exception {
private PooledSoftReference<T> findReference(final T obj) {
final Optional<PooledSoftReference<T>> first = allReferences.stream()
.filter(reference -> reference.getObject() != null && reference.getObject().equals(obj)).findFirst();
return first.isPresent() ? first.get() : null;
return first.orElse(null);
}

/**
Expand Down

0 comments on commit e5b5ac0

Please sign in to comment.