Skip to content

Commit

Permalink
Make Tuple a record (elastic#88280)
Browse files Browse the repository at this point in the history
Tuple is used extensively across the ES codebase and can be effectively represent as a Java record.
  • Loading branch information
arteam authored Jul 28, 2022
1 parent fb01f4e commit 8a159e9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 61 deletions.
43 changes: 1 addition & 42 deletions libs/core/src/main/java/org/elasticsearch/core/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,9 @@

package org.elasticsearch.core;

public class Tuple<V1, V2> {
public record Tuple<V1, V2> (V1 v1, V2 v2) {

public static <V1, V2> Tuple<V1, V2> 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 + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Map<String, String>> {
public HeaderValue(String headerValue, Map<String, String> parametersForValidation) {
super(headerValue, parametersForValidation);
}
record HeaderValue(String v1, Map<String, String> v2) {

public HeaderValue(String headerValue) {
this(headerValue, Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1060,21 +1059,13 @@ public LogicalPlan apply(LogicalPlan p) {
}
}

private static class PercentileKey extends Tuple<Expression, PercentilesConfig> {
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());
}
}

Expand Down

0 comments on commit 8a159e9

Please sign in to comment.