Coverage Summary for Class: TowerStorage (it.polimi.ingsw.Model)

Class Class, % Method, % Branch, % Line, %
TowerStorage 100% (1/1) 100% (5/5) 85,7% (12/14) 100% (17/17)


1 package it.polimi.ingsw.Model; 2  3  4 import it.polimi.ingsw.Exceptions.Input.DuplicateElementException; 5 import it.polimi.ingsw.Exceptions.Input.InvalidElementException; 6 import it.polimi.ingsw.Model.Enums.TowerColour; 7  8 import java.io.Serial; 9 import java.io.Serializable; 10 import java.util.Stack; 11  12 /** 13  * A container for a set of {@link Tower}s of the same {@link TowerColour} 14  */ 15 public class TowerStorage implements Serializable { 16  @Serial 17  private static final long serialVersionUID = 133L; // convention: 1 for model, (01 -> 99) for objects 18  19  private final TowerColour colour; 20  private final Stack<Tower> storage; 21  22  /** 23  * Creates the storage and fills it up with towers 24  * 25  * @param colour the colour of the towers in storage 26  * @param amount how many towers will be added to the storage 27  */ 28  public TowerStorage(TowerColour colour, int amount) { 29  this.colour = colour; 30  this.storage = new Stack<>(); 31  for (int i = 0; i < amount; i++) { 32  this.storage.push(new Tower(colour, this)); 33  } 34  } 35  36  /** 37  * Get the colour of the stored {@link Tower}s 38  * 39  * @return the {@link TowerColour} this storage handles 40  */ 41  public TowerColour getColour() { 42  return colour; 43  } 44  45  /** 46  * Extract a tower from storage. 47  * 48  * @return the extracted {@link Tower} or null if the storage is empty 49  */ 50  public Tower extractTower() { 51  if (getTowerCount() == 0) return null; 52  return this.storage.pop(); 53  } 54  55  /** 56  * Get the amount of towers left in storage 57  * 58  * @return the amount of {@link Tower}s left in storage 59  */ 60  public int getTowerCount() { 61  return this.storage.size(); 62  } 63  64  /** 65  * Put a {@link Tower} into storage 66  * 67  * @param t the Tower to add into storage 68  * @throws DuplicateElementException if the same {@link Tower} was found already present in storage 69  * @throws InvalidElementException {@link Tower} if the {@link TowerColour} of the Tower is not the same as the {@link TowerColour} of the 70  * storage 71  */ 72  public void pushTower(Tower t) throws DuplicateElementException, InvalidElementException { 73  boolean checkIfPresent = storage.stream() 74  .anyMatch(i -> t == i); 75  if (!checkIfPresent && t.getColour() == this.colour) { 76  this.storage.push(t); 77  } else { 78  if (checkIfPresent) { 79  throw new DuplicateElementException("Tower"); 80  } 81  if (t.getColour() != this.colour) { 82  throw new InvalidElementException("Tower"); 83  } 84  } 85  } 86 }