From b6dcafb18bdfccb5f86519faf9a946b1f9b7c1aa Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 21 Aug 2017 19:11:28 +0300 Subject: [PATCH] Removed need for WorldTimes Optionals. --- .../djrapitops/plan/data/time/WorldTimes.java | 41 ++++++++++++----- .../plan/data/time/WorldTimesTest.java | 46 ++++++++----------- 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java b/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java index ff9dc61a8..63e028ee1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java @@ -4,10 +4,9 @@ import main.java.com.djrapitops.plan.utilities.MiscUtils; import java.util.HashMap; import java.util.Map; -import java.util.Optional; /** - * TimeKeeper class that tracks the time spent in each World based on Playtime. + * Class that tracks the time spent in each World based on GMTimes. * * @author Rsl1122 * @since 3.6.0 @@ -59,20 +58,42 @@ public class WorldTimes { currentGamemode = gameMode; } - public Optional getWorldPlaytime(String world) { + /** + * Used to get a total playtime of a world. + * + * @param world World name being checked. + * @return total milliseconds spent in a world. + */ + public long getWorldPlaytime(String world) { GMTimes gmTimes = worldTimes.get(world); if (gmTimes != null) { - return Optional.of(gmTimes.getTotal()); + return gmTimes.getTotal(); } - return Optional.empty(); + return 0; } - public Optional getGMTimes(String world) { + public long getTotal() { + return worldTimes.values().stream() + .mapToLong(GMTimes::getTotal) + .sum(); + } + + /** + * Used for Quick access to time of each GameMode. + *

+ * Should not be used for changing state, + * because if player has not played in the world, + * an empty GMTimes is given, with 0 as playtime + * + * @param world World name being checked. + * @return GMTimes object with play times of each GameMode. + */ + public GMTimes getGMTimes(String world) { GMTimes gmTimes = worldTimes.get(world); if (gmTimes != null) { - return Optional.of(gmTimes); + return gmTimes; } - return Optional.empty(); + return new GMTimes(); } @Override @@ -91,8 +112,4 @@ public class WorldTimes { public String getCurrentWorld() { return currentWorld; } - - public String getCurrentGamemode() { - return currentGamemode; - } } diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java index d3a7667bd..a4a91f572 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java @@ -6,7 +6,10 @@ import org.junit.Before; import org.junit.Test; import test.java.utils.RandomData; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import static org.junit.Assert.assertEquals; @@ -25,10 +28,7 @@ public class WorldTimesTest { @Before public void setUp() throws Exception { test = new WorldTimes(worldOne, gms[0]); - Optional gmTimes = test.getGMTimes(worldOne); - gmTimes.ifPresent(gmTimes1 -> - time = gmTimes1.getLastStateChange() - ); + time = test.getGMTimes(worldOne).getLastStateChange(); System.out.println(test); } @@ -37,8 +37,8 @@ public class WorldTimesTest { long changeTime = time + 1000L; test.updateState(worldTwo, gms[0], changeTime); System.out.println(test); - assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); - assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); + assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); } @Test @@ -46,8 +46,8 @@ public class WorldTimesTest { long changeTime = time + 1000L; test.updateState(worldOne, gms[0], changeTime); System.out.println(test); - assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); - assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); + assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); } @Test @@ -56,13 +56,13 @@ public class WorldTimesTest { long changeTime2 = changeTime + 1000L; test.updateState(worldTwo, gms[2], changeTime); System.out.println(test); - assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); - assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); + assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); test.updateState(worldOne, gms[1], changeTime2); System.out.println(test); - assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); - assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); - assertEquals(1000L, test.getGMTimes(worldTwo).get().getTime(gms[2])); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); + assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); + assertEquals(1000L, test.getGMTimes(worldTwo).getTime(gms[2])); } @Test @@ -96,16 +96,8 @@ public class WorldTimesTest { long worldTimeOne = worldOneCount * amount; long worldTimeTwo = worldTwoCount * amount; - long time1 = 0L; - long time2 = 0L; - Optional worldPlaytime = test.getWorldPlaytime(worldOne); - if (worldPlaytime.isPresent()) { - time1 += worldPlaytime.get(); - } - Optional worldPlaytime2 = test.getWorldPlaytime(worldTwo); - if (worldPlaytime2.isPresent()) { - time2 += worldPlaytime2.get(); - } + long time1 = test.getWorldPlaytime(worldOne); + long time2 = test.getWorldPlaytime(worldTwo); System.out.println(test); // Tests World time calculation. @@ -118,7 +110,7 @@ public class WorldTimesTest { public void testGMTrackingSingleWorld() { long changeTime = time + 1000L; long changeTime2 = changeTime + 1000L; - GMTimes gmTimes = test.getGMTimes(worldOne).get(); + GMTimes gmTimes = test.getGMTimes(worldOne); test.updateState(worldOne, "CREATIVE", changeTime); assertEquals(1000L, gmTimes.getTime("SURVIVAL")); assertEquals(0L, gmTimes.getTime("CREATIVE")); @@ -132,7 +124,7 @@ public class WorldTimesTest { public void testGMTrackingTwoWorlds() { long changeTime = time + 1000L; long changeTime2 = time + 2000L; - GMTimes worldOneGMTimes = test.getGMTimes(worldOne).get(); + GMTimes worldOneGMTimes = test.getGMTimes(worldOne); test.updateState(worldOne, "CREATIVE", changeTime); test.updateState(worldOne, "ADVENTURE", changeTime2); assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); @@ -140,7 +132,7 @@ public class WorldTimesTest { assertEquals(0L, worldOneGMTimes.getTime("ADVENTURE")); test.updateState(worldTwo, "SURVIVAL", time + 3000L); - GMTimes worldTwoGMTimes = test.getGMTimes(worldTwo).get(); + GMTimes worldTwoGMTimes = test.getGMTimes(worldTwo); assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE")); assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE"));