aboutsummaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-05 23:20:05 +0200
committerPacien TRAN-GIRARD2016-05-05 23:20:05 +0200
commite8d654f41294d686dc5c5213fd49a7ecf121d3df (patch)
tree65c2bf981c4ffd4d2fe3a10fa0fc64903c928eef /src/ch
parent58a37ddccd5b23b87ee236b44f65337ec0cedcc8 (diff)
downloadxblast-e8d654f41294d686dc5c5213fd49a7ecf121d3df.tar.gz
Factor list concatenation
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/xblast/Lists.java15
-rw-r--r--src/ch/epfl/xblast/server/GameStateSerializer.java6
2 files changed, 17 insertions, 4 deletions
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 {
152 return new ArrayList<>(l.subList(n, l.size())); 152 return new ArrayList<>(l.subList(n, l.size()));
153 } 153 }
154 154
155 /**
156 * Returns an immutable copy of the given lists, concatenated.
157 *
158 * @param lists the lists to concatenate
159 * @param <T> the type of the list's elements
160 * @return the list result of the concatenation
161 */
162 public static <T> List<T> concatenated(List<T>... lists) {
163 return Collections.unmodifiableList(
164 Stream.of(lists)
165 .map(List::stream)
166 .reduce(Stream::concat).orElse(Stream.empty())
167 .collect(Collectors.toList()));
168 }
169
155} 170}
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;
10 10
11import java.util.*; 11import java.util.*;
12import java.util.stream.Collectors; 12import java.util.stream.Collectors;
13import java.util.stream.Stream;
14 13
15/** 14/**
16 * The game state serializer that serializes a GameState into a byte sequence 15 * The game state serializer that serializes a GameState into a byte sequence
@@ -144,12 +143,11 @@ public final class GameStateSerializer {
144 * @return the serialized GameState 143 * @return the serialized GameState
145 */ 144 */
146 public static List<Byte> serialize(BoardPainter bp, GameState gs) { 145 public static List<Byte> serialize(BoardPainter bp, GameState gs) {
147 return Collections.unmodifiableList(Stream.of( 146 return Lists.concatenated(
148 compress(serializeBoard(bp, gs.board())), 147 compress(serializeBoard(bp, gs.board())),
149 compress(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())), 148 compress(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())),
150 serializePlayers(gs.players(), gs.ticks()), 149 serializePlayers(gs.players(), gs.ticks()),
151 serializeRemainingTime(gs.remainingTime()) 150 serializeRemainingTime(gs.remainingTime()));
152 ).flatMap(List::stream).collect(Collectors.toList()));
153 } 151 }
154 152
155} 153}