From 8a159e9759bf504cb5e83dd02b8a85340a305ed0 Mon Sep 17 00:00:00 2001 From: Artem Prigoda Date: Thu, 28 Jul 2022 09:14:12 +0200 Subject: [PATCH] Make Tuple a record (#88280) Tuple is used extensively across the ES codebase and can be effectively represent as a Java record. --- .../java/org/elasticsearch/core/Tuple.java | 43 +------------------ .../common/collect/TupleTests.java | 2 +- .../org/elasticsearch/xcontent/MediaType.java | 7 +-- .../xpack/sql/optimizer/Optimizer.java | 15 ++----- 4 files changed, 6 insertions(+), 61 deletions(-) diff --git a/libs/core/src/main/java/org/elasticsearch/core/Tuple.java b/libs/core/src/main/java/org/elasticsearch/core/Tuple.java index c5c8746bbaaf3..a535e3b8d996d 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/Tuple.java +++ b/libs/core/src/main/java/org/elasticsearch/core/Tuple.java @@ -8,50 +8,9 @@ package org.elasticsearch.core; -public class Tuple { +public record Tuple (V1 v1, V2 v2) { public static Tuple tuple(V1 v1, V2 v2) { return new Tuple<>(v1, v2); } - - private final V1 v1; - private final V2 v2; - - public Tuple(V1 v1, V2 v2) { - this.v1 = v1; - this.v2 = v2; - } - - public V1 v1() { - return v1; - } - - public V2 v2() { - return v2; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Tuple tuple = (Tuple) o; - - if (v1 != null ? v1.equals(tuple.v1) == false : tuple.v1 != null) return false; - if (v2 != null ? v2.equals(tuple.v2) == false : tuple.v2 != null) return false; - - return true; - } - - @Override - public int hashCode() { - int result = v1 != null ? v1.hashCode() : 0; - result = 31 * result + (v2 != null ? v2.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return "Tuple [v1=" + v1 + ", v2=" + v2 + "]"; - } } diff --git a/libs/core/src/test/java/org/elasticsearch/common/collect/TupleTests.java b/libs/core/src/test/java/org/elasticsearch/common/collect/TupleTests.java index 62f4c7fa894e1..962f519f93a93 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/collect/TupleTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/collect/TupleTests.java @@ -32,6 +32,6 @@ public void testTuple() { assertNotEquals(t3, t4); assertNotEquals(t1, t5); - assertThat(t1.toString(), equalTo("Tuple [v1=2, v2=foo]")); + assertThat(t1.toString(), equalTo("Tuple[v1=2, v2=foo]")); } } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaType.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaType.java index 3e69a416dc581..9884e0cdc85b5 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaType.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaType.java @@ -8,8 +8,6 @@ package org.elasticsearch.xcontent; -import org.elasticsearch.core.Tuple; - import java.util.Collections; import java.util.Map; import java.util.Set; @@ -40,10 +38,7 @@ public interface MediaType { * Parameters for validation is a map where a key is a parameter name, value is a parameter regex which is used for validation. * Regex will be applied with case insensitivity. */ - class HeaderValue extends Tuple> { - public HeaderValue(String headerValue, Map parametersForValidation) { - super(headerValue, parametersForValidation); - } + record HeaderValue(String v1, Map v2) { public HeaderValue(String headerValue) { this(headerValue, Collections.emptyMap()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java index 81854eee8cda3..617c9226b4937 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.sql.optimizer; -import org.elasticsearch.core.Tuple; import org.elasticsearch.search.aggregations.metrics.PercentilesConfig; import org.elasticsearch.xpack.ql.expression.Alias; import org.elasticsearch.xpack.ql.expression.Attribute; @@ -1060,21 +1059,13 @@ public LogicalPlan apply(LogicalPlan p) { } } - private static class PercentileKey extends Tuple { + private record PercentileKey(Expression field, PercentilesConfig percentilesConfig) { PercentileKey(Percentile per) { - super(per.field(), per.percentilesConfig()); + this(per.field(), per.percentilesConfig()); } PercentileKey(PercentileRank per) { - super(per.field(), per.percentilesConfig()); - } - - private Expression field() { - return v1(); - } - - private PercentilesConfig percentilesConfig() { - return v2(); + this(per.field(), per.percentilesConfig()); } }