Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add equals/hashcode to fieldtypes #11644

Merged
merged 2 commits into from
Jun 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
* This defines the core properties and functions to operate on a field.
Expand Down Expand Up @@ -200,7 +201,31 @@ public MappedFieldType clone() {
return new MappedFieldType(this);
}

// norelease: we need to override freeze() and add safety checks that all settings are actually set
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MappedFieldType)) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super.equals already ensures this, since it does: if (getClass() != obj.getClass()) return false;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generated all equals/hashcode methods with intellij. I've removed the checks before super.equals, and filed an issue with intellij [1] to be smarter about this.

I also had noticed always using Objects.equals here, even for primitive types. It looks like this is fixed [2] in an unreleased version of intellij. For now, I've gone through and changed each primitive type to use == instead.

[1] https://youtrack.jetbrains.com/issue/IDEA-141479
[2] https://youtrack.jetbrains.com/issue/IDEA-139622

if (!super.equals(o)) return false;
MappedFieldType fieldType = (MappedFieldType) o;
return Objects.equals(boost, fieldType.boost) &&
Objects.equals(docValues, fieldType.docValues) &&
Objects.equals(names, fieldType.names) &&
Objects.equals(indexAnalyzer, fieldType.indexAnalyzer) &&
Objects.equals(searchAnalyzer, fieldType.searchAnalyzer) &&
Objects.equals(searchQuoteAnalyzer(), fieldType.searchQuoteAnalyzer()) &&
Objects.equals(similarity, fieldType.similarity) &&
Objects.equals(normsLoading, fieldType.normsLoading) &&
Objects.equals(fieldDataType, fieldType.fieldDataType) &&
Objects.equals(nullValue, fieldType.nullValue) &&
Objects.equals(nullValueAsString, fieldType.nullValueAsString);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), names, boost, docValues, indexAnalyzer, searchAnalyzer, searchQuoteAnalyzer, similarity, normsLoading, fieldDataType, nullValue, nullValueAsString);
}

// norelease: we need to override freeze() and add safety checks that all settings are actually set

public boolean isNumeric() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.index.mapper.MapperBuilders.binaryField;
import static org.elasticsearch.index.mapper.core.TypeParsers.parseField;
Expand Down Expand Up @@ -82,7 +83,7 @@ public Builder(String name) {
@Override
public BinaryFieldMapper build(BuilderContext context) {
setupFieldType(context);
((BinaryFieldType)fieldType).tryUncompressing = context.indexCreatedVersion().before(Version.V_2_0_0);
((BinaryFieldType)fieldType).setTryUncompressing(context.indexCreatedVersion().before(Version.V_2_0_0));
return new BinaryFieldMapper(fieldType, docValues,
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
}
Expand All @@ -106,7 +107,7 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
}

static final class BinaryFieldType extends MappedFieldType {
protected boolean tryUncompressing = false;
private boolean tryUncompressing = false;

public BinaryFieldType() {
super(AbstractFieldMapper.Defaults.FIELD_TYPE);
Expand All @@ -122,6 +123,29 @@ public MappedFieldType clone() {
return new BinaryFieldType(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BinaryFieldType)) return false;
if (!super.equals(o)) return false;
BinaryFieldType that = (BinaryFieldType) o;
return Objects.equals(tryUncompressing, that.tryUncompressing);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), tryUncompressing);
}

public boolean tryUncompressing() {
return tryUncompressing;
}

public void setTryUncompressing(boolean tryUncompressing) {
checkIfFrozen();
this.tryUncompressing = tryUncompressing;
}

@Override
public BytesReference value(Object value) {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -234,6 +235,21 @@ public DateFieldType clone() {
return new DateFieldType(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DateFieldType)) return false;
if (!super.equals(o)) return false;
DateFieldType that = (DateFieldType) o;
return Objects.equals(dateTimeFormatter.format(), that.dateTimeFormatter.format()) &&
Objects.equals(timeUnit, that.timeUnit);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), dateTimeFormatter, timeUnit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You use dateTimeFormatter.format() for equals but dateTimeFormatter for hashCode

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed.

}

public FormatDateTimeFormatter dateTimeFormatter() {
return dateTimeFormatter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,28 @@ public MappedFieldType clone() {
return new GeoPointFieldType(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof GeoPointFieldType)) return false;
if (!super.equals(o)) return false;
GeoPointFieldType that = (GeoPointFieldType) o;
return java.util.Objects.equals(geohashPrecision, that.geohashPrecision) &&
java.util.Objects.equals(geohashPrefixEnabled, that.geohashPrefixEnabled) &&
java.util.Objects.equals(validateLon, that.validateLon) &&
java.util.Objects.equals(validateLat, that.validateLat) &&
java.util.Objects.equals(normalizeLon, that.normalizeLon) &&
java.util.Objects.equals(normalizeLat, that.normalizeLat) &&
java.util.Objects.equals(geohashFieldType, that.geohashFieldType) &&
java.util.Objects.equals(latFieldType, that.latFieldType) &&
java.util.Objects.equals(lonFieldType, that.lonFieldType);
}

@Override
public int hashCode() {
return java.util.Objects.hash(super.hashCode(), geohashFieldType, geohashPrecision, geohashPrefixEnabled, latFieldType, lonFieldType, validateLon, validateLat, normalizeLon, normalizeLat);
}

public boolean isGeohashEnabled() {
return geohashFieldType != null;
}
Expand Down