Move custom set to abstraction package
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
qvalentin 2022-04-22 13:52:49 +02:00
parent 0da4c57409
commit aef1acf43e
Signed by: qvalentin
GPG key ID: C979FA1EAFCABF1C
9 changed files with 18 additions and 10 deletions

View file

@ -0,0 +1,17 @@
package datastructures.set;
import java.util.Set;
import java.util.stream.Stream;
public interface CustomSet<T> {
Stream<T> stream();
boolean add(T value);
boolean remove(T value);
Set<T> getSet();
}

View file

@ -0,0 +1,44 @@
package datastructures.set;
import java.util.Set;
import java.util.stream.Stream;
public class CustomStrictSet<T> implements CustomSet<T> {
private final Set<T> set;
public CustomStrictSet(Set<T> set) {
this.set = set;
}
@Override
public Stream<T> stream() {
return this.set.stream();
}
@Override
public Set<T> getSet() {
return this.set;
}
@Override
public boolean add(T value) {
if (this.set.contains(value)) {
throw new ElementAlreadyInSet("Tried adding " + value + " of class " + value
.getClass()
.getName() + " but the set already contains it.");
}
return this.set.add(value);
}
@Override
public boolean remove(T value) {
if (!this.set.contains(value)) {
throw new ElementNotInSet("Tried removing " + value + " of class " + value
.getClass()
.getName() + " but the set not contains it.");
}
return this.set.remove(value);
}
}

View file

@ -0,0 +1,8 @@
package datastructures.set;
public class ElementAlreadyInSet extends RuntimeException {
public ElementAlreadyInSet(String message) {
super(message);
}
}

View file

@ -0,0 +1,8 @@
package datastructures.set;
public class ElementNotInSet extends RuntimeException {
public ElementNotInSet(String message) {
super(message);
}
}