Simplify ConcurrentReferenceHashMap

This commit is contained in:
stsypanov 2019-02-05 13:52:00 +02:00 committed by Juergen Hoeller
parent d2bfca7900
commit e7dc439f90
1 changed files with 11 additions and 14 deletions

View File

@ -285,7 +285,7 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
return doTask(key, new Task<V>(TaskOption.RESTRUCTURE_BEFORE, TaskOption.RESIZE) { return doTask(key, new Task<V>(TaskOption.RESTRUCTURE_BEFORE, TaskOption.RESIZE) {
@Override @Override
@Nullable @Nullable
protected V execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries entries) { protected V execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries<V> entries) {
if (entry != null) { if (entry != null) {
V oldValue = entry.getValue(); V oldValue = entry.getValue();
if (overwriteExisting) { if (overwriteExisting) {
@ -530,15 +530,12 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
final Reference<K, V> head = this.references[index]; final Reference<K, V> head = this.references[index];
Reference<K, V> ref = findInChain(head, key, hash); Reference<K, V> ref = findInChain(head, key, hash);
Entry<K, V> entry = (ref != null ? ref.get() : null); Entry<K, V> entry = (ref != null ? ref.get() : null);
Entries entries = new Entries() { Entries<V> entries = value -> {
@Override
public void add(@Nullable V value) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Entry<K, V> newEntry = new Entry<>((K) key, value); Entry<K, V> newEntry = new Entry<>((K) key, value);
Reference<K, V> newReference = Segment.this.referenceManager.createReference(newEntry, hash, head); Reference<K, V> newReference = Segment.this.referenceManager.createReference(newEntry, hash, head);
Segment.this.references[index] = newReference; Segment.this.references[index] = newReference;
Segment.this.count.incrementAndGet(); Segment.this.count.incrementAndGet();
}
}; };
return task.execute(ref, entry, entries); return task.execute(ref, entry, entries);
} }
@ -802,7 +799,7 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
* @see #execute(Reference, Entry) * @see #execute(Reference, Entry)
*/ */
@Nullable @Nullable
protected T execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries entries) { protected T execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries<V> entries) {
return execute(ref, entry); return execute(ref, entry);
} }
@ -830,15 +827,15 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
/** /**
* Allows a task access to {@link Segment} entries. * Allows a task access to {@link ConcurrentReferenceHashMap.Segment} entries.
*/ */
private abstract class Entries { private interface Entries<V> {
/** /**
* Add a new entry with the specified value. * Add a new entry with the specified value.
* @param value the value to add * @param value the value to add
*/ */
public abstract void add(@Nullable V value); void add(@Nullable V value);
} }