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

Class Class, % Method, % Branch, % Line, %
Tower 100% (1/1) 100% (3/3) 100% (2/2) 80% (8/10)


1 package it.polimi.ingsw.Model; 2  3 import it.polimi.ingsw.Exceptions.Input.DuplicateElementException; 4 import it.polimi.ingsw.Exceptions.Input.InvalidElementException; 5 import it.polimi.ingsw.Logger; 6 import it.polimi.ingsw.Model.Enums.TowerColour; 7  8 import java.io.Serial; 9 import java.io.Serializable; 10  11 /** 12  * Allows for the representation of the game's tower pawn 13  */ 14 public class Tower implements Serializable { 15  @Serial 16  private static final long serialVersionUID = 131L; // convention: 1 for model, (01 -> 99) for objects 17  private final TowerColour colour; 18  private final TowerStorage storage; 19  20  /** 21  * Creates a Tower by assigning it a colour and a storage. A tower may find itself inside the storage or outside of it, 22  * in which case it has the ability to return to its storage on its own. 23  * 24  * @param colour the {@link TowerColour} assigned to the tower 25  * @param storage the {@link TowerStorage} object responsible for storing towers during the game 26  */ 27  public Tower(TowerColour colour, TowerStorage storage) { 28  if (!colour.equals(storage.getColour())) { 29  throw new IllegalArgumentException("Tower's colour and TowerStorage's colour are different"); 30  } 31  this.colour = colour; 32  this.storage = storage; 33  } 34  35  /** 36  * Get the colour of the tower 37  * 38  * @return the {@link TowerColour} of the tower 39  */ 40  public TowerColour getColour() { 41  return colour; 42  } 43  44  /** 45  * Send the tower back to its {@link TowerStorage}. If the tower is already back in storage, then nothing is done. 46  * 47  * @throws InvalidElementException if {@link #getColour()} is different than {@link TowerStorage#getColour()} 48  */ 49  public void linkBackToStorage() throws InvalidElementException { 50  try { 51  this.storage.pushTower(this); 52  } catch (DuplicateElementException e) { 53  Logger.warning("a tower that was already in storage tried to get back into it"); 54  } 55  } 56 }