diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index 46dceef..c718b18 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -63,11 +63,17 @@ public class Team { * * @param worms The worms to add. * - * @post + * @post The team of each worm is set to the right team. Each worm is add to the wormCollection. + * |Collection wormCollection = getAllWormsOfTeam() + * |Arrays.stream(worms).forEach(w -> { + * | w.setTeam(this); + * | wormCollection.add(w); + * |}); * - * @throws IllegalArgumentException Given worm is null + * @throws IllegalArgumentException Given worms are null. * |worms == null - * @throws IllegalArgumentException + * @throws IllegalArgumentException The worms aren't valid worms. + * |! canHaveAsWorm(worms) */ public void addWorm(Worm... worms) throws IllegalArgumentException { if (worms == null) throw new IllegalArgumentException(); @@ -80,23 +86,34 @@ public class Team { } /** - * - * @param worm - * - * @return ... - * |if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam() && !worm.isTerminated()) - * | for (Worm elWorm : worms) - * | if (! elWorm.getName().equals(worm.getName())) - * | result == true - * @throws IllegalArgumentException - * ... - * |worm == null + * Checks the correctness of the given worms. + * + * @param worms The worms to check. + * + * @return Returns true if the given worm is a valid worm. The worm can not be null, the team of the worm can + * not be null, the name of the worm can not already be in the team, the team can not already contains the + * worm. If the size of the collection of all worms of a team is equal to 0 and the worm is not terminated, + * then continue with the next worm. Other conditions for the worm are: the mass can not be lower then the + * minimum mass of the team divided by 2 and can not be higher then 2 times the minimum mass of the team. + * the worm can als not be terminated. + * |HashSet names = new HashSet<>() + * |Collection wormsOfTeam = getAllWormsOfTeam() + * |for (Worm w: worms) + * | if (w == null || w.getTeam() != null || !names.add(w.getName())) + * | result == false + * | if (wormsofTeam.contains(w)) + * | result == false + * | if (wormsOfTeam.size() == 0 && !w.isTerminated()) + * | continue + * | if (w.getMass() < getMinMassTeam() / 2 || w.getMass() > 2 * getMinMassTeam() || w.isTerminated()) + * | result == false + * |result == true */ - private boolean canHaveAsWorm(Worm... worm) { + private boolean canHaveAsWorm(Worm... worms) { HashSet names = new HashSet<>(); - for (Worm w : worm) { + for (Worm w : worms) { if (w == null || w.getTeam() != null || !names.add(w.getName())) return false; if (this.wormCollection.contains(w)) return false; if (this.wormCollection.size() == 0 && !w.isTerminated()) continue; @@ -109,17 +126,18 @@ public class Team { } /** + * Removes given worms from a team. + * + * @param worm The worms who should be removed. * - * @param worm + * @post All given worms are removed from the team and the team of the worm is set to null. + * |for (Worm w: worm) + * | getAllWormsOfTeam().remove(w) + * | w.setTeam(null) * - * @post ... - * |getAllwormsOfTeam().remove(worm) - * @post ... - * |worm.setTeam(null) - * - * @throws IllegalArgumentException - * ... - * |worm == null + * @throws IllegalArgumentException If the given worms are null or a worm is null or a worm is already in the team. + * |worm == null || Arrays.stream(worm).anyMatch(w -> Objects.isNull(w) || + * | !getAllWormsOfTeam().contains(w)) */ public void removeWormsFromTeam(Worm... worm) throws IllegalArgumentException { @@ -132,45 +150,69 @@ public class Team { } /** - * - * @param worm - * - * @return ... + * Checks of the team contains the given worm. + * + * @param worm The worm to check. + * + * @return Returns true if the worm is in the team. * |result == getAllWormsOfTeam().contains(worm) - * - * @throws IllegalArgumentException - * ... - * |worm == null + * + * @throws IllegalArgumentException If the worm is equal to null. + * |worm == null */ public boolean containsWorm(Worm worm) throws IllegalArgumentException { if (worm == null) throw new IllegalArgumentException(); - return this.wormCollection.contains(worm); + return this.wormCollection.contains(worm); } + /** + * Gives all the worms of the team. + * + * @return All the worms of the team in a Collection. + * |result == wormCollection + */ + public Collection getAllWormsOfTeam() { + return wormCollection; + } + + /** + * Gives all the worms of the team. + * + * @return All the worms of the team in a List. + * |result == ArrayList<>(this.wormCollection) + */ public List getAllWormsList() { return new ArrayList<>(this.wormCollection); } /** - * - * @return ... - * |getAllWormsOfTeam().size() + * Gives the number of worms in the team + * + * @return The number of worms in the team. + * |result == getAllWormsOfTeam().size() */ public int getNbWorms() { return this.wormCollection.size(); } /** + * Puts two teams together. + * + * @param receivingTeam The team that gets the worms of another team. + * @param supplyingTeam The team that gives his worms to another team. * - * @param receivingTeam - * @param supplyingTeam + * @post the receivingTeam has his worms and the worms of the supplyingTeam. + * |new.receivingTeam == old.receivingTeam.getAllWormsOfTeam() + supplyingTeam.getAllWormsOfTeam() + * @post the worms of supplyingTeam are removed from that team. + * |Worm[] supWorms = supplyingTeam.getAllWormsOfTeam().toArray(new Worm[0]) + * |supplyingTeam.removeWormsFromTeam(supWorms) * - * @post ... - * |receivingTeam.addWorm(supplyingTeam) - * - * @throws IllegalArgumentException - * ... - * |receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam) + * @throws IllegalArgumentException If the receivingTeam or the supplyingTeam is equal to null or the receivingTeam + * equals the supplyingTeam or a worm of the supplyingTeam is also in the recei- + * vingTeam. + * |receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam) + * | || supplyingTeam.getAllWormsOfTeam().stream().anyMatch(s -> + * |receivingTeam.getAllWormsOfTeam().contains(s)) */ public static void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException { @@ -183,10 +225,7 @@ public class Team { supplyingTeam.removeWormsFromTeam(supWorms); receivingTeam.addWorm(supWorms); } - - /** - * - */ + private Collection wormCollection; //===================================================================================