Skip to content

Commit 0e24c69

Browse files
committed
Added clear() method
- Added test for clear() behaviour - Version bump to 0.7
1 parent 9eb184d commit 0e24c69

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.lewdev</groupId>
77
<artifactId>probability-lib</artifactId>
8-
<version>0.6</version>
8+
<version>0.7</version>
99
<packaging>jar</packaging>
1010

1111
<name>probability-lib</name>

src/main/java/com/lewdev/probabilitylib/ProbabilityCollection.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* </ul>
4646
*
4747
* @author Lewys Davies
48-
* @version 0.6
48+
* @version 0.7
4949
*
5050
* @param <E> Type of elements
5151
*/
@@ -152,6 +152,14 @@ public boolean remove(E object) {
152152
return removed;
153153
}
154154

155+
/**
156+
* Remove all objects from this collection
157+
*/
158+
public void clear() {
159+
this.collection.clear();
160+
this.totalProbability = 0;
161+
}
162+
155163
/**
156164
* Get a random object from this collection, based on probability.
157165
*
@@ -161,7 +169,7 @@ public boolean remove(E object) {
161169
*/
162170
public E get() {
163171
if(this.isEmpty()) {
164-
throw new IllegalStateException("Cannot get an element out of a empty collection");
172+
throw new IllegalStateException("Cannot get an object out of a empty collection");
165173
}
166174

167175
ProbabilitySetElement<E> toFind = new ProbabilitySetElement<>(null, 0);

src/test/java/com/lewdev/probabilitylib/ProbabilityCollectionTest.java

+52
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,58 @@ public void test_remove_duplicates() {
133133
assertTrue(collection.isEmpty());
134134
assertEquals(0, collection.getTotalProbability());
135135
}
136+
137+
@RepeatedTest(value = 10_000)
138+
public void test_clear() {
139+
ProbabilityCollection<String> collection = new ProbabilityCollection<>();
140+
assertEquals(0, collection.size());
141+
assertTrue(collection.isEmpty());
142+
assertEquals(0, collection.getTotalProbability());
143+
144+
collection.clear();
145+
146+
assertEquals(0, collection.size());
147+
assertTrue(collection.isEmpty());
148+
assertEquals(0, collection.getTotalProbability());
149+
150+
collection.add("tmp", 1);
151+
152+
assertEquals(1, collection.size());
153+
assertFalse(collection.isEmpty());
154+
assertEquals(1, collection.getTotalProbability());
155+
156+
collection.clear();
157+
158+
assertEquals(0, collection.size());
159+
assertTrue(collection.isEmpty());
160+
assertEquals(0, collection.getTotalProbability());
161+
162+
String t1 = "Hello";
163+
String t2 = "World";
164+
String t3 = "!";
165+
166+
for(int i = 0; i < 10; i++) {
167+
collection.add(t1, 10);
168+
}
169+
170+
for(int i = 0; i < 10; i++) {
171+
collection.add(t2, 10);
172+
}
173+
174+
for(int i = 0; i < 10; i++) {
175+
collection.add(t3, 10);
176+
}
177+
178+
assertEquals(30, collection.size());
179+
assertFalse(collection.isEmpty());
180+
assertEquals(300, collection.getTotalProbability());
181+
182+
collection.clear();
183+
184+
assertEquals(0, collection.getTotalProbability());
185+
assertEquals(0, collection.size());
186+
assertTrue(collection.isEmpty());
187+
}
136188

137189
@RepeatedTest(1_000_000)
138190
public void test_probability() {

0 commit comments

Comments
 (0)