diff --git a/server/src/main/java/org/elasticsearch/cluster/DiffableUtils.java b/server/src/main/java/org/elasticsearch/cluster/DiffableUtils.java index bcb9222e384ae..fc23db6015fa3 100644 --- a/server/src/main/java/org/elasticsearch/cluster/DiffableUtils.java +++ b/server/src/main/java/org/elasticsearch/cluster/DiffableUtils.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.util.Maps; import java.io.IOException; import java.util.ArrayList; @@ -153,7 +154,7 @@ private static > MapDiff createDiff( inserts++; } else if (entry.getValue().equals(previousValue) == false) { if (valueSerializer.supportsDiffableValues()) { - diffs.add(mapEntry(entry.getKey(), valueSerializer.diff(entry.getValue(), previousValue))); + diffs.add(new Maps.ImmutableEntry<>(entry.getKey(), valueSerializer.diff(entry.getValue(), previousValue))); } else { upserts.add(entry); } @@ -307,14 +308,14 @@ private MapDiff( for (int i = 0; i < diffsCount; i++) { K key = keySerializer.readKey(in); Diff diff = valueSerializer.readDiff(in, key); - diffs.add(mapEntry(key, diff)); + diffs.add(new Maps.ImmutableEntry<>(key, diff)); } int upsertsCount = in.readVInt(); upserts = upsertsCount == 0 ? List.of() : new ArrayList<>(upsertsCount); for (int i = 0; i < upsertsCount; i++) { K key = keySerializer.readKey(in); T newValue = valueSerializer.read(in, key); - upserts.add(mapEntry(key, newValue)); + upserts.add(new Maps.ImmutableEntry<>(key, newValue)); } this.builderCtor = builderCtor; } @@ -402,25 +403,6 @@ public void writeTo(StreamOutput out) throws IOException { } } - private static Map.Entry mapEntry(K key, T newValue) { - return new Map.Entry<>() { - @Override - public K getKey() { - return key; - } - - @Override - public T getValue() { - return newValue; - } - - @Override - public T setValue(T value) { - throw new UnsupportedOperationException(); - } - }; - } - /** * Provides read and write operations to serialize keys of map * @param type of key diff --git a/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java b/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java index df5b57055bda9..895df8d34a96e 100644 --- a/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java +++ b/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java @@ -14,6 +14,8 @@ import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.hppc.procedures.ObjectObjectProcedure; +import org.elasticsearch.common.util.Maps; + import java.util.AbstractCollection; import java.util.AbstractMap; import java.util.AbstractSet; @@ -127,45 +129,6 @@ public Set> entrySet() { return (es = entrySet) == null ? (entrySet = new EntrySet<>(map)) : es; } - private static final class ImmutableEntry implements Map.Entry { - private final KType key; - private final VType value; - - ImmutableEntry(KType key, VType value) { - this.key = key; - this.value = value; - } - - @Override - public KType getKey() { - return key; - } - - @Override - public VType getValue() { - return value; - } - - @Override - public VType setValue(VType value) { - throw new UnsupportedOperationException("collection is immutable"); - } - - @Override - @SuppressWarnings("rawtypes") - public boolean equals(Object o) { - if (this == o) return true; - if ((o instanceof Map.Entry) == false) return false; - Map.Entry that = (Map.Entry) o; - return Objects.equals(key, that.getKey()) && Objects.equals(value, that.getValue()); - } - - @Override - public int hashCode() { - return Objects.hashCode(key) ^ Objects.hashCode(value); - } - } - private static final class ConversionIterator implements Iterator> { private final Iterator> original; @@ -185,7 +148,7 @@ public Map.Entry next() { if (obj == null) { return null; } - return new ImmutableEntry<>(obj.key, obj.value); + return new Maps.ImmutableEntry<>(obj.key, obj.value); } @Override @@ -244,7 +207,7 @@ public Spliterator> spliterator() { @Override public void forEach(Consumer> action) { map.forEach((Consumer>) ooCursor -> { - ImmutableEntry entry = new ImmutableEntry<>(ooCursor.key, ooCursor.value); + Maps.ImmutableEntry entry = new Maps.ImmutableEntry<>(ooCursor.key, ooCursor.value); action.accept(entry); }); } diff --git a/server/src/main/java/org/elasticsearch/common/util/Maps.java b/server/src/main/java/org/elasticsearch/common/util/Maps.java index 417b880414e7e..a0ff346da0d9c 100644 --- a/server/src/main/java/org/elasticsearch/common/util/Maps.java +++ b/server/src/main/java/org/elasticsearch/common/util/Maps.java @@ -301,4 +301,41 @@ public static Map copyOf(Map source, Function copyValue } return copy; } + + /** + * An immutable implementation of {@link Map.Entry}. + * @param key key + * @param value value + */ + public record ImmutableEntry (KType key, VType value) implements Map.Entry { + + @Override + public KType getKey() { + return key; + } + + @Override + public VType getValue() { + return value; + } + + @Override + public VType setValue(VType value) { + throw new UnsupportedOperationException(); + } + + @Override + @SuppressWarnings("rawtypes") + public boolean equals(Object o) { + if (this == o) return true; + if ((o instanceof Map.Entry) == false) return false; + Map.Entry that = (Map.Entry) o; + return Objects.equals(key, that.getKey()) && Objects.equals(value, that.getValue()); + } + + @Override + public int hashCode() { + return Objects.hashCode(key) ^ Objects.hashCode(value); + } + } }