♻️ Make the move set an actual Set

pull/4/head
Alexis Drai 2 years ago
parent e9c468fdc8
commit 70873539c7

@ -18,6 +18,7 @@ import org.bson.types.ObjectId;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class PokemongCodec extends GenericCodec<Pokemong> { public class PokemongCodec extends GenericCodec<Pokemong> {
@ -133,15 +134,15 @@ public class PokemongCodec extends GenericCodec<Pokemong> {
.collect(Collectors.toList()); .collect(Collectors.toList());
pokemong.setTypes(types); pokemong.setTypes(types);
List<PokemongMove> moveSet = document.getList("moveSet", Document.class) Set<PokemongMove> moveSet = document.getList("moveSet", Document.class)
.stream() .stream()
.map(pokemongMoveDoc -> { .map(pokemongMoveDoc -> {
PokemongMove move = new PokemongMove(); PokemongMove move = new PokemongMove();
move.setId(((ObjectId) pokemongMoveDoc.get("_id")).toString()); move.setId(((ObjectId) pokemongMoveDoc.get("_id")).toString());
move.setName(pokemongMoveDoc.getString("name")); move.setName(pokemongMoveDoc.getString("name"));
return move; return move;
}) })
.collect(Collectors.toList()); .collect(Collectors.toSet());
pokemong.setMoveSet(moveSet); pokemong.setMoveSet(moveSet);
return pokemong; return pokemong;

@ -7,6 +7,7 @@ import org.bson.types.ObjectId;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
public class Pokemong extends GenericEntity { public class Pokemong extends GenericEntity {
public static final String COLLECTION_NAME = "pokemongs"; public static final String COLLECTION_NAME = "pokemongs";
@ -25,7 +26,7 @@ public class Pokemong extends GenericEntity {
/** /**
* pokemong.moveSet: [{_id: ObjectId, name: String}] * pokemong.moveSet: [{_id: ObjectId, name: String}]
*/ */
private List<PokemongMove> moveSet; private Set<PokemongMove> moveSet;
public Pokemong() {} public Pokemong() {}
@ -95,11 +96,11 @@ public class Pokemong extends GenericEntity {
this.types = types; this.types = types;
} }
public List<PokemongMove> getMoveSet() { public Set<PokemongMove> getMoveSet() {
return Collections.unmodifiableList(moveSet); return Collections.unmodifiableSet(moveSet);
} }
public void setMoveSet(List<PokemongMove> moveSet) { public void setMoveSet(Set<PokemongMove> moveSet) {
this.moveSet = moveSet; this.moveSet = moveSet;
} }

@ -13,6 +13,7 @@ import jakarta.inject.Inject;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.Set;
@ApplicationScoped @ApplicationScoped
public class PokemongService extends GenericService<Pokemong> { public class PokemongService extends GenericService<Pokemong> {
@ -83,7 +84,7 @@ public class PokemongService extends GenericService<Pokemong> {
throw new NonValidEntityException("pokemong types was null or empty or had more than 2 types"); throw new NonValidEntityException("pokemong types was null or empty or had more than 2 types");
} }
List<PokemongMove> moveSet = pokemong.getMoveSet(); Set<PokemongMove> moveSet = pokemong.getMoveSet();
if (moveSet == null if (moveSet == null
|| moveSet.size() == 0 || moveSet.size() == 0
|| moveSet.size() > 4) || moveSet.size() > 4)

Loading…
Cancel
Save