Coverage Summary for Class: Island (it.polimi.ingsw.Model)
| Class | Class, % | Method, % | Branch, % | Line, % |
|---|---|---|---|---|
| Island | 100% (1/1) | 100% (6/6) | 75% (3/4) | 68,8% (11/16) |
1 package it.polimi.ingsw.Model; 2 3 import it.polimi.ingsw.Exceptions.Input.InvalidElementException; 4 import it.polimi.ingsw.Logger; 5 import it.polimi.ingsw.Misc.OptionalValue; 6 import it.polimi.ingsw.Model.Enums.PawnColour; 7 import it.polimi.ingsw.Model.Enums.TowerColour; 8 9 import java.io.Serial; 10 import java.io.Serializable; 11 import java.util.ArrayList; 12 import java.util.List; 13 14 /** 15 * Islands are containers of {@link PawnColour}s and {@link Tower}.The islands can be grouped through {@link IslandGroup} 16 */ 17 public class Island implements Serializable { 18 @Serial 19 private static final long serialVersionUID = 121L; // convention: 1 for model, (01 -> 99) for objects 20 21 private final int id; 22 private final ArrayList<PawnColour> students; 23 private Tower tower; 24 25 /** 26 * Construct an Island, assinging an ID to it. 27 * 28 * @param id the id of the constructed Island. 29 */ 30 public Island(int id) { 31 this.id = id; 32 this.students = new ArrayList<>(); 33 this.tower = null; 34 } 35 36 /** 37 * @return the ID of the Island 38 */ 39 public int getId() { 40 return id; 41 } 42 43 /** 44 * @return the {@link PawnColour}s contained in the Island 45 */ 46 public List<PawnColour> getStudents() { 47 return new ArrayList<>(students); 48 } 49 50 /** 51 * If a tower is present, return a non-empty {@link OptionalValue} 52 * 53 * @return the colour of the contained tower wrapped in a {@link OptionalValue} 54 */ 55 public OptionalValue<TowerColour> getTowerColour() { 56 if (this.tower == null) return OptionalValue.empty(); 57 else return OptionalValue.of(this.tower.getColour()); 58 } 59 60 /** 61 * a student can be added to the Island through this method 62 * 63 * @param p the {@link PawnColour} to add to the island 64 */ 65 public void addStudent(PawnColour p) { 66 students.add(p); 67 } 68 69 /** 70 * a {@link Tower} may need to be swapped or added during the Island's lifespan, this method can be used for that 71 * 72 * @param t the new {@link Tower} to be put on the island. the old tower (if any was present) will be returned to its 73 * rightful storage automatically. The input can be null, this removes the tower from the island 74 */ 75 public void swapTower(Tower t) { 76 if (this.tower != null) { 77 try { 78 this.tower.linkBackToStorage(); 79 } catch (InvalidElementException e) { 80 Logger.severe("Tower could not be pushed back to its original Storage. Critical, unrecoverable error"); 81 throw new RuntimeException(e); 82 } 83 } 84 this.tower = t; 85 } 86 87 }