Move custom set to abstraction package
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0da4c57409
commit
aef1acf43e
9 changed files with 18 additions and 10 deletions
|
@ -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();
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package datastructures.set;
|
||||
|
||||
public class ElementAlreadyInSet extends RuntimeException {
|
||||
|
||||
public ElementAlreadyInSet(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package datastructures.set;
|
||||
|
||||
public class ElementNotInSet extends RuntimeException {
|
||||
|
||||
public ElementNotInSet(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
Reference in a new issue