From 7653376ece28a66786f1c7a2494d589fa5e7034d Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 23 Jun 2015 10:46:08 +0200 Subject: [PATCH] Query refactoring: simplify equals implementation and dedup code Closes #11818 --- .../index/query/AbstractQueryBuilder.java | 43 ++++++++++++++----- .../index/query/AndQueryBuilder.java | 9 +--- .../index/query/BaseTermQueryBuilder.java | 9 +--- .../index/query/BoolQueryBuilder.java | 9 +--- .../index/query/BoostingQueryBuilder.java | 9 +--- .../query/ConstantScoreQueryBuilder.java | 9 +--- .../index/query/DisMaxQueryBuilder.java | 9 +--- .../index/query/ExistsQueryBuilder.java | 9 +--- .../index/query/FQueryFilterBuilder.java | 9 +--- .../query/FieldMaskingSpanQueryBuilder.java | 9 +--- .../index/query/IdsQueryBuilder.java | 9 +--- .../index/query/LimitQueryBuilder.java | 11 +---- .../index/query/MatchAllQueryBuilder.java | 11 +---- .../index/query/NotQueryBuilder.java | 9 +--- .../index/query/OrQueryBuilder.java | 9 +--- .../index/query/QueryFilterBuilder.java | 9 +--- .../index/query/RangeQueryBuilder.java | 9 +--- 17 files changed, 50 insertions(+), 141 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java index 2baed612644..7bd72ce2933 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -66,6 +66,38 @@ public abstract class AbstractQueryBuilder extends ToXC return null; } + //norelease remove this once all builders implement readFrom themselves + @Override + public QB readFrom(StreamInput in) throws IOException { + return null; + } + + //norelease remove this once all builders implement writeTo themselves + @Override + public void writeTo(StreamOutput out) throws IOException { + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + @SuppressWarnings("unchecked") + QB other = (QB) obj; + return doEquals(other); + } + + /** + * Indicates whether some other {@link QueryBuilder} object of the same type is "equal to" this one. + */ + //norelease to be made abstract once all queries are refactored + protected boolean doEquals(QB other) { + throw new UnsupportedOperationException(); + } + /** * This helper method checks if the object passed in is a string, if so it * converts it to a {@link BytesRef}. @@ -112,15 +144,4 @@ public abstract class AbstractQueryBuilder extends ToXC } return queries; } - - //norelease remove this once all builders implement readFrom themselves - @Override - public QB readFrom(StreamInput in) throws IOException { - return null; - } - - //norelease remove this once all builders implement writeTo themselves - @Override - public void writeTo(StreamOutput out) throws IOException { - } } diff --git a/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java index 920e20f2be3..3c6e2096465 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java @@ -136,14 +136,7 @@ public class AndQueryBuilder extends AbstractQueryBuilder { } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - AndQueryBuilder other = (AndQueryBuilder) obj; + public boolean doEquals(AndQueryBuilder other) { return Objects.equals(filters, other.filters) && Objects.equals(queryName, other.queryName); } diff --git a/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java index 75b97a234d7..1f8df908e10 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java @@ -194,14 +194,7 @@ public abstract class BaseTermQueryBuilder> } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - BaseTermQueryBuilder other = (BaseTermQueryBuilder) obj; + public final boolean doEquals(BaseTermQueryBuilder other) { return Objects.equals(fieldName, other.fieldName) && Objects.equals(value, other.value) && Objects.equals(boost, other.boost) && diff --git a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java index f315b9dce75..9bec51198bd 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java @@ -335,14 +335,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder imp } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - BoolQueryBuilder other = (BoolQueryBuilder) obj; + public boolean doEquals(BoolQueryBuilder other) { return Objects.equals(boost, other.boost) && Objects.equals(adjustPureNegative, other.adjustPureNegative) && Objects.equals(disableCoord, other.disableCoord) && diff --git a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java index 697a8785bae..fb05b41f2ca 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java @@ -176,14 +176,7 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - DisMaxQueryBuilder other = (DisMaxQueryBuilder) obj; + public boolean doEquals(DisMaxQueryBuilder other) { return Objects.equals(queries, other.queries) && Objects.equals(tieBreaker, other.tieBreaker) && Objects.equals(boost, other.boost) && diff --git a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java index 5576777d083..3b020bcbcd0 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java @@ -141,14 +141,7 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - ExistsQueryBuilder other = (ExistsQueryBuilder) obj; + public boolean doEquals(ExistsQueryBuilder other) { return Objects.equals(name, other.name) && Objects.equals(queryName, other.queryName); } diff --git a/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java b/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java index 1052eb4e37c..4f17d238acd 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java @@ -110,14 +110,7 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder imple } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - IdsQueryBuilder other = (IdsQueryBuilder) obj; + public boolean doEquals(IdsQueryBuilder other) { return Objects.equals(ids, other.ids) && Arrays.equals(types, other.types) && Objects.equals(boost, other.boost) && diff --git a/core/src/main/java/org/elasticsearch/index/query/LimitQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/LimitQueryBuilder.java index 973d22887c4..6d89f79b7b0 100644 --- a/core/src/main/java/org/elasticsearch/index/query/LimitQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/LimitQueryBuilder.java @@ -56,15 +56,8 @@ public class LimitQueryBuilder extends AbstractQueryBuilder { } @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - LimitQueryBuilder that = (LimitQueryBuilder) o; - return Integer.compare(that.limit, limit) == 0; + public boolean doEquals(LimitQueryBuilder other) { + return Integer.compare(other.limit, limit) == 0; } @Override diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java index 35c8e761a68..bcc2b6b91f6 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java @@ -76,15 +76,8 @@ public class MatchAllQueryBuilder extends AbstractQueryBuilder { } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - NotQueryBuilder other = (NotQueryBuilder) obj; + public boolean doEquals(NotQueryBuilder other) { return Objects.equals(filter, other.filter) && Objects.equals(queryName, other.queryName); } diff --git a/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java index 27d6298b01d..63c9e338a83 100644 --- a/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java @@ -135,14 +135,7 @@ public class OrQueryBuilder extends AbstractQueryBuilder { } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - OrQueryBuilder other = (OrQueryBuilder) obj; + public boolean doEquals(OrQueryBuilder other) { return Objects.equals(filters, other.filters) && Objects.equals(queryName, other.queryName); } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryFilterBuilder.java b/core/src/main/java/org/elasticsearch/index/query/QueryFilterBuilder.java index a7122564048..b4b30b7c572 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryFilterBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryFilterBuilder.java @@ -83,14 +83,7 @@ public class QueryFilterBuilder extends AbstractQueryBuilder } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - QueryFilterBuilder other = (QueryFilterBuilder) obj; + public boolean doEquals(QueryFilterBuilder other) { return Objects.equals(queryBuilder, other.queryBuilder); } diff --git a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java index dfa36d2a805..ba3b5fb380e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -378,14 +378,7 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - RangeQueryBuilder other = (RangeQueryBuilder) obj; + public boolean doEquals(RangeQueryBuilder other) { return Objects.equals(fieldName, other.fieldName) && Objects.equals(from, other.from) && Objects.equals(to, other.to) &&