Change equals() & hashCode() to using Java 7 Methods

Change toString() to using Apache Lang3 ToStringHelper
Further Cleanup of Tests
This commit is contained in:
Fuzzlemann 2017-08-31 20:39:38 +02:00
parent dc2b139ac6
commit 3579a84a75
14 changed files with 124 additions and 143 deletions

View File

@ -4,11 +4,12 @@
*/
package main.java.com.djrapitops.plan.data;
import com.google.common.base.Objects;
import main.java.com.djrapitops.plan.database.tables.Actions;
import main.java.com.djrapitops.plan.utilities.FormatUtils;
import main.java.com.djrapitops.plan.utilities.html.Html;
import java.util.Objects;
/**
* Class that represents an action made by a player.
*
@ -54,11 +55,6 @@ public class Action {
return serverID;
}
@Override
public String toString() {
return Html.TABLELINE_3.parse(FormatUtils.formatTimeStampYear(date), doneAction.toString(), additionalInfo);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -67,11 +63,16 @@ public class Action {
return date == action.date &&
serverID == action.serverID &&
doneAction == action.doneAction &&
Objects.equal(additionalInfo, action.additionalInfo);
Objects.equals(additionalInfo, action.additionalInfo);
}
@Override
public int hashCode() {
return Objects.hashCode(date, doneAction, additionalInfo, serverID);
return Objects.hash(date, doneAction, additionalInfo, serverID);
}
@Override
public String toString() {
return Html.TABLELINE_3.parse(FormatUtils.formatTimeStampYear(date), doneAction.toString(), additionalInfo);
}
}

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.data;
import main.java.com.djrapitops.plan.database.tables.Actions;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.util.Objects;
import java.util.UUID;
@ -57,39 +58,31 @@ public class PlayerKill {
return weapon;
}
@Override
public String toString() {
return "{victim:" + victim + "|time:" + time + "|weapon:" + weapon + '}';
public Action convertToAction() {
return new Action(time, Actions.KILLED, "name with " + weapon); // TODO Name Cache.
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PlayerKill other = (PlayerKill) obj;
return this.time == other.time
&& Objects.equals(this.weapon, other.weapon)
&& Objects.equals(this.victim, other.victim);
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlayerKill that = (PlayerKill) o;
return time == that.time &&
Objects.equals(victim, that.victim) &&
Objects.equals(weapon, that.weapon);
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + Objects.hashCode(this.victim);
hash = 89 * hash + (int) (this.time ^ (this.time >>> 32));
hash = 89 * hash + Objects.hashCode(this.weapon);
return hash;
return Objects.hash(victim, time, weapon);
}
public Action convertToAction() {
return new Action(time, Actions.KILLED, "name with " + weapon); // TODO Name Cache.
@Override
public String toString() {
return new ToStringBuilder(this)
.append("victim", victim)
.append("time", time)
.append("weapon", weapon)
.toString();
}
}

View File

@ -5,6 +5,7 @@ import main.java.com.djrapitops.plan.data.time.WorldTimes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
/**
* Object for storing various information about a player's play session.
@ -162,29 +163,6 @@ public class Session {
return deaths;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Session other = (Session) obj;
return this.sessionStart == other.sessionStart && this.sessionEnd == other.sessionEnd;
}
@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + (int) (this.sessionStart ^ (this.sessionStart >>> 32));
hash = 97 * hash + (int) (this.sessionEnd ^ (this.sessionEnd >>> 32));
return hash;
}
public boolean isFetchedFromDB() {
return sessionID != null;
}
@ -202,4 +180,23 @@ public class Session {
public void setSessionID(int sessionID) {
this.sessionID = sessionID;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Session session = (Session) o;
return sessionStart == session.sessionStart &&
sessionEnd == session.sessionEnd &&
mobKills == session.mobKills &&
deaths == session.deaths &&
Objects.equals(sessionID, session.sessionID) &&
Objects.equals(worldTimes, session.worldTimes) &&
Objects.equals(playerKills, session.playerKills);
}
@Override
public int hashCode() {
return Objects.hash(sessionStart, sessionID, worldTimes, sessionEnd, playerKills, mobKills, deaths);
}
}

View File

@ -5,7 +5,9 @@
*/
package main.java.com.djrapitops.plan.data;
import com.google.common.base.Objects;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.util.Objects;
/**
* Class containing single datapoint of TPS / Players online / CPU Usage / Used Memory / Entity Count / Chunks loaded.
@ -123,19 +125,19 @@ public class TPS {
@Override
public int hashCode() {
return Objects.hashCode(date, ticksPerSecond, players, cpuUsage, usedMemory, entityCount, chunksLoaded);
return Objects.hash(date, ticksPerSecond, players, cpuUsage, usedMemory, entityCount, chunksLoaded);
}
@Override
public String toString() {
return "TPS{" +
"date=" + date +
", ticksPerSecond=" + ticksPerSecond +
", players=" + players +
", cpuUsage=" + cpuUsage +
", usedMemory=" + usedMemory +
", entityCount=" + entityCount +
", chunksLoaded=" + chunksLoaded +
'}';
return new ToStringBuilder(this)
.append("date", date)
.append("ticksPerSecond", ticksPerSecond)
.append("players", players)
.append("cpuUsage", cpuUsage)
.append("usedMemory", usedMemory)
.append("entityCount", entityCount)
.append("chunksLoaded", chunksLoaded)
.toString();
}
}

View File

@ -1,7 +1,6 @@
package main.java.com.djrapitops.plan.data;
import com.google.common.base.Objects;
import java.util.Objects;
import java.util.UUID;
/**
@ -79,12 +78,12 @@ public class UserInfo {
lastSeen == userInfo.lastSeen &&
banned == userInfo.banned &&
opped == userInfo.opped &&
Objects.equal(uuid, userInfo.uuid) &&
Objects.equal(name, userInfo.name);
Objects.equals(uuid, userInfo.uuid) &&
Objects.equals(name, userInfo.name);
}
@Override
public int hashCode() {
return Objects.hashCode(uuid, name, registered, lastSeen, banned, opped);
return Objects.hash(uuid, name, registered, lastSeen, banned, opped);
}
}

View File

@ -1,5 +1,7 @@
package main.java.com.djrapitops.plan.data;
import java.util.Objects;
/**
* Object containing webserver security user information.
*
@ -34,19 +36,14 @@ public class WebUser {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WebUser webUser = (WebUser) o;
return permLevel == webUser.permLevel
&& user.equals(webUser.user)
&& saltedPassHash.equals(webUser.saltedPassHash);
return permLevel == webUser.permLevel &&
Objects.equals(user, webUser.user) &&
Objects.equals(saltedPassHash, webUser.saltedPassHash);
}
@Override
public int hashCode() {
int result = user.hashCode();
result = 31 * result + saltedPassHash.hashCode();
result = 31 * result + permLevel;
return result;
return Objects.hash(user, saltedPassHash, permLevel);
}
}

View File

@ -1,9 +1,11 @@
package main.java.com.djrapitops.plan.data.time;
import com.djrapitops.plugin.utilities.Verify;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Abstract class for keeping track of time spent in each state.
@ -140,28 +142,23 @@ public abstract class TimeKeeper {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimeKeeper that = (TimeKeeper) o;
return lastStateChange == that.lastStateChange
&& (times != null ? times.equals(that.times) : that.times == null)
&& (state != null ? state.equals(that.state) : that.state == null);
return lastStateChange == that.lastStateChange &&
Objects.equals(times, that.times) &&
Objects.equals(state, that.state);
}
@Override
public int hashCode() {
int result = times != null ? times.hashCode() : 0;
result = 31 * result + (state != null ? state.hashCode() : 0);
result = 31 * result + (int) (lastStateChange ^ (lastStateChange >>> 32));
return result;
return Objects.hash(times, state, lastStateChange);
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"times=" + times +
", state='" + state + '\'' +
", lastStateChange=" + lastStateChange +
'}';
return new ToStringBuilder(this)
.append("times", times)
.append("state", state)
.append("lastStateChange", lastStateChange)
.toString();
}
}

View File

@ -111,19 +111,6 @@ public class WorldTimes {
return worldTimes.getOrDefault(world, new GMTimes());
}
@Override
public String toString() {
StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n");
for (Map.Entry<String, GMTimes> entry : worldTimes.entrySet()) {
b.append("World '").append(entry.getKey()).append("':\n");
GMTimes value = entry.getValue();
b.append(" Total: ").append(value.getTotal()).append("\n");
b.append(" ").append(value.toString()).append("\n");
}
b.append("}");
return b.toString();
}
/**
* Used to get the Map for saving.
*
@ -136,4 +123,19 @@ public class WorldTimes {
public void setGMTimesForWorld(String world, GMTimes gmTimes) {
worldTimes.put(world, gmTimes);
}
@Override
public String toString() {
StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n");
for (Map.Entry<String, GMTimes> entry : worldTimes.entrySet()) {
GMTimes value = entry.getValue();
b.append("World '").append(entry.getKey()).append("':\n")
.append(" Total: ").append(value.getTotal()).append("\n")
.append(" ").append(value.toString()).append("\n");
}
b.append("}");
return b.toString();
}
}

View File

@ -4,8 +4,7 @@
*/
package main.java.com.djrapitops.plan.systems.info.server;
import com.google.common.base.Objects;
import java.util.Objects;
import java.util.UUID;
/**
@ -60,13 +59,13 @@ public class ServerInfo {
if (o == null || getClass() != o.getClass()) return false;
ServerInfo that = (ServerInfo) o;
return id == that.id &&
Objects.equal(uuid, that.uuid) &&
Objects.equal(name, that.name) &&
Objects.equal(webAddress, that.webAddress);
Objects.equals(uuid, that.uuid) &&
Objects.equals(name, that.name) &&
Objects.equals(webAddress, that.webAddress);
}
@Override
public int hashCode() {
return Objects.hashCode(id, uuid, name, webAddress);
return Objects.hash(uuid, id, name, webAddress);
}
}

View File

@ -1,5 +1,7 @@
package main.java.com.djrapitops.plan.systems.webserver.response;
import java.util.Objects;
/**
* @author Rsl1122
* @since 3.5.2
@ -43,17 +45,13 @@ public abstract class Response {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response response = (Response) o;
return (header != null ? header.equals(response.header) : response.header == null)
&& (content != null ? content.equals(response.content) : response.content == null);
return Objects.equals(header, response.header) &&
Objects.equals(content, response.content);
}
@Override
public int hashCode() {
int result = header != null ? header.hashCode() : 0;
result = 31 * result + (content != null ? content.hashCode() : 0);
return result;
return Objects.hash(header, content);
}
}

View File

@ -1,6 +1,8 @@
package main.java.com.djrapitops.plan.utilities.analysis;
import com.google.common.base.Objects;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.util.Objects;
/**
* @author Rsl1122
@ -23,11 +25,6 @@ public class Point {
return y;
}
@Override
public String toString() {
return "{x:" + x + " y:" + y + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -39,6 +36,14 @@ public class Point {
@Override
public int hashCode() {
return Objects.hashCode(x, y);
return Objects.hash(x, y);
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("x", x)
.append("y", y)
.toString();
}
}

View File

@ -14,15 +14,6 @@ public class SessionTest {
private Session session;
/**
*
*/
public SessionTest() {
}
/**
*
*/
@Before
public void setUp() {
}

View File

@ -3,28 +3,28 @@ package main.java.com.djrapitops.plan.data;
import main.java.com.djrapitops.plan.Plan;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.TestInit;
/**
* @author Rsl1122
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
@Ignore
public class UserInfoTest {
// TODO Rewrite
private UserInfo session;
private Plan plan;
public UserInfoTest() {
}
@Before
public void setUp() throws Exception {
// TestInit t = TestInit.init();
TestInit.init();
}
@Test

View File

@ -73,7 +73,6 @@ public class ComparatorTest {
assertEquals(longValues, afterSort);
}
@Test
public void testUserDataNameComparator() {
List<UserInfo> userInfo = RandomData.randomUserData();
@ -111,7 +110,8 @@ public class ComparatorTest {
RandomData.randomString(7),
RandomData.randomString(4),
RandomData.randomString(86),
RandomData.randomString(6)));
RandomData.randomString(6))
);
assertEquals(86, strings.get(0).length());
assertEquals(20, strings.get(1).length());