Skip to content

Commit

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

Signed-off-by: miguel-vila <[email protected]>

* add changelog entry

Signed-off-by: miguel-vila <[email protected]>

* remove change

Signed-off-by: miguel-vila <[email protected]>

* take into account primitives

Signed-off-by: miguel-vila <[email protected]>

* refactor and format

Signed-off-by: miguel-vila <[email protected]>

* use Object.equals

Co-authored-by: Thomas Farr <[email protected]>
Signed-off-by: Miguel Vilá <[email protected]>

* use `&&` chain

Signed-off-by: miguel-vila <[email protected]>

* adjust last line

Signed-off-by: miguel-vila <[email protected]>

* use fqn

Signed-off-by: miguel-vila <[email protected]>

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

Signed-off-by: miguel-vila <[email protected]>

* remove unused var definition

Signed-off-by: miguel-vila <[email protected]>

* codegen equals/hashCode for request shapes

Signed-off-by: miguel-vila <[email protected]>

* add hashCode/equals to TaggedUnion

Signed-off-by: miguel-vila <[email protected]>

* use import

Signed-off-by: miguel-vila <[email protected]>

* fix equals for request shapes

Signed-off-by: miguel-vila <[email protected]>

* codegen latest from main

Signed-off-by: miguel-vila <[email protected]>

---------

Signed-off-by: miguel-vila <[email protected]>
Signed-off-by: Miguel Vilá <[email protected]>
Co-authored-by: Thomas Farr <[email protected]>
(cherry picked from commit 18a8460)
  • Loading branch information
Xtansia committed Oct 1, 2024
1 parent 5b90fb3 commit d0d1bc0
Show file tree
Hide file tree
Showing 51 changed files with 796 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [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.1
Expand Down
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 @@ -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.numberDeserializer(), "successful");
op.add(Builder::total, JsonpDeserializer.numberDeserializer(), "total");
}

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

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
ShardStatistics other = (ShardStatistics) o;
return Objects.equals(this.failed, other.failed)
&& Objects.equals(this.failures, other.failures)
&& Objects.equals(this.skipped, other.skipped)
&& Objects.equals(this.successful, other.successful)
&& Objects.equals(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 @@ -277,4 +278,31 @@ protected static <BuilderT extends AbstractBuilder<BuilderT>> void setupWriteRes
op.add(AbstractBuilder::shards, ShardStatistics._DESERIALIZER, "_shards");
op.add(AbstractBuilder::version, JsonpDeserializer.longDeserializer(), "_version");
}

public int hashCode() {
int result = 17;
result = 31 * result + Boolean.hashCode(this.forcedRefresh);
result = 31 * result + this.id.hashCode();
result = 31 * result + this.index.hashCode();
result = 31 * result + Long.hashCode(this.primaryTerm);
result = 31 * result + this.result.hashCode();
result = 31 * result + Long.hashCode(this.seqNo);
result = 31 * result + this.shards.hashCode();
result = 31 * result + Long.hashCode(this.version);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
WriteResponseBase other = (WriteResponseBase) o;
return this.forcedRefresh() == other.forcedRefresh()
&& Objects.equals(this.id, other.id)
&& Objects.equals(this.index, other.index)
&& this.primaryTerm() == other.primaryTerm()
&& Objects.equals(this.result, other.result)
&& this.seqNo() == other.seqNo()
&& Objects.equals(this.shards, other.shards)
&& this.version() == other.version();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.opensearch.client.opensearch.ml;

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 @@ -286,4 +287,29 @@ protected static void setupActionDeserializer(ObjectDeserializer<Action.Builder>
op.add(Builder::requestBody, JsonpDeserializer.stringDeserializer(), "request_body");
op.add(Builder::url, JsonpDeserializer.stringDeserializer(), "url");
}

public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(this.actionType);
result = 31 * result + Objects.hashCode(this.headers);
result = 31 * result + Objects.hashCode(this.method);
result = 31 * result + Objects.hashCode(this.postProcessFunction);
result = 31 * result + Objects.hashCode(this.preProcessFunction);
result = 31 * result + Objects.hashCode(this.requestBody);
result = 31 * result + Objects.hashCode(this.url);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
Action other = (Action) o;
return Objects.equals(this.actionType, other.actionType)
&& Objects.equals(this.headers, other.headers)
&& Objects.equals(this.method, other.method)
&& Objects.equals(this.postProcessFunction, other.postProcessFunction)
&& Objects.equals(this.preProcessFunction, other.preProcessFunction)
&& Objects.equals(this.requestBody, other.requestBody)
&& Objects.equals(this.url, other.url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.opensearch.client.opensearch.ml;

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 @@ -279,4 +280,29 @@ protected static void setupClientConfigDeserializer(ObjectDeserializer<ClientCon
op.add(Builder::retryBackoffPolicy, JsonpDeserializer.stringDeserializer(), "retry_backoff_policy");
op.add(Builder::retryTimeoutSeconds, JsonpDeserializer.integerDeserializer(), "retry_timeout_seconds");
}

public int hashCode() {
int result = 17;
result = 31 * result + Integer.hashCode(this.connectionTimeout);
result = 31 * result + Integer.hashCode(this.maxConnection);
result = 31 * result + Integer.hashCode(this.maxRetryTimes);
result = 31 * result + Integer.hashCode(this.readTimeout);
result = 31 * result + Integer.hashCode(this.retryBackoffMillis);
result = 31 * result + Objects.hashCode(this.retryBackoffPolicy);
result = 31 * result + Integer.hashCode(this.retryTimeoutSeconds);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
ClientConfig other = (ClientConfig) o;
return this.connectionTimeout() == other.connectionTimeout()
&& this.maxConnection() == other.maxConnection()
&& this.maxRetryTimes() == other.maxRetryTimes()
&& this.readTimeout() == other.readTimeout()
&& this.retryBackoffMillis() == other.retryBackoffMillis()
&& Objects.equals(this.retryBackoffPolicy, other.retryBackoffPolicy)
&& this.retryTimeoutSeconds() == other.retryTimeoutSeconds();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import jakarta.json.stream.JsonGenerator;
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 @@ -365,4 +366,31 @@ protected static void setupCreateConnectorRequestDeserializer(ObjectDeserializer
true,
CreateConnectorResponse._DESERIALIZER
);

public int hashCode() {
int result = 17;
result = 31 * result + this.actions.hashCode();
result = 31 * result + Objects.hashCode(this.clientConfig);
result = 31 * result + this.credential.hashCode();
result = 31 * result + this.description.hashCode();
result = 31 * result + this.name.hashCode();
result = 31 * result + this.parameters.hashCode();
result = 31 * result + this.protocol.hashCode();
result = 31 * result + Integer.hashCode(this.version);
return result;
}

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
CreateConnectorRequest other = (CreateConnectorRequest) o;
return Objects.equals(this.actions, other.actions)
&& Objects.equals(this.clientConfig, other.clientConfig)
&& Objects.equals(this.credential, other.credential)
&& Objects.equals(this.description, other.description)
&& Objects.equals(this.name, other.name)
&& Objects.equals(this.parameters, other.parameters)
&& Objects.equals(this.protocol, other.protocol)
&& this.version() == other.version();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.opensearch.client.opensearch.ml;

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 @@ -111,4 +112,17 @@ public CreateConnectorResponse build() {
protected static void setupCreateConnectorResponseDeserializer(ObjectDeserializer<CreateConnectorResponse.Builder> op) {
op.add(Builder::connectorId, JsonpDeserializer.stringDeserializer(), "connector_id");
}

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

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
CreateConnectorResponse other = (CreateConnectorResponse) o;
return Objects.equals(this.connectorId, other.connectorId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import jakarta.json.stream.JsonGenerator;
import java.util.HashMap;
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 @@ -214,4 +215,23 @@ protected static void setupCredentialDeserializer(ObjectDeserializer<Credential.
builder.metadata.put(name, JsonData._DESERIALIZER.deserialize(parser, mapper));
});
}

public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(this.accessKey);
result = 31 * result + Objects.hashCode(this.secretKey);
result = 31 * result + Objects.hashCode(this.sessionToken);
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;
Credential other = (Credential) o;
return Objects.equals(this.accessKey, other.accessKey)
&& Objects.equals(this.secretKey, other.secretKey)
&& Objects.equals(this.sessionToken, other.sessionToken)
&& Objects.equals(this.metadata, other.metadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

package org.opensearch.client.opensearch.ml;

import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import org.opensearch.client.opensearch._types.ErrorResponse;
Expand Down Expand Up @@ -98,4 +99,17 @@ public DeleteAgentRequest build() {
false,
DeleteAgentResponse._DESERIALIZER
);

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

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
DeleteAgentRequest other = (DeleteAgentRequest) o;
return Objects.equals(this.agentId, other.agentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ public DeleteAgentResponse build() {
protected static void setupDeleteAgentResponseDeserializer(ObjectDeserializer<DeleteAgentResponse.Builder> op) {
WriteResponseBase.setupWriteResponseBaseDeserializer(op);
}

public int hashCode() {
int result = super.hashCode();
return result;
}

public boolean equals(Object o) {
if (!super.equals(o)) {
return false;
}
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

package org.opensearch.client.opensearch.ml;

import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Generated;
import org.opensearch.client.opensearch._types.ErrorResponse;
Expand Down Expand Up @@ -98,4 +99,17 @@ public DeleteConnectorRequest build() {
false,
DeleteConnectorResponse._DESERIALIZER
);

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

public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
DeleteConnectorRequest other = (DeleteConnectorRequest) o;
return Objects.equals(this.connectorId, other.connectorId);
}
}
Loading

0 comments on commit d0d1bc0

Please sign in to comment.