From e8d654f41294d686dc5c5213fd49a7ecf121d3df Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 5 May 2016 23:20:05 +0200 Subject: Factor list concatenation --- src/ch/epfl/xblast/Lists.java | 15 +++++++++++++++ src/ch/epfl/xblast/server/GameStateSerializer.java | 6 ++---- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index b943162..073e0cc 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java @@ -152,4 +152,19 @@ public final class Lists { return new ArrayList<>(l.subList(n, l.size())); } + /** + * Returns an immutable copy of the given lists, concatenated. + * + * @param lists the lists to concatenate + * @param the type of the list's elements + * @return the list result of the concatenation + */ + public static List concatenated(List... lists) { + return Collections.unmodifiableList( + Stream.of(lists) + .map(List::stream) + .reduce(Stream::concat).orElse(Stream.empty()) + .collect(Collectors.toList())); + } + } diff --git a/src/ch/epfl/xblast/server/GameStateSerializer.java b/src/ch/epfl/xblast/server/GameStateSerializer.java index a24ed95..df448a5 100644 --- a/src/ch/epfl/xblast/server/GameStateSerializer.java +++ b/src/ch/epfl/xblast/server/GameStateSerializer.java @@ -10,7 +10,6 @@ import ch.epfl.xblast.server.painter.PlayerPainter; import java.util.*; import java.util.stream.Collectors; -import java.util.stream.Stream; /** * The game state serializer that serializes a GameState into a byte sequence @@ -144,12 +143,11 @@ public final class GameStateSerializer { * @return the serialized GameState */ public static List serialize(BoardPainter bp, GameState gs) { - return Collections.unmodifiableList(Stream.of( + return Lists.concatenated( compress(serializeBoard(bp, gs.board())), compress(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())), serializePlayers(gs.players(), gs.ticks()), - serializeRemainingTime(gs.remainingTime()) - ).flatMap(List::stream).collect(Collectors.toList())); + serializeRemainingTime(gs.remainingTime())); } } -- cgit v1.2.3