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.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
public class Trace {
@ -202,13 +203,14 @@ public class Trace {
return;
}
List<TNTPoint> workingRecords = new ArrayList<>(records);
Set<ViewFlag> flagList = playerTraceShowData.getEffectiveViewFlags();
// Apply filters
Stream<TNTPoint> workingRecordsStream = records.stream();
for (ViewFlag flag : flagList) {
workingRecords = flag.filter(workingRecords);
workingRecordsStream = flag.filter(workingRecordsStream);
}
List<TNTPoint> workingRecords = workingRecordsStream.collect(Collectors.toList());
// Bundle records at unique positions
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.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 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") {
@Override
public List<TNTPoint> filter(List<TNTPoint> records) {
return records.stream()
.filter(TNTPoint::isExplosion)
.collect(Collectors.toList());
public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.filter(TNTPoint::isExplosion);
}
};
public static ViewFlag IGNITE = new ViewFlag(true, true, "ignite", "i") {
@Override
public List<TNTPoint> filter(List<TNTPoint> records) {
return records.stream()
.filter(record -> record.isAfterFirstExplosion())
.collect(Collectors.toList());
public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.filter(record -> record.isAfterFirstExplosion());
}
};
public static ViewFlag SOURCE = new ViewFlag(true, false, IGNITE, "source", "s") {
@Override
public List<TNTPoint> filter(List<TNTPoint> records) {
return records.stream()
.filter(record -> record.getFuse() == 80)
.collect(Collectors.toList());
public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.filter(record -> record.getFuse() == 80);
}
};
public static ViewFlag BUILD_DESTROY_ONLY = new ViewFlag(true, false, "build-destroy-only") {
@Override
public List<TNTPoint> filter(List<TNTPoint> records) {
return records.stream()
.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedBuildArea())
.collect(Collectors.toList());
public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedBuildArea());
}
};
public static ViewFlag TESTBLOCK_DESTROY_ONLY = new ViewFlag(true, false, "testblock-destroy-only") {
@Override
public List<TNTPoint> filter(List<TNTPoint> records) {
return records.stream()
.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedTestBlock())
.collect(Collectors.toList());
public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.filter(record -> record.getHistory().get(record.getHistory().size() - 1).isDestroyedTestBlock());
}
};
public static ViewFlag MICROMOTION = new ViewFlag(true, false, "micromotion", "m") {
@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<TNTPoint> toRemove = new HashSet<>();
@ -119,7 +112,7 @@ public abstract class ViewFlag {
records.removeAll(record.getHistory());
}
return records;
return records.stream();
}
};
@ -239,7 +232,7 @@ public abstract class ViewFlag {
* @param records Records to be filtered
* @return Filtered records
*/
public List<TNTPoint> filter(List<TNTPoint> records) {
public Stream<TNTPoint> filter(Stream<TNTPoint> 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.rendering.ViewFlag;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A view flag for rendering a trace only in a given time intervall
@ -56,9 +55,7 @@ public class AtFlag extends ViewFlag {
}
@Override
public List<TNTPoint> filter(List<TNTPoint> records) {
return records.stream()
.filter(record -> record.getTicksSinceStart() >= start && record.getTicksSinceStart() <= end)
.collect(Collectors.toList());
public Stream<TNTPoint> filter(Stream<TNTPoint> records) {
return records.filter(record -> record.getTicksSinceStart() >= start && record.getTicksSinceStart() <= end);
}
}

Datei anzeigen

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