Skip to content

Commit

Permalink
Simplify equals, hashCode and toString methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Jan 8, 2024
1 parent fbed43a commit 05a8011
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 482 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Map;
import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;

public class ClusterStats
{
private int runningQueryCount;
Expand Down Expand Up @@ -131,98 +133,48 @@ public void setUserQueuedCount(Map<String, Integer> userQueuedCount)
this.userQueuedCount = userQueuedCount;
}

public boolean equals(final Object o)
@Override
public boolean equals(Object o)
{
if (o == this) {
if (this == o) {
return true;
}
if (!(o instanceof ClusterStats other)) {
return false;
}
if (!other.canEqual(this)) {
return false;
}
if (this.getRunningQueryCount() != other.getRunningQueryCount()) {
return false;
}
if (this.getQueuedQueryCount() != other.getQueuedQueryCount()) {
return false;
}
if (this.getBlockedQueryCount() != other.getBlockedQueryCount()) {
return false;
}
if (this.getNumWorkerNodes() != other.getNumWorkerNodes()) {
return false;
}
if (this.isHealthy() != other.isHealthy()) {
return false;
}
final Object clusterId = this.getClusterId();
final Object otherClusterId = other.getClusterId();
if (!Objects.equals(clusterId, otherClusterId)) {
return false;
}
final Object proxyTo = this.getProxyTo();
final Object otherProxyTo = other.getProxyTo();
if (!Objects.equals(proxyTo, otherProxyTo)) {
return false;
}
final Object externalUrl = this.getExternalUrl();
final Object otherExternalUrl = other.getExternalUrl();
if (!Objects.equals(externalUrl, otherExternalUrl)) {
if (o == null || getClass() != o.getClass()) {
return false;
}
final Object routingGroup = this.getRoutingGroup();
final Object otherRoutingGroup = other.getRoutingGroup();
if (!Objects.equals(routingGroup, otherRoutingGroup)) {
return false;
}
final Object userQueuedCount = this.getUserQueuedCount();
final Object otherUserQueuedCount = other.getUserQueuedCount();
return Objects.equals(userQueuedCount, otherUserQueuedCount);
}

protected boolean canEqual(final Object other)
{
return other instanceof ClusterStats;
ClusterStats stats = (ClusterStats) o;
return runningQueryCount == stats.runningQueryCount &&
queuedQueryCount == stats.queuedQueryCount &&
blockedQueryCount == stats.blockedQueryCount &&
numWorkerNodes == stats.numWorkerNodes &&
healthy == stats.healthy &&
Objects.equals(clusterId, stats.clusterId) &&
Objects.equals(proxyTo, stats.proxyTo) &&
Objects.equals(externalUrl, stats.externalUrl) &&
Objects.equals(routingGroup, stats.routingGroup) &&
Objects.equals(userQueuedCount, stats.userQueuedCount);
}

@Override
public int hashCode()
{
final int prime = 59;
int result = 1;
result = result * prime + this.getRunningQueryCount();
result = result * prime + this.getQueuedQueryCount();
result = result * prime + this.getBlockedQueryCount();
result = result * prime + this.getNumWorkerNodes();
result = result * prime + (this.isHealthy() ? 79 : 97);
final Object clusterId = this.getClusterId();
result = result * prime + (clusterId == null ? 43 : clusterId.hashCode());
final Object proxyTo = this.getProxyTo();
result = result * prime + (proxyTo == null ? 43 : proxyTo.hashCode());
final Object externalUrl = this.getExternalUrl();
result = result * prime + (externalUrl == null ? 43 : externalUrl.hashCode());
final Object routingGroup = this.getRoutingGroup();
result = result * prime + (routingGroup == null ? 43 : routingGroup.hashCode());
final Object userQueuedCount = this.getUserQueuedCount();
result = result * prime + (userQueuedCount == null ? 43 : userQueuedCount.hashCode());
return result;
return Objects.hash(runningQueryCount, queuedQueryCount, blockedQueryCount, numWorkerNodes, healthy, clusterId, proxyTo, externalUrl, routingGroup, userQueuedCount);
}

@Override
public String toString()
{
return "ClusterStats{" +
"runningQueryCount=" + runningQueryCount +
", queuedQueryCount=" + queuedQueryCount +
", blockedQueryCount=" + blockedQueryCount +
", numWorkerNodes=" + numWorkerNodes +
", healthy=" + healthy +
", clusterId='" + clusterId + '\'' +
", proxyTo='" + proxyTo + '\'' +
", externalUrl='" + externalUrl + '\'' +
", routingGroup='" + routingGroup + '\'' +
", userQueuedCount=" + userQueuedCount +
'}';
return toStringHelper(this)
.add("runningQueryCount", runningQueryCount)
.add("queuedQueryCount", queuedQueryCount)
.add("blockedQueryCount", blockedQueryCount)
.add("numWorkerNodes", numWorkerNodes)
.add("healthy", healthy)
.add("clusterId", clusterId)
.add("proxyTo", proxyTo)
.add("externalUrl", externalUrl)
.add("routingGroup", routingGroup)
.add("userQueuedCount", userQueuedCount)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.List;
import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static io.trino.gateway.ha.router.ResourceGroupsManager.ResourceGroupsDetail;
import static io.trino.gateway.ha.router.ResourceGroupsManager.SelectorsDetail;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -172,39 +173,31 @@ public void setDisplayName(String displayName)
this.displayName = displayName;
}

public boolean equals(final Object o)
@Override
public boolean equals(Object o)
{
if (o == this) {
if (this == o) {
return true;
}
if (!(o instanceof EntityView other)) {
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!other.canEqual(this)) {
return false;
}
final Object displayName = this.getDisplayName();
final Object otherDisplayName = other.getDisplayName();
return Objects.equals(displayName, otherDisplayName);
}

protected boolean canEqual(final Object other)
{
return other instanceof EntityView;
EntityView that = (EntityView) o;
return Objects.equals(displayName, that.displayName);
}

@Override
public int hashCode()
{
final int prime = 59;
int result = 1;
final Object displayName = this.getDisplayName();
result = result * prime + (displayName == null ? 43 : displayName.hashCode());
return result;
return Objects.hash(displayName);
}

@Override
public String toString()
{
return "EntityEditorResource.EntityView(displayName=" + this.getDisplayName() + ")";
return toStringHelper(this)
.add("displayName", displayName)
.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

@RolesAllowed("USER")
Expand Down Expand Up @@ -230,78 +231,41 @@ public void setQueryDistribution(Map<String, Integer> queryDistribution)
this.queryDistribution = queryDistribution;
}

public boolean equals(final Object o)
@Override
public boolean equals(Object o)
{
if (o == this) {
if (this == o) {
return true;
}
if (!(o instanceof GatewayView other)) {
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!other.canEqual(this)) {
return false;
}
if (this.getGatewayStartTime() != other.getGatewayStartTime()) {
return false;
}
final Object displayName = this.getDisplayName();
final Object otherDisplayName = other.getDisplayName();
if (!Objects.equals(displayName, otherDisplayName)) {
return false;
}
final Object backendConfigurations = this.getBackendConfigurations();
final Object otherBackendConfigurations = other.getBackendConfigurations();
if (!Objects.equals(backendConfigurations, otherBackendConfigurations)) {
return false;
}
final Object queryHistory = this.getQueryHistory();
final Object otherQueryHistory = other.getQueryHistory();
if (!Objects.equals(queryHistory, otherQueryHistory)) {
return false;
}
final Object backendStates = this.getBackendStates();
final Object otherBackendStates = other.getBackendStates();
if (!Objects.equals(backendStates, otherBackendStates)) {
return false;
}
final Object queryDistribution = this.getQueryDistribution();
final Object otherQueryDistribution = other.getQueryDistribution();
return Objects.equals(queryDistribution, otherQueryDistribution);
}

protected boolean canEqual(final Object other)
{
return other instanceof GatewayView;
GatewayView that = (GatewayView) o;
return gatewayStartTime == that.gatewayStartTime &&
Objects.equals(displayName, that.displayName) &&
Objects.equals(backendConfigurations, that.backendConfigurations) &&
Objects.equals(queryHistory, that.queryHistory) &&
Objects.equals(backendStates, that.backendStates) &&
Objects.equals(queryDistribution, that.queryDistribution);
}

@Override
public int hashCode()
{
final int prime = 59;
int result = 1;
final long gatewayStartTime = this.getGatewayStartTime();
result = result * prime + (int) (gatewayStartTime >>> 32 ^ gatewayStartTime);
final Object displayName = this.getDisplayName();
result = result * prime + (displayName == null ? 43 : displayName.hashCode());
final Object backendConfigurations = this.getBackendConfigurations();
result = result * prime + (backendConfigurations == null ? 43 : backendConfigurations.hashCode());
final Object queryHistory = this.getQueryHistory();
result = result * prime + (queryHistory == null ? 43 : queryHistory.hashCode());
final Object backendStates = this.getBackendStates();
result = result * prime + (backendStates == null ? 43 : backendStates.hashCode());
final Object queryDistribution = this.getQueryDistribution();
result = result * prime + (queryDistribution == null ? 43 : queryDistribution.hashCode());
return result;
return Objects.hash(gatewayStartTime, displayName, backendConfigurations, queryHistory, backendStates, queryDistribution);
}

@Override
public String toString()
{
return "GatewayViewResource.GatewayView(gatewayStartTime="
+ this.getGatewayStartTime() + ", displayName="
+ this.getDisplayName() + ", backendConfigurations="
+ this.getBackendConfigurations() + ", queryHistory="
+ this.getQueryHistory() + ", backendStates="
+ this.getBackendStates() + ", queryDistribution="
+ this.getQueryDistribution() + ")";
return toStringHelper(this)
.add("gatewayStartTime", gatewayStartTime)
.add("displayName", displayName)
.add("backendConfigurations", backendConfigurations)
.add("queryHistory", queryHistory)
.add("backendStates", backendStates)
.add("queryDistribution", queryDistribution)
.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;
import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;

public class BackendStateManager
{
@Nullable
Expand Down Expand Up @@ -76,46 +78,32 @@ public Map<String, Integer> getState()
return this.state;
}

public boolean equals(final Object o)
@Override
public boolean equals(Object o)
{
if (o == this) {
if (this == o) {
return true;
}
if (!(o instanceof BackendState other)) {
return false;
}
if (!other.canEqual(this)) {
return false;
}
final Object name = this.getName();
final Object otherName = other.getName();
if (!Objects.equals(name, otherName)) {
if (o == null || getClass() != o.getClass()) {
return false;
}
final Object state = this.getState();
final Object otherState = other.getState();
return Objects.equals(state, otherState);
}

protected boolean canEqual(final Object other)
{
return other instanceof BackendState;
BackendState that = (BackendState) o;
return Objects.equals(name, that.name) && Objects.equals(state, that.state);
}

@Override
public int hashCode()
{
final int prime = 59;
int result = 1;
final Object name = this.getName();
result = result * prime + (name == null ? 43 : name.hashCode());
final Object state = this.getState();
result = result * prime + (state == null ? 43 : state.hashCode());
return result;
return Objects.hash(name, state);
}

@Override
public String toString()
{
return "BackendStateManager.BackendState(name=" + this.getName() + ", state=" + this.getState() + ")";
return toStringHelper(this)
.add("name", name)
.add("state", state)
.toString();
}
}
}
Loading

0 comments on commit 05a8011

Please sign in to comment.