Updated TwoPSet to use Generics instead of Strings (#4981)

This commit is contained in:
Niklas Hoefflin
2023-12-11 19:58:56 +01:00
committed by GitHub
parent b8b1dea38d
commit 4aa8e6a0eb
2 changed files with 16 additions and 16 deletions

View File

@ -15,9 +15,9 @@ import java.util.Set;
* @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah)
*/ */
public class TwoPSet { public class TwoPSet<T> {
private Set<String> setA; private final Set<T> setA;
private Set<String> setR; private final Set<T> setR;
/** /**
* Constructs an empty Two-Phase Set. * Constructs an empty Two-Phase Set.
@ -33,7 +33,7 @@ public class TwoPSet {
* @param element The element to be checked. * @param element The element to be checked.
* @return True if the element is in the set and has not been removed, otherwise false. * @return True if the element is in the set and has not been removed, otherwise false.
*/ */
public boolean lookup(String element) { public boolean lookup(T element) {
return setA.contains(element) && !setR.contains(element); return setA.contains(element) && !setR.contains(element);
} }
@ -42,7 +42,7 @@ public class TwoPSet {
* *
* @param element The element to be added. * @param element The element to be added.
*/ */
public void add(String element) { public void add(T element) {
setA.add(element); setA.add(element);
} }
@ -51,7 +51,7 @@ public class TwoPSet {
* *
* @param element The element to be removed. * @param element The element to be removed.
*/ */
public void remove(String element) { public void remove(T element) {
if (lookup(element)) { if (lookup(element)) {
setR.add(element); setR.add(element);
} }
@ -63,7 +63,7 @@ public class TwoPSet {
* @param otherSet The other 2P-Set to compare with. * @param otherSet The other 2P-Set to compare with.
* @return True if both SetA and SetR are subset, otherwise false. * @return True if both SetA and SetR are subset, otherwise false.
*/ */
public boolean compare(TwoPSet otherSet) { public boolean compare(TwoPSet<T> otherSet) {
return otherSet.setA.containsAll(setA) && otherSet.setR.containsAll(setR); return otherSet.setA.containsAll(setA) && otherSet.setR.containsAll(setR);
} }
@ -73,8 +73,8 @@ public class TwoPSet {
* @param otherSet The other 2P-Set to merge with. * @param otherSet The other 2P-Set to merge with.
* @return A new 2P-Set containing the merged elements. * @return A new 2P-Set containing the merged elements.
*/ */
public TwoPSet merge(TwoPSet otherSet) { public TwoPSet<T> merge(TwoPSet<T> otherSet) {
TwoPSet mergedSet = new TwoPSet(); TwoPSet<T> mergedSet = new TwoPSet<>();
mergedSet.setA.addAll(this.setA); mergedSet.setA.addAll(this.setA);
mergedSet.setA.addAll(otherSet.setA); mergedSet.setA.addAll(otherSet.setA);
mergedSet.setR.addAll(this.setR); mergedSet.setR.addAll(this.setR);

View File

@ -7,11 +7,11 @@ import org.junit.jupiter.api.Test;
class TwoPSetTest { class TwoPSetTest {
private TwoPSet set; private TwoPSet<String> set;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
set = new TwoPSet(); set = new TwoPSet<>();
} }
@Test @Test
@ -38,10 +38,10 @@ class TwoPSetTest {
@Test @Test
void testCompare() { void testCompare() {
TwoPSet set1 = new TwoPSet(); TwoPSet<String> set1 = new TwoPSet<>();
set1.add("A"); set1.add("A");
set1.add("B"); set1.add("B");
TwoPSet set2 = new TwoPSet(); TwoPSet<String> set2 = new TwoPSet<>();
set2.add("A"); set2.add("A");
assertFalse(set1.compare(set2)); assertFalse(set1.compare(set2));
set2.add("B"); set2.add("B");
@ -54,13 +54,13 @@ class TwoPSetTest {
@Test @Test
void testMerge() { void testMerge() {
TwoPSet set1 = new TwoPSet(); TwoPSet<String> set1 = new TwoPSet<>();
set1.add("A"); set1.add("A");
set1.add("B"); set1.add("B");
TwoPSet set2 = new TwoPSet(); TwoPSet<String> set2 = new TwoPSet<>();
set2.add("B"); set2.add("B");
set2.add("C"); set2.add("C");
TwoPSet mergedSet = set1.merge(set2); TwoPSet<String> mergedSet = set1.merge(set2);
assertTrue(mergedSet.lookup("A")); assertTrue(mergedSet.lookup("A"));
assertTrue(mergedSet.lookup("B")); assertTrue(mergedSet.lookup("B"));
assertTrue(mergedSet.lookup("C")); assertTrue(mergedSet.lookup("C"));