Utilize streams when finding most recent waypoint of a given tag

This commit is contained in:
Brady 2018-08-23 14:52:35 -05:00
parent 36cbffb593
commit 8dd064838b
No known key found for this signature in database
GPG Key ID: 73A788379A197567
2 changed files with 5 additions and 11 deletions

View File

@ -32,7 +32,7 @@ import java.util.Map;
public class Waypoint { public class Waypoint {
public final String name; public final String name;
public final Tag tag; public final Tag tag;
public final long creationTimestamp; private final long creationTimestamp;
public final BlockPos location; public final BlockPos location;
public Waypoint(String name, Tag tag, BlockPos location) { public Waypoint(String name, Tag tag, BlockPos location) {

View File

@ -79,11 +79,11 @@ public class Waypoints {
try ( try (
FileOutputStream fileOut = new FileOutputStream(fileName.toFile()); FileOutputStream fileOut = new FileOutputStream(fileName.toFile());
BufferedOutputStream bufOut = new BufferedOutputStream(fileOut); BufferedOutputStream bufOut = new BufferedOutputStream(fileOut);
DataOutputStream out = new DataOutputStream(bufOut); DataOutputStream out = new DataOutputStream(bufOut)
) { ) {
for (Waypoint waypoint : waypoints.get(tag)) { for (Waypoint waypoint : waypoints.get(tag)) {
out.writeUTF(waypoint.name); out.writeUTF(waypoint.name);
out.writeLong(waypoint.creationTimestamp); out.writeLong(waypoint.creationTimestamp());
out.writeInt(waypoint.location.getX()); out.writeInt(waypoint.location.getX());
out.writeInt(waypoint.location.getY()); out.writeInt(waypoint.location.getY());
out.writeInt(waypoint.location.getZ()); out.writeInt(waypoint.location.getZ());
@ -98,14 +98,8 @@ public class Waypoints {
} }
public Waypoint getMostRecentByTag(Waypoint.Tag tag) { public Waypoint getMostRecentByTag(Waypoint.Tag tag) {
Set<Waypoint> pts = waypoints.get(tag); // Find a waypoint of the given tag which has the greatest timestamp value, indicating the most recent
Waypoint best = null; return this.waypoints.get(tag).stream().min(Comparator.comparingLong(w -> -w.creationTimestamp())).orElse(null);
for (Waypoint waypoint : pts) {
if (best == null || waypoint.creationTimestamp > best.creationTimestamp) {
best = waypoint;
}
}
return best;
} }
public void addWaypoint(Waypoint waypoint) { public void addWaypoint(Waypoint waypoint) {