Skip to content

Commit a8bec9c

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Use JDK alternatives to AbstractMapEntry in some places.
RELNOTES=n/a PiperOrigin-RevId: 778480079
1 parent 9892c31 commit a8bec9c

File tree

6 files changed

+58
-178
lines changed

6 files changed

+58
-178
lines changed

android/guava/src/com/google/common/collect/LinkedListMultimap.java

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.ObjectInputStream;
3232
import java.io.ObjectOutputStream;
3333
import java.io.Serializable;
34+
import java.util.AbstractMap.SimpleEntry;
3435
import java.util.AbstractSequentialList;
3536
import java.util.Collection;
3637
import java.util.ConcurrentModificationException;
@@ -106,37 +107,14 @@ public class LinkedListMultimap<K extends @Nullable Object, V extends @Nullable
106107
*/
107108

108109
static final class Node<K extends @Nullable Object, V extends @Nullable Object>
109-
extends AbstractMapEntry<K, V> {
110-
@ParametricNullness final K key;
111-
@ParametricNullness V value;
110+
extends SimpleEntry<K, V> {
112111
@Nullable Node<K, V> next; // the next node (with any key)
113112
@Weak @Nullable Node<K, V> previous; // the previous node (with any key)
114113
@Nullable Node<K, V> nextSibling; // the next node with the same key
115114
@Weak @Nullable Node<K, V> previousSibling; // the previous node with the same key
116115

117116
Node(@ParametricNullness K key, @ParametricNullness V value) {
118-
this.key = key;
119-
this.value = value;
120-
}
121-
122-
@Override
123-
@ParametricNullness
124-
public K getKey() {
125-
return key;
126-
}
127-
128-
@Override
129-
@ParametricNullness
130-
public V getValue() {
131-
return value;
132-
}
133-
134-
@Override
135-
@ParametricNullness
136-
public V setValue(@ParametricNullness V newValue) {
137-
V result = value;
138-
this.value = newValue;
139-
return result;
117+
super(key, value);
140118
}
141119
}
142120

@@ -288,12 +266,12 @@ private void removeNode(Node<K, V> node) {
288266
* Multimap. This should be the case (except in case of concurrent modification, when all bets
289267
* are off).
290268
*/
291-
KeyList<K, V> keyList = requireNonNull(keyToKeyList.remove(node.key));
269+
KeyList<K, V> keyList = requireNonNull(keyToKeyList.remove(node.getKey()));
292270
keyList.count = 0;
293271
modCount++;
294272
} else {
295273
// requireNonNull is safe (under the conditions listed in the comment in the branch above).
296-
KeyList<K, V> keyList = requireNonNull(keyToKeyList.get(node.key));
274+
KeyList<K, V> keyList = requireNonNull(keyToKeyList.get(node.getKey()));
297275
keyList.count--;
298276

299277
if (node.previousSibling == null) {
@@ -425,7 +403,7 @@ public void add(Entry<K, V> e) {
425403

426404
void setValue(@ParametricNullness V value) {
427405
checkState(current != null);
428-
current.value = value;
406+
current.setValue(value);
429407
}
430408
}
431409

@@ -456,18 +434,18 @@ public K next() {
456434
throw new NoSuchElementException();
457435
}
458436
current = next;
459-
seenKeys.add(current.key);
437+
seenKeys.add(current.getKey());
460438
do { // skip ahead to next unseen key
461439
next = next.next;
462-
} while ((next != null) && !seenKeys.add(next.key));
463-
return current.key;
440+
} while ((next != null) && !seenKeys.add(next.getKey()));
441+
return current.getKey();
464442
}
465443

466444
@Override
467445
public void remove() {
468446
checkForConcurrentModification();
469447
checkState(current != null, "no calls to next() since the last call to remove()");
470-
removeAllNodes(current.key);
448+
removeAllNodes(current.getKey());
471449
current = null;
472450
expectedModCount = modCount;
473451
}
@@ -531,7 +509,7 @@ public V next() {
531509
previous = current = next;
532510
next = next.nextSibling;
533511
nextIndex++;
534-
return current.value;
512+
return current.getValue();
535513
}
536514

537515
@Override
@@ -549,7 +527,7 @@ public V previous() {
549527
next = current = previous;
550528
previous = previous.previousSibling;
551529
nextIndex--;
552-
return current.value;
530+
return current.getValue();
553531
}
554532

555533
@Override
@@ -578,7 +556,7 @@ public void remove() {
578556
@Override
579557
public void set(@ParametricNullness V value) {
580558
checkState(current != null);
581-
current.value = value;
559+
current.setValue(value);
582560
}
583561

584562
@Override

android/guava/src/com/google/common/collect/MapMakerInternalMap.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.lang.ref.WeakReference;
4040
import java.util.AbstractCollection;
4141
import java.util.AbstractMap;
42+
import java.util.AbstractMap.SimpleEntry;
4243
import java.util.AbstractSet;
4344
import java.util.Collection;
4445
import java.util.Iterator;
@@ -2646,46 +2647,20 @@ public V next() {
26462647
* Custom Entry class used by EntryIterator.next(), that relays setValue changes to the underlying
26472648
* map.
26482649
*/
2649-
final class WriteThroughEntry extends AbstractMapEntry<K, V> {
2650-
final K key; // non-null
2651-
V value; // non-null
2652-
2650+
final class WriteThroughEntry extends SimpleEntry<K, V> {
26532651
WriteThroughEntry(K key, V value) {
2654-
this.key = key;
2655-
this.value = value;
2656-
}
2657-
2658-
@Override
2659-
public K getKey() {
2660-
return key;
2661-
}
2662-
2663-
@Override
2664-
public V getValue() {
2665-
return value;
2652+
super(key, value);
26662653
}
26672654

2668-
@Override
2669-
public boolean equals(@Nullable Object object) {
2670-
// Cannot use key and value equivalence
2671-
if (object instanceof Entry) {
2672-
Entry<?, ?> that = (Entry<?, ?>) object;
2673-
return key.equals(that.getKey()) && value.equals(that.getValue());
2674-
}
2675-
return false;
2676-
}
2677-
2678-
@Override
2679-
public int hashCode() {
2680-
// Cannot use key and value equivalence
2681-
return key.hashCode() ^ value.hashCode();
2682-
}
2655+
/*
2656+
* We inherit equals() and hashCode() instead of overriding them to use keyEquivalence and
2657+
* valueEquivalence.
2658+
*/
26832659

26842660
@Override
26852661
public V setValue(V newValue) {
2686-
V oldValue = put(key, newValue);
2687-
value = newValue; // only if put succeeds
2688-
return oldValue;
2662+
put(getKey(), newValue);
2663+
return super.setValue(newValue); // done after put() so that it happens only if put() succeeds
26892664
}
26902665
}
26912666

android/guava/src/com/google/common/collect/TreeRangeMap.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.common.base.Predicate;
3232
import com.google.common.collect.Maps.IteratorBasedAbstractMap;
3333
import java.util.AbstractMap;
34+
import java.util.AbstractMap.SimpleImmutableEntry;
3435
import java.util.ArrayList;
3536
import java.util.Collection;
3637
import java.util.Iterator;
@@ -93,39 +94,25 @@ private TreeRangeMap(NavigableMap<Cut<K>, RangeMapEntry<K, V>> entriesByLowerBou
9394
}
9495

9596
private static final class RangeMapEntry<K extends Comparable, V>
96-
extends AbstractMapEntry<Range<K>, V> {
97-
private final Range<K> range;
98-
private final V value;
99-
97+
extends SimpleImmutableEntry<Range<K>, V> {
10098
RangeMapEntry(Cut<K> lowerBound, Cut<K> upperBound, V value) {
10199
this(Range.create(lowerBound, upperBound), value);
102100
}
103101

104102
RangeMapEntry(Range<K> range, V value) {
105-
this.range = range;
106-
this.value = value;
107-
}
108-
109-
@Override
110-
public Range<K> getKey() {
111-
return range;
112-
}
113-
114-
@Override
115-
public V getValue() {
116-
return value;
103+
super(range, value);
117104
}
118105

119-
public boolean contains(K value) {
120-
return range.contains(value);
106+
boolean contains(K value) {
107+
return getKey().contains(value);
121108
}
122109

123110
Cut<K> getLowerBound() {
124-
return range.lowerBound;
111+
return getKey().lowerBound;
125112
}
126113

127114
Cut<K> getUpperBound() {
128-
return range.upperBound;
115+
return getKey().upperBound;
129116
}
130117
}
131118

guava/src/com/google/common/collect/LinkedListMultimap.java

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.ObjectInputStream;
3333
import java.io.ObjectOutputStream;
3434
import java.io.Serializable;
35+
import java.util.AbstractMap.SimpleEntry;
3536
import java.util.AbstractSequentialList;
3637
import java.util.Collection;
3738
import java.util.ConcurrentModificationException;
@@ -109,37 +110,14 @@ public class LinkedListMultimap<K extends @Nullable Object, V extends @Nullable
109110
*/
110111

111112
static final class Node<K extends @Nullable Object, V extends @Nullable Object>
112-
extends AbstractMapEntry<K, V> {
113-
@ParametricNullness final K key;
114-
@ParametricNullness V value;
113+
extends SimpleEntry<K, V> {
115114
@Nullable Node<K, V> next; // the next node (with any key)
116115
@Weak @Nullable Node<K, V> previous; // the previous node (with any key)
117116
@Nullable Node<K, V> nextSibling; // the next node with the same key
118117
@Weak @Nullable Node<K, V> previousSibling; // the previous node with the same key
119118

120119
Node(@ParametricNullness K key, @ParametricNullness V value) {
121-
this.key = key;
122-
this.value = value;
123-
}
124-
125-
@Override
126-
@ParametricNullness
127-
public K getKey() {
128-
return key;
129-
}
130-
131-
@Override
132-
@ParametricNullness
133-
public V getValue() {
134-
return value;
135-
}
136-
137-
@Override
138-
@ParametricNullness
139-
public V setValue(@ParametricNullness V newValue) {
140-
V result = value;
141-
this.value = newValue;
142-
return result;
120+
super(key, value);
143121
}
144122
}
145123

@@ -291,12 +269,12 @@ private void removeNode(Node<K, V> node) {
291269
* Multimap. This should be the case (except in case of concurrent modification, when all bets
292270
* are off).
293271
*/
294-
KeyList<K, V> keyList = requireNonNull(keyToKeyList.remove(node.key));
272+
KeyList<K, V> keyList = requireNonNull(keyToKeyList.remove(node.getKey()));
295273
keyList.count = 0;
296274
modCount++;
297275
} else {
298276
// requireNonNull is safe (under the conditions listed in the comment in the branch above).
299-
KeyList<K, V> keyList = requireNonNull(keyToKeyList.get(node.key));
277+
KeyList<K, V> keyList = requireNonNull(keyToKeyList.get(node.getKey()));
300278
keyList.count--;
301279

302280
if (node.previousSibling == null) {
@@ -428,7 +406,7 @@ public void add(Entry<K, V> e) {
428406

429407
void setValue(@ParametricNullness V value) {
430408
checkState(current != null);
431-
current.value = value;
409+
current.setValue(value);
432410
}
433411
}
434412

@@ -459,18 +437,18 @@ public K next() {
459437
throw new NoSuchElementException();
460438
}
461439
current = next;
462-
seenKeys.add(current.key);
440+
seenKeys.add(current.getKey());
463441
do { // skip ahead to next unseen key
464442
next = next.next;
465-
} while ((next != null) && !seenKeys.add(next.key));
466-
return current.key;
443+
} while ((next != null) && !seenKeys.add(next.getKey()));
444+
return current.getKey();
467445
}
468446

469447
@Override
470448
public void remove() {
471449
checkForConcurrentModification();
472450
checkState(current != null, "no calls to next() since the last call to remove()");
473-
removeAllNodes(current.key);
451+
removeAllNodes(current.getKey());
474452
current = null;
475453
expectedModCount = modCount;
476454
}
@@ -534,7 +512,7 @@ public V next() {
534512
previous = current = next;
535513
next = next.nextSibling;
536514
nextIndex++;
537-
return current.value;
515+
return current.getValue();
538516
}
539517

540518
@Override
@@ -552,7 +530,7 @@ public V previous() {
552530
next = current = previous;
553531
previous = previous.previousSibling;
554532
nextIndex--;
555-
return current.value;
533+
return current.getValue();
556534
}
557535

558536
@Override
@@ -581,7 +559,7 @@ public void remove() {
581559
@Override
582560
public void set(@ParametricNullness V value) {
583561
checkState(current != null);
584-
current.value = value;
562+
current.setValue(value);
585563
}
586564

587565
@Override

0 commit comments

Comments
 (0)