Merge branch 'master' of gitlab.principis.be:OGP/worms

This commit is contained in:
2018-05-23 17:25:55 +02:00

View File

@@ -63,11 +63,17 @@ public class Team {
* *
* @param worms The worms to add. * @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<Worm> 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 * |worms == null
* @throws IllegalArgumentException * @throws IllegalArgumentException The worms aren't valid worms.
* |! canHaveAsWorm(worms)
*/ */
public void addWorm(Worm... worms) throws IllegalArgumentException { public void addWorm(Worm... worms) throws IllegalArgumentException {
if (worms == null) throw new IllegalArgumentException(); if (worms == null) throw new IllegalArgumentException();
@@ -80,23 +86,34 @@ public class Team {
} }
/** /**
* Checks the correctness of the given worms.
* *
* @param worm * @param worms The worms to check.
* *
* @return ... * @return Returns true if the given worm is a valid worm. The worm can not be null, the team of the worm can
* |if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam() && !worm.isTerminated()) * not be null, the name of the worm can not already be in the team, the team can not already contains the
* | for (Worm elWorm : worms) * worm. If the size of the collection of all worms of a team is equal to 0 and the worm is not terminated,
* | if (! elWorm.getName().equals(worm.getName())) * then continue with the next worm. Other conditions for the worm are: the mass can not be lower then the
* | result == true * minimum mass of the team divided by 2 and can not be higher then 2 times the minimum mass of the team.
* @throws IllegalArgumentException * the worm can als not be terminated.
* ... * |HashSet<String> names = new HashSet<>()
* |worm == null * |Collection<Worm> 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<String> names = new HashSet<>(); HashSet<String> names = new HashSet<>();
for (Worm w : worm) { for (Worm w : worms) {
if (w == null || w.getTeam() != null || !names.add(w.getName())) return false; if (w == null || w.getTeam() != null || !names.add(w.getName())) return false;
if (this.wormCollection.contains(w)) return false; if (this.wormCollection.contains(w)) return false;
if (this.wormCollection.size() == 0 && !w.isTerminated()) continue; if (this.wormCollection.size() == 0 && !w.isTerminated()) continue;
@@ -109,17 +126,18 @@ public class Team {
} }
/** /**
* Removes given worms from a team.
* *
* @param worm * @param worm The worms who should be removed.
* *
* @post ... * @post All given worms are removed from the team and the team of the worm is set to null.
* |getAllwormsOfTeam().remove(worm) * |for (Worm w: worm)
* @post ... * | getAllWormsOfTeam().remove(w)
* |worm.setTeam(null) * | w.setTeam(null)
* *
* @throws IllegalArgumentException * @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) ||
* |worm == null * | !getAllWormsOfTeam().contains(w))
*/ */
public void removeWormsFromTeam(Worm... worm) throws IllegalArgumentException { public void removeWormsFromTeam(Worm... worm) throws IllegalArgumentException {
@@ -132,14 +150,14 @@ public class Team {
} }
/** /**
* Checks of the team contains the given worm.
* *
* @param worm * @param worm The worm to check.
* *
* @return ... * @return Returns true if the worm is in the team.
* |result == getAllWormsOfTeam().contains(worm) * |result == getAllWormsOfTeam().contains(worm)
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException If the worm is equal to null.
* ...
* |worm == null * |worm == null
*/ */
public boolean containsWorm(Worm worm) throws IllegalArgumentException { public boolean containsWorm(Worm worm) throws IllegalArgumentException {
@@ -147,30 +165,54 @@ public class Team {
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<Worm> 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<Worm> getAllWormsList() { public List<Worm> getAllWormsList() {
return new ArrayList<>(this.wormCollection); return new ArrayList<>(this.wormCollection);
} }
/** /**
* Gives the number of worms in the team
* *
* @return ... * @return The number of worms in the team.
* |getAllWormsOfTeam().size() * |result == getAllWormsOfTeam().size()
*/ */
public int getNbWorms() { public int getNbWorms() {
return this.wormCollection.size(); return this.wormCollection.size();
} }
/** /**
* Puts two teams together.
* *
* @param receivingTeam * @param receivingTeam The team that gets the worms of another team.
* @param supplyingTeam * @param supplyingTeam The team that gives his worms to another team.
* *
* @post ... * @post the receivingTeam has his worms and the worms of the supplyingTeam.
* |receivingTeam.addWorm(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)
* *
* @throws IllegalArgumentException * @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) * |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 { public static void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException {
@@ -184,9 +226,6 @@ public class Team {
receivingTeam.addWorm(supWorms); receivingTeam.addWorm(supWorms);
} }
/**
*
*/
private Collection<Worm> wormCollection; private Collection<Worm> wormCollection;
//=================================================================================== //===================================================================================