Coverage Summary for Class: StudentBag (it.polimi.ingsw.Model)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
StudentBag |
100%
(1/1)
|
100%
(7/7)
|
85,7%
(12/14)
|
91,3%
(21/23)
|
1 package it.polimi.ingsw.Model;
2
3 import it.polimi.ingsw.Exceptions.Container.EmptyContainerException;
4 import it.polimi.ingsw.Misc.Utils;
5 import it.polimi.ingsw.Model.Enums.PawnColour;
6
7 import java.io.Serial;
8 import java.io.Serializable;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 /**
13 * The StudentBag contains a limited number of {@link PawnColour} ordered randomly.
14 */
15 public class StudentBag implements Serializable {
16 @Serial
17 private static final long serialVersionUID = 130L; // convention: 1 for model, (01 -> 99) for objects
18 private ArrayList<PawnColour> studentBag;
19 private boolean isEmpty;
20
21 /**
22 * Construct the bag
23 *
24 * @param numOfStudentsPerColour the number of each color of students to put in the bag
25 */
26 public StudentBag(int numOfStudentsPerColour) {
27 this.studentBag = new ArrayList<>(numOfStudentsPerColour * PawnColour.values().length);
28 for (PawnColour colour :
29 PawnColour.values()) {
30 for (int i = 0; i < numOfStudentsPerColour; i++) {
31 studentBag.add(colour);
32 }
33 }
34 Utils.shuffle(this.studentBag);
35 this.isEmpty = false;
36 }
37
38 /**
39 * Check to see if the bag is empty
40 *
41 * @return true if the bag is empty
42 */
43 public boolean isEmpty() {
44 return this.isEmpty;
45 }
46
47 /**
48 * Extract multiple {@link PawnColour}s at once
49 *
50 * @param extractions number of maximum extractions to carry out. The number of extracted students may be lower than the
51 * number specified as input, if the bag empties out during the extraction
52 * @return an Unmodifiable {@link List} containing the extracted {@link PawnColour}
53 */
54 public List<PawnColour> multipleExtraction(int extractions) {
55 List<PawnColour> extracted = new ArrayList<>();
56 for (int i = 0; i < extractions && !this.isEmpty; i++) {
57 try {
58 extracted.add(this.extract());
59 } catch (EmptyContainerException e) {
60 // this catch clause should never be executed
61 throw new RuntimeException(e);
62 }
63 }
64 return List.copyOf(extracted);
65 }
66
67 /**
68 * Extract a single {@link PawnColour} from the bag
69 *
70 * @return the extracted student
71 * @throws EmptyContainerException if the bag is empty
72 */
73 public PawnColour extract() throws EmptyContainerException {
74 if (this.isEmpty) throw new EmptyContainerException("StudentBag");
75 if (this.getSize() == 1) this.isEmpty = true;
76 return this.studentBag.remove(this.studentBag.size() - 1);
77 }
78
79 /**
80 * Check the remaining students in the bag
81 *
82 * @return the size of the bag
83 */
84 public int getSize() {
85 return studentBag.size();
86 }
87
88 /**
89 * Put a student back in the bag and shuffle it in.
90 *
91 * @param colour the {@link PawnColour} to add back to the bag
92 */
93 public void appendAndShuffle(PawnColour colour) {
94 if (this.getSize() == 0) this.isEmpty = false;
95 this.studentBag.add(colour);
96 Utils.shuffle(this.studentBag);
97 }
98
99 /**
100 * Used to sanitize {@link Model}. Removes information about the internal contents of the bag, making the spoofing of
101 * technically hidden information impossible on the client side.
102 */
103 public void removeContentReference() {
104 this.studentBag = null;
105 }
106 }