From 8dd064838b333b51092c828170e6c24125cbe49b Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 23 Aug 2018 14:52:35 -0500 Subject: [PATCH] Utilize streams when finding most recent waypoint of a given tag --- src/main/java/baritone/chunk/Waypoint.java | 2 +- src/main/java/baritone/chunk/Waypoints.java | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/baritone/chunk/Waypoint.java b/src/main/java/baritone/chunk/Waypoint.java index cb2adbc8..aa800907 100644 --- a/src/main/java/baritone/chunk/Waypoint.java +++ b/src/main/java/baritone/chunk/Waypoint.java @@ -32,7 +32,7 @@ import java.util.Map; public class Waypoint { public final String name; public final Tag tag; - public final long creationTimestamp; + private final long creationTimestamp; public final BlockPos location; public Waypoint(String name, Tag tag, BlockPos location) { diff --git a/src/main/java/baritone/chunk/Waypoints.java b/src/main/java/baritone/chunk/Waypoints.java index 2ad7f16d..fafc102b 100644 --- a/src/main/java/baritone/chunk/Waypoints.java +++ b/src/main/java/baritone/chunk/Waypoints.java @@ -79,11 +79,11 @@ public class Waypoints { try ( FileOutputStream fileOut = new FileOutputStream(fileName.toFile()); BufferedOutputStream bufOut = new BufferedOutputStream(fileOut); - DataOutputStream out = new DataOutputStream(bufOut); + DataOutputStream out = new DataOutputStream(bufOut) ) { for (Waypoint waypoint : waypoints.get(tag)) { out.writeUTF(waypoint.name); - out.writeLong(waypoint.creationTimestamp); + out.writeLong(waypoint.creationTimestamp()); out.writeInt(waypoint.location.getX()); out.writeInt(waypoint.location.getY()); out.writeInt(waypoint.location.getZ()); @@ -98,14 +98,8 @@ public class Waypoints { } public Waypoint getMostRecentByTag(Waypoint.Tag tag) { - Set pts = waypoints.get(tag); - Waypoint best = null; - for (Waypoint waypoint : pts) { - if (best == null || waypoint.creationTimestamp > best.creationTimestamp) { - best = waypoint; - } - } - return best; + // Find a waypoint of the given tag which has the greatest timestamp value, indicating the most recent + return this.waypoints.get(tag).stream().min(Comparator.comparingLong(w -> -w.creationTimestamp())).orElse(null); } public void addWaypoint(Waypoint waypoint) {