Skip to content

Commit

Permalink
[Backport 2.x] Codegen equals & hashCode (#1217)
Browse files Browse the repository at this point in the history
* Generate hashCode and equals (#1201)

* 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]>
Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit 18a8460)

* Re-run codegen (#1216)

* Re-run codegen

Signed-off-by: Thomas Farr <[email protected]>

* Improve null handling

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit 897cc9b)
  • Loading branch information
Xtansia authored Oct 1, 2024
1 parent 5b90fb3 commit 329fa95
Show file tree
Hide file tree
Showing 56 changed files with 964 additions and 40 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,8 +40,10 @@
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.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
Expand All @@ -66,15 +68,19 @@ public class ErrorCause implements PlainJsonSerializable {
@Nullable
private final String reason;

@Nonnull
private final List<ErrorCause> rootCause;

@Nullable
private final String stackTrace;

@Nonnull
private final List<ErrorCause> suppressed;

@Nonnull
private final String type;

@Nonnull
private final Map<String, JsonData> metadata;

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -115,6 +121,7 @@ public final String reason() {
/**
* API name: {@code root_cause}
*/
@Nonnull
public final List<ErrorCause> rootCause() {
return this.rootCause;
}
Expand All @@ -133,6 +140,7 @@ public final String stackTrace() {
/**
* API name: {@code suppressed}
*/
@Nonnull
public final List<ErrorCause> suppressed() {
return this.suppressed;
}
Expand All @@ -143,13 +151,15 @@ public final List<ErrorCause> suppressed() {
* API name: {@code type}
* </p>
*/
@Nonnull
public final String type() {
return this.type;
}

/**
* Additional details about the error.
*/
@Nonnull
public final Map<String, JsonData> metadata() {
return this.metadata;
}
Expand Down Expand Up @@ -404,4 +414,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 (o == null || 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)
&& this.type.equals(other.type)
&& Objects.equals(this.metadata, other.metadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
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.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
Expand All @@ -62,6 +64,7 @@ public class ShardFailure implements PlainJsonSerializable {
@Nullable
private final String node;

@Nonnull
private final ErrorCause reason;

private final int shard;
Expand Down Expand Up @@ -102,6 +105,7 @@ public final String node() {
/**
* Required - API name: {@code reason}
*/
@Nonnull
public final ErrorCause reason() {
return this.reason;
}
Expand Down Expand Up @@ -245,4 +249,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 (o == null || this.getClass() != o.getClass()) return false;
ShardFailure other = (ShardFailure) o;
return Objects.equals(this.index, other.index)
&& Objects.equals(this.node, other.node)
&& this.reason.equals(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,8 +38,10 @@

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.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
Expand All @@ -57,15 +59,19 @@
@Generated("org.opensearch.client.codegen.CodeGenerator")
public class ShardStatistics implements PlainJsonSerializable {

@Nonnull
private final Number failed;

@Nonnull
private final List<ShardFailure> failures;

@Nullable
private final Number skipped;

@Nonnull
private final Number successful;

@Nonnull
private final Number total;

// ---------------------------------------------------------------------------------------------
Expand All @@ -85,13 +91,15 @@ public static ShardStatistics of(Function<ShardStatistics.Builder, ObjectBuilder
/**
* Required - API name: {@code failed}
*/
@Nonnull
public final Number failed() {
return this.failed;
}

/**
* API name: {@code failures}
*/
@Nonnull
public final List<ShardFailure> failures() {
return this.failures;
}
Expand All @@ -107,13 +115,15 @@ public final Number skipped() {
/**
* Required - API name: {@code successful}
*/
@Nonnull
public final Number successful() {
return this.successful;
}

/**
* Required - API name: {@code total}
*/
@Nonnull
public final Number total() {
return this.total;
}
Expand Down Expand Up @@ -263,4 +273,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 (o == null || this.getClass() != o.getClass()) return false;
ShardStatistics other = (ShardStatistics) o;
return this.failed.equals(other.failed)
&& Objects.equals(this.failures, other.failures)
&& Objects.equals(this.skipped, other.skipped)
&& this.successful.equals(other.successful)
&& this.total.equals(other.total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
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.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
Expand All @@ -56,16 +58,20 @@ public abstract class WriteResponseBase implements PlainJsonSerializable {
@Nullable
private final Boolean forcedRefresh;

@Nonnull
private final String id;

@Nonnull
private final String index;

private final long primaryTerm;

@Nonnull
private final Result result;

private final long seqNo;

@Nonnull
private final ShardStatistics shards;

private final long version;
Expand Down Expand Up @@ -94,13 +100,15 @@ public final Boolean forcedRefresh() {
/**
* Required - API name: {@code _id}
*/
@Nonnull
public final String id() {
return this.id;
}

/**
* Required - API name: {@code _index}
*/
@Nonnull
public final String index() {
return this.index;
}
Expand All @@ -115,6 +123,7 @@ public final long primaryTerm() {
/**
* Required - API name: {@code result}
*/
@Nonnull
public final Result result() {
return this.result;
}
Expand All @@ -129,6 +138,7 @@ public final long seqNo() {
/**
* Required - API name: {@code _shards}
*/
@Nonnull
public final ShardStatistics shards() {
return this.shards;
}
Expand Down Expand Up @@ -277,4 +287,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 + Objects.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 (o == null || this.getClass() != o.getClass()) return false;
WriteResponseBase other = (WriteResponseBase) o;
return Objects.equals(this.forcedRefresh, other.forcedRefresh)
&& this.id.equals(other.id)
&& this.index.equals(other.index)
&& this.primaryTerm == other.primaryTerm
&& this.result.equals(other.result)
&& this.seqNo == other.seqNo
&& this.shards.equals(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 (o == null || 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);
}
}
Loading

0 comments on commit 329fa95

Please sign in to comment.