Skip to content

Commit 4aa8e6a

Browse files
authored
Updated TwoPSet to use Generics instead of Strings (TheAlgorithms#4981)
1 parent b8b1dea commit 4aa8e6a

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
* @author itakurah (Niklas Hoefflin) (https://github.com/itakurah)
1616
*/
1717

18-
public class TwoPSet {
19-
private Set<String> setA;
20-
private Set<String> setR;
18+
public class TwoPSet<T> {
19+
private final Set<T> setA;
20+
private final Set<T> setR;
2121

2222
/**
2323
* Constructs an empty Two-Phase Set.
@@ -33,7 +33,7 @@ public TwoPSet() {
3333
* @param element The element to be checked.
3434
* @return True if the element is in the set and has not been removed, otherwise false.
3535
*/
36-
public boolean lookup(String element) {
36+
public boolean lookup(T element) {
3737
return setA.contains(element) && !setR.contains(element);
3838
}
3939

@@ -42,7 +42,7 @@ public boolean lookup(String element) {
4242
*
4343
* @param element The element to be added.
4444
*/
45-
public void add(String element) {
45+
public void add(T element) {
4646
setA.add(element);
4747
}
4848

@@ -51,7 +51,7 @@ public void add(String element) {
5151
*
5252
* @param element The element to be removed.
5353
*/
54-
public void remove(String element) {
54+
public void remove(T element) {
5555
if (lookup(element)) {
5656
setR.add(element);
5757
}
@@ -63,7 +63,7 @@ public void remove(String element) {
6363
* @param otherSet The other 2P-Set to compare with.
6464
* @return True if both SetA and SetR are subset, otherwise false.
6565
*/
66-
public boolean compare(TwoPSet otherSet) {
66+
public boolean compare(TwoPSet<T> otherSet) {
6767
return otherSet.setA.containsAll(setA) && otherSet.setR.containsAll(setR);
6868
}
6969

@@ -73,8 +73,8 @@ public boolean compare(TwoPSet otherSet) {
7373
* @param otherSet The other 2P-Set to merge with.
7474
* @return A new 2P-Set containing the merged elements.
7575
*/
76-
public TwoPSet merge(TwoPSet otherSet) {
77-
TwoPSet mergedSet = new TwoPSet();
76+
public TwoPSet<T> merge(TwoPSet<T> otherSet) {
77+
TwoPSet<T> mergedSet = new TwoPSet<>();
7878
mergedSet.setA.addAll(this.setA);
7979
mergedSet.setA.addAll(otherSet.setA);
8080
mergedSet.setR.addAll(this.setR);

src/test/java/com/thealgorithms/datastructures/crdt/TwoPSetTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
class TwoPSetTest {
99

10-
private TwoPSet set;
10+
private TwoPSet<String> set;
1111

1212
@BeforeEach
1313
void setUp() {
14-
set = new TwoPSet();
14+
set = new TwoPSet<>();
1515
}
1616

1717
@Test
@@ -38,10 +38,10 @@ void testRemove() {
3838

3939
@Test
4040
void testCompare() {
41-
TwoPSet set1 = new TwoPSet();
41+
TwoPSet<String> set1 = new TwoPSet<>();
4242
set1.add("A");
4343
set1.add("B");
44-
TwoPSet set2 = new TwoPSet();
44+
TwoPSet<String> set2 = new TwoPSet<>();
4545
set2.add("A");
4646
assertFalse(set1.compare(set2));
4747
set2.add("B");
@@ -54,13 +54,13 @@ void testCompare() {
5454

5555
@Test
5656
void testMerge() {
57-
TwoPSet set1 = new TwoPSet();
57+
TwoPSet<String> set1 = new TwoPSet<>();
5858
set1.add("A");
5959
set1.add("B");
60-
TwoPSet set2 = new TwoPSet();
60+
TwoPSet<String> set2 = new TwoPSet<>();
6161
set2.add("B");
6262
set2.add("C");
63-
TwoPSet mergedSet = set1.merge(set2);
63+
TwoPSet<String> mergedSet = set1.merge(set2);
6464
assertTrue(mergedSet.lookup("A"));
6565
assertTrue(mergedSet.lookup("B"));
6666
assertTrue(mergedSet.lookup("C"));

0 commit comments

Comments
 (0)