SteamWar/BauSystem2.0
Archiviert
12
0

Rewrite ViewFlag.filter with Stream API to reduce copies of lists
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2024-04-21 15:47:18 +02:00
Ursprung b64e06479f
Commit e931d7157e
4 geänderte Dateien mit 26 neuen und 37 gelöschten Zeilen

Datei anzeigen

@ -35,6 +35,7 @@ import java.io.*;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
public class Trace { public class Trace {
@ -202,13 +203,14 @@ public class Trace {
return; return;
} }
List<TNTPoint> workingRecords = new ArrayList<>(records);
Set<ViewFlag> flagList = playerTraceShowData.getEffectiveViewFlags(); Set<ViewFlag> flagList = playerTraceShowData.getEffectiveViewFlags();
// Apply filters // Apply filters
Stream<TNTPoint> workingRecordsStream = records.stream();
for (ViewFlag flag : flagList) { for (ViewFlag flag : flagList) {
workingRecords = flag.filter(workingRecords); workingRecordsStream = flag.filter(workingRecordsStream);
} }
List<TNTPoint> workingRecords = workingRecordsStream.collect(Collectors.toList());
// Bundle records at unique positions // Bundle records at unique positions
List<List<TNTPoint>> bundles = bundleRecords(workingRecords, playerTraceShowData.getBundleFilter()); List<List<TNTPoint>> bundles = bundleRecords(workingRecords, playerTraceShowData.getBundleFilter());

Datei anzeigen

@ -31,6 +31,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* A settable flag that changes how a trace is rendered * A settable flag that changes how a trace is rendered
@ -48,52 +49,44 @@ public abstract class ViewFlag {
public static ViewFlag EXPLOSION = new ViewFlag(true, false, "explosion", "e") { public static ViewFlag EXPLOSION = new ViewFlag(true, false, "explosion", "e") {
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.stream() return records.filter(TNTPoint::isExplosion);
.filter(TNTPoint::isExplosion)
.collect(Collectors.toList());
} }
}; };
public static ViewFlag IGNITE = new ViewFlag(true, true, "ignite", "i") { public static ViewFlag IGNITE = new ViewFlag(true, true, "ignite", "i") {
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.stream() return records.filter(record -> record.isAfterFirstExplosion());
.filter(record -> record.isAfterFirstExplosion())
.collect(Collectors.toList());
} }
}; };
public static ViewFlag SOURCE = new ViewFlag(true, false, IGNITE, "source", "s") { public static ViewFlag SOURCE = new ViewFlag(true, false, IGNITE, "source", "s") {
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.stream() return records.filter(record -> record.getFuse() == 80);
.filter(record -> record.getFuse() == 80)
.collect(Collectors.toList());
} }
}; };
public static ViewFlag BUILD_DESTROY_ONLY = new ViewFlag(true, false, "build-destroy-only") { public static ViewFlag BUILD_DESTROY_ONLY = new ViewFlag(true, false, "build-destroy-only") {
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.stream() return records.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedBuildArea());
.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedBuildArea())
.collect(Collectors.toList());
} }
}; };
public static ViewFlag TESTBLOCK_DESTROY_ONLY = new ViewFlag(true, false, "testblock-destroy-only") { public static ViewFlag TESTBLOCK_DESTROY_ONLY = new ViewFlag(true, false, "testblock-destroy-only") {
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.stream() return records.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedTestBlock());
.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedTestBlock())
.collect(Collectors.toList());
} }
}; };
public static ViewFlag MICROMOTION = new ViewFlag(true, false, "micromotion", "m") { public static ViewFlag MICROMOTION = new ViewFlag(true, false, "micromotion", "m") {
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> stream) {
List<TNTPoint> records = stream.collect(Collectors.toList());
;
Set<Integer> seen = new HashSet<>(); Set<Integer> seen = new HashSet<>();
Set<TNTPoint> toRemove = new HashSet<>(); Set<TNTPoint> toRemove = new HashSet<>();
@ -119,7 +112,7 @@ public abstract class ViewFlag {
records.removeAll(record.getHistory()); records.removeAll(record.getHistory());
} }
return records; return records.stream();
} }
}; };
@ -239,7 +232,7 @@ public abstract class ViewFlag {
* @param records Records to be filtered * @param records Records to be filtered
* @return Filtered records * @return Filtered records
*/ */
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records; return records;
} }

Datei anzeigen

@ -22,8 +22,7 @@ package de.steamwar.bausystem.features.tracer.rendering.dynamicflags;
import de.steamwar.bausystem.features.tracer.TNTPoint; import de.steamwar.bausystem.features.tracer.TNTPoint;
import de.steamwar.bausystem.features.tracer.rendering.ViewFlag; import de.steamwar.bausystem.features.tracer.rendering.ViewFlag;
import java.util.List; import java.util.stream.Stream;
import java.util.stream.Collectors;
/** /**
* A view flag for rendering a trace only in a given time intervall * A view flag for rendering a trace only in a given time intervall
@ -56,9 +55,7 @@ public class AtFlag extends ViewFlag {
} }
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.stream() return records.filter(record -> record.getTicksSinceStart() >= start && record.getTicksSinceStart() <= end);
.filter(record -> record.getTicksSinceStart() >= start && record.getTicksSinceStart() <= end)
.collect(Collectors.toList());
} }
} }

Datei anzeigen

@ -23,9 +23,8 @@ import de.steamwar.bausystem.features.tracer.TNTPoint;
import de.steamwar.bausystem.features.tracer.rendering.ViewFlag; import de.steamwar.bausystem.features.tracer.rendering.ViewFlag;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Stream;
/** /**
* A flag for rendering only the records of specific tnts * A flag for rendering only the records of specific tnts
@ -53,10 +52,8 @@ public class IsolateFlag extends ViewFlag {
} }
@Override @Override
public List<TNTPoint> filter(List<TNTPoint> records) { public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
if (tntToIsolate.isEmpty()) return records; if (tntToIsolate.isEmpty()) return records;
return records.stream() return records.filter(record -> tntToIsolate.contains(record.getTntId()));
.filter(record -> tntToIsolate.contains(record.getTntId()))
.collect(Collectors.toList());
} }
} }