Skip to content
Permalink
Browse files
* Chain Stream (#89)
* Replace For loop with map.forEach
* Inline variable
  • Loading branch information
arturobernalg committed Aug 14, 2021
1 parent b5bf4b1 commit 8ff8f5ab7a64de07dfdbc5a7e4ad621ba592408b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
@@ -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));
}
});

@@ -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());
}

/**
@@ -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;
@@ -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);
}
@@ -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);
}

/**

0 comments on commit 8ff8f5a

Please sign in to comment.