From a2d9a9f1d8c2538a4f632a6d9f3205208a4f2da0 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 29 Dec 2018 14:21:20 -0500 Subject: [PATCH] Clarifications, same for QueryResponse. --- .../api/proxy/server/QueryResponse.java | 71 ++++++++++++++++++- .../api/proxy/server/ServerPing.java | 4 ++ .../api/proxy/server/QueryResponseTest.java | 19 +++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java b/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java index 7530e54d6..9d7c54749 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java @@ -1,5 +1,6 @@ package com.velocitypowered.api.proxy.server; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -8,6 +9,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.Optional; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -28,7 +30,8 @@ public final class QueryResponse { private final String proxyVersion; private final Collection plugins; - private QueryResponse(String hostname, String gameVersion, String map, int currentPlayers, + @VisibleForTesting + QueryResponse(String hostname, String gameVersion, String map, int currentPlayers, int maxPlayers, String proxyHost, int proxyPort, Collection players, String proxyVersion, Collection plugins) { this.hostname = hostname; @@ -138,7 +141,10 @@ public final class QueryResponse { /** - * Creates a new {@link Builder} instance from data represented by this response. + * Creates a new {@link Builder} instance from data represented by this response, so that you + * may create a new {@link QueryResponse} with new data. It is guaranteed that + * {@code queryResponse.toBuilder().build().equals(queryResponse)}: that is, if no other + * changes are made to the returned builder, the built instance will equal the original instance. * * @return {@link QueryResponse} builder */ @@ -165,6 +171,50 @@ public final class QueryResponse { return new Builder(); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + QueryResponse response = (QueryResponse) o; + return currentPlayers == response.currentPlayers + && maxPlayers == response.maxPlayers + && proxyPort == response.proxyPort + && hostname.equals(response.hostname) + && gameVersion.equals(response.gameVersion) + && map.equals(response.map) + && proxyHost.equals(response.proxyHost) + && players.equals(response.players) + && proxyVersion.equals(response.proxyVersion) + && plugins.equals(response.plugins); + } + + @Override + public int hashCode() { + return Objects + .hash(hostname, gameVersion, map, currentPlayers, maxPlayers, proxyHost, proxyPort, players, + proxyVersion, plugins); + } + + @Override + public String toString() { + return "QueryResponse{" + + "hostname='" + hostname + '\'' + + ", gameVersion='" + gameVersion + '\'' + + ", map='" + map + '\'' + + ", currentPlayers=" + currentPlayers + + ", maxPlayers=" + maxPlayers + + ", proxyHost='" + proxyHost + '\'' + + ", proxyPort=" + proxyPort + + ", players=" + players + + ", proxyVersion='" + proxyVersion + '\'' + + ", plugins=" + plugins + + '}'; + } + /** * A builder for {@link QueryResponse} objects. */ @@ -392,5 +442,22 @@ public final class QueryResponse { .add("version", version) .toString(); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PluginInformation that = (PluginInformation) o; + return name.equals(that.name) && Objects.equals(version, that.version); + } + + @Override + public int hashCode() { + return Objects.hash(name, version); + } } } diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java index 66483ae33..f571c4517 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java @@ -33,6 +33,7 @@ public final class ServerPing { /** * Constructs a ServerPing instance. + * * @param version the version of the server * @param players the players on the server * @param description the MOTD for the server @@ -102,6 +103,9 @@ public final class ServerPing { /** * Returns a copy of this {@link ServerPing} instance as a builder so that it can be modified. + * It is guaranteed that {@code ping.asBuilder().ping().equals(ping)}: that is, if no other + * changes are made to the returned builder, the built instance will equal the original instance. + * * @return a copy of this instance as a {@link Builder} */ public Builder asBuilder() { diff --git a/api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java b/api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java new file mode 100644 index 000000000..ffeeccb20 --- /dev/null +++ b/api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java @@ -0,0 +1,19 @@ +package com.velocitypowered.api.proxy.server; + +import static org.junit.jupiter.api.Assertions.*; + +import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.proxy.server.QueryResponse.PluginInformation; +import org.junit.jupiter.api.Test; + +class QueryResponseTest { + + @Test + void toBuilderConsistency() { + QueryResponse response = new QueryResponse("test", "test", "test", + 1, 2, "test", 1234, ImmutableList.of("tuxed"), + "0.0.1", ImmutableList.of(new PluginInformation("test", "1.0.0"), + new PluginInformation("test2", null))); + assertEquals(response, response.toBuilder().build()); + } +} \ No newline at end of file