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.
*
* @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
* @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<String> names = new HashSet<>()
* |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<>();
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 {
}
/**
* Checks of the team contains the given worm.
*
* @param worm The worm to check.
*
* @param worm
*
* @return ...
* @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<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() {
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 {
@@ -184,9 +226,6 @@ public class Team {
receivingTeam.addWorm(supWorms);
}
/**
*
*/
private Collection<Worm> wormCollection;
//===================================================================================