Skip to content

Commit

Permalink
Generate hashCode and equals (opensearch-project#1201)
Browse files Browse the repository at this point in the history
* Generate hashCode and equals , wip

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* add changelog entry

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* remove change

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* take into account primitives

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* refactor and format

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* use Object.equals

Co-authored-by: Thomas Farr <xtansia@xtansia.com>
Signed-off-by: Miguel Vilá <miguelvilag@gmail.com>

* use `&&` chain

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* adjust last line

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* use fqn

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* use fqn for Objects.hashCode, take into account parent

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* remove unused var definition

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* codegen equals/hashCode for request shapes

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* add hashCode/equals to TaggedUnion

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* use import

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* fix equals for request shapes

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

* codegen latest from main

Signed-off-by: miguel-vila <miguelvilag@gmail.com>

---------

Signed-off-by: miguel-vila <miguelvilag@gmail.com>
Signed-off-by: Miguel Vilá <miguelvilag@gmail.com>
Co-authored-by: Thomas Farr <xtansia@xtansia.com>
  • Loading branch information
miguel-vila and Xtansia authored Sep 30, 2024
1 parent 96be238 commit 18a8460
Show file tree
Hide file tree
Showing 82 changed files with 1,481 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
## [Unreleased 2.x]

### Added
- Add `hashCode` and `equals` implementations ([#312](https://github.com/opensearch-project/opensearch-java/pull/312)).

### Dependencies
- Bumps `org.junit:junit-bom` from 5.10.3 to 5.11.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,17 @@ protected static <BuilderT extends AbstractBuilder<BuilderT>> void setupAcknowle
) {
op.add(AbstractBuilder::acknowledged, JsonpDeserializer.booleanDeserializer(), "acknowledged");
}

public int hashCode() {
int result = 17;
result = 31 * result + Boolean.hashCode(this.acknowledged);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
AcknowledgedResponseBase other = (AcknowledgedResponseBase) o;
return this.acknowledged() == other.acknowledged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import jakarta.json.stream.JsonGenerator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializer;
Expand Down Expand Up @@ -281,4 +282,27 @@ protected static <BuilderT extends AbstractBuilder<BuilderT>> void setupBaseNode
op.add(AbstractBuilder::roles, JsonpDeserializer.arrayDeserializer(NodeRole._DESERIALIZER), "roles");
op.add(AbstractBuilder::transportAddress, JsonpDeserializer.stringDeserializer(), "transport_address");
}

public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(this.attributes);
result = 31 * result + Objects.hashCode(this.host);
result = 31 * result + Objects.hashCode(this.ip);
result = 31 * result + this.name.hashCode();
result = 31 * result + Objects.hashCode(this.roles);
result = 31 * result + Objects.hashCode(this.transportAddress);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
BaseNode other = (BaseNode) o;
return Objects.equals(this.attributes, other.attributes)
&& Objects.equals(this.host, other.host)
&& Objects.equals(this.ip, other.ip)
&& Objects.equals(this.name, other.name)
&& Objects.equals(this.roles, other.roles)
&& Objects.equals(this.transportAddress, other.transportAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
package org.opensearch.client.opensearch._types;

import jakarta.json.stream.JsonGenerator;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -313,4 +314,29 @@ protected static void setupBulkByScrollFailureDeserializer(ObjectDeserializer<Bu
op.add(Builder::shard, JsonpDeserializer.integerDeserializer(), "shard");
op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status");
}

public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(this.cause);
result = 31 * result + Objects.hashCode(this.id);
result = 31 * result + Objects.hashCode(this.index);
result = 31 * result + Objects.hashCode(this.node);
result = 31 * result + Objects.hashCode(this.reason);
result = 31 * result + Integer.hashCode(this.shard);
result = 31 * result + Integer.hashCode(this.status);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
BulkByScrollFailure other = (BulkByScrollFailure) o;
return Objects.equals(this.cause, other.cause)
&& Objects.equals(this.id, other.id)
&& Objects.equals(this.index, other.index)
&& Objects.equals(this.node, other.node)
&& Objects.equals(this.reason, other.reason)
&& this.shard() == other.shard()
&& this.status() == other.status();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import jakarta.json.stream.JsonGenerator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -614,4 +615,47 @@ protected static void setupBulkByScrollTaskStatusDeserializer(ObjectDeserializer
op.add(Builder::updated, JsonpDeserializer.longDeserializer(), "updated");
op.add(Builder::versionConflicts, JsonpDeserializer.longDeserializer(), "version_conflicts");
}

public int hashCode() {
int result = 17;
result = 31 * result + Integer.hashCode(this.batches);
result = 31 * result + Objects.hashCode(this.canceled);
result = 31 * result + Long.hashCode(this.created);
result = 31 * result + Long.hashCode(this.deleted);
result = 31 * result + Long.hashCode(this.noops);
result = 31 * result + Float.hashCode(this.requestsPerSecond);
result = 31 * result + this.retries.hashCode();
result = 31 * result + Integer.hashCode(this.sliceId);
result = 31 * result + Objects.hashCode(this.slices);
result = 31 * result + Objects.hashCode(this.throttled);
result = 31 * result + Long.hashCode(this.throttledMillis);
result = 31 * result + Objects.hashCode(this.throttledUntil);
result = 31 * result + Long.hashCode(this.throttledUntilMillis);
result = 31 * result + Long.hashCode(this.total);
result = 31 * result + Long.hashCode(this.updated);
result = 31 * result + Long.hashCode(this.versionConflicts);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
BulkByScrollTaskStatus other = (BulkByScrollTaskStatus) o;
return this.batches() == other.batches()
&& Objects.equals(this.canceled, other.canceled)
&& this.created() == other.created()
&& this.deleted() == other.deleted()
&& this.noops() == other.noops()
&& this.requestsPerSecond() == other.requestsPerSecond()
&& Objects.equals(this.retries, other.retries)
&& this.sliceId() == other.sliceId()
&& Objects.equals(this.slices, other.slices)
&& Objects.equals(this.throttled, other.throttled)
&& this.throttledMillis() == other.throttledMillis()
&& Objects.equals(this.throttledUntil, other.throttledUntil)
&& this.throttledUntilMillis() == other.throttledUntilMillis()
&& this.total() == other.total()
&& this.updated() == other.updated()
&& this.versionConflicts() == other.versionConflicts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,19 @@ private static JsonpDeserializer<BulkByScrollTaskStatusOrException> buildBulkByS
public static final JsonpDeserializer<BulkByScrollTaskStatusOrException> _DESERIALIZER = JsonpDeserializer.lazy(
BulkByScrollTaskStatusOrException::buildBulkByScrollTaskStatusOrExceptionDeserializer
);

public int hashCode() {
int result = 17;
result = 31 * result + _kind.hashCode();
result = 31 * result + _value.hashCode();
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
BulkByScrollTaskStatusOrException other = (BulkByScrollTaskStatusOrException) o;
return this._kind().equals(other._kind()) && this._get().equals(other._get());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -404,4 +405,29 @@ protected static void setupErrorCauseDeserializer(ObjectDeserializer<ErrorCause.
builder.metadata.put(name, JsonData._DESERIALIZER.deserialize(parser, mapper));
});
}

public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(this.causedBy);
result = 31 * result + Objects.hashCode(this.reason);
result = 31 * result + Objects.hashCode(this.rootCause);
result = 31 * result + Objects.hashCode(this.stackTrace);
result = 31 * result + Objects.hashCode(this.suppressed);
result = 31 * result + this.type.hashCode();
result = 31 * result + Objects.hashCode(this.metadata);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
ErrorCause other = (ErrorCause) o;
return Objects.equals(this.causedBy, other.causedBy)
&& Objects.equals(this.reason, other.reason)
&& Objects.equals(this.rootCause, other.rootCause)
&& Objects.equals(this.stackTrace, other.stackTrace)
&& Objects.equals(this.suppressed, other.suppressed)
&& Objects.equals(this.type, other.type)
&& Objects.equals(this.metadata, other.metadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import jakarta.json.stream.JsonGenerator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -255,4 +256,23 @@ protected static void setupNodeStatisticsDeserializer(ObjectDeserializer<NodeSta
op.add(Builder::successful, JsonpDeserializer.integerDeserializer(), "successful");
op.add(Builder::total, JsonpDeserializer.integerDeserializer(), "total");
}

public int hashCode() {
int result = 17;
result = 31 * result + Integer.hashCode(this.failed);
result = 31 * result + Objects.hashCode(this.failures);
result = 31 * result + Integer.hashCode(this.successful);
result = 31 * result + Integer.hashCode(this.total);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
NodeStatistics other = (NodeStatistics) o;
return this.failed() == other.failed()
&& Objects.equals(this.failures, other.failures)
&& this.successful() == other.successful()
&& this.total() == other.total();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
package org.opensearch.client.opensearch._types;

import jakarta.json.stream.JsonGenerator;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -351,4 +352,35 @@ protected static void setupOpenSearchVersionInfoDeserializer(ObjectDeserializer<
op.add(Builder::minimumWireCompatibilityVersion, JsonpDeserializer.stringDeserializer(), "minimum_wire_compatibility_version");
op.add(Builder::number, JsonpDeserializer.stringDeserializer(), "number");
}

public int hashCode() {
int result = 17;
result = 31 * result + this.buildDate.hashCode();
result = 31 * result + Objects.hashCode(this.buildFlavor);
result = 31 * result + this.buildHash.hashCode();
result = 31 * result + Boolean.hashCode(this.buildSnapshot);
result = 31 * result + this.buildType.hashCode();
result = 31 * result + this.distribution.hashCode();
result = 31 * result + this.luceneVersion.hashCode();
result = 31 * result + this.minimumIndexCompatibilityVersion.hashCode();
result = 31 * result + this.minimumWireCompatibilityVersion.hashCode();
result = 31 * result + this.number.hashCode();
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
OpenSearchVersionInfo other = (OpenSearchVersionInfo) o;
return Objects.equals(this.buildDate, other.buildDate)
&& Objects.equals(this.buildFlavor, other.buildFlavor)
&& Objects.equals(this.buildHash, other.buildHash)
&& this.buildSnapshot() == other.buildSnapshot()
&& Objects.equals(this.buildType, other.buildType)
&& Objects.equals(this.distribution, other.distribution)
&& Objects.equals(this.luceneVersion, other.luceneVersion)
&& Objects.equals(this.minimumIndexCompatibilityVersion, other.minimumIndexCompatibilityVersion)
&& Objects.equals(this.minimumWireCompatibilityVersion, other.minimumWireCompatibilityVersion)
&& Objects.equals(this.number, other.number);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,18 @@ protected static void setupRetriesDeserializer(ObjectDeserializer<Retries.Builde
op.add(Builder::bulk, JsonpDeserializer.longDeserializer(), "bulk");
op.add(Builder::search, JsonpDeserializer.longDeserializer(), "search");
}

public int hashCode() {
int result = 17;
result = 31 * result + Long.hashCode(this.bulk);
result = 31 * result + Long.hashCode(this.search);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
Retries other = (Retries) o;
return this.bulk() == other.bulk() && this.search() == other.search();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
package org.opensearch.client.opensearch._types;

import jakarta.json.stream.JsonGenerator;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -245,4 +246,25 @@ protected static void setupShardFailureDeserializer(ObjectDeserializer<ShardFail
op.add(Builder::shard, JsonpDeserializer.integerDeserializer(), "shard");
op.add(Builder::status, JsonpDeserializer.stringDeserializer(), "status");
}

public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(this.index);
result = 31 * result + Objects.hashCode(this.node);
result = 31 * result + this.reason.hashCode();
result = 31 * result + Integer.hashCode(this.shard);
result = 31 * result + Objects.hashCode(this.status);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
ShardFailure other = (ShardFailure) o;
return Objects.equals(this.index, other.index)
&& Objects.equals(this.node, other.node)
&& Objects.equals(this.reason, other.reason)
&& this.shard() == other.shard()
&& Objects.equals(this.status, other.status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import jakarta.json.stream.JsonGenerator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -263,4 +264,25 @@ protected static void setupShardStatisticsDeserializer(ObjectDeserializer<ShardS
op.add(Builder::successful, JsonpDeserializer.integerDeserializer(), "successful");
op.add(Builder::total, JsonpDeserializer.integerDeserializer(), "total");
}

public int hashCode() {
int result = 17;
result = 31 * result + Integer.hashCode(this.failed);
result = 31 * result + Objects.hashCode(this.failures);
result = 31 * result + Integer.hashCode(this.skipped);
result = 31 * result + Integer.hashCode(this.successful);
result = 31 * result + Integer.hashCode(this.total);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
ShardStatistics other = (ShardStatistics) o;
return this.failed() == other.failed()
&& Objects.equals(this.failures, other.failures)
&& this.skipped() == other.skipped()
&& this.successful() == other.successful()
&& this.total() == other.total();
}
}
Loading

0 comments on commit 18a8460

Please sign in to comment.