Add Generic way of calling Persistence Adapter when the state of a set changes
This commit is contained in:
parent
9c974db33f
commit
a1a67b3a7c
17
3-Domain/src/main/java/abstraction/CustomSet.java
Normal file
17
3-Domain/src/main/java/abstraction/CustomSet.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package abstraction;
|
||||
|
||||
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,43 @@
|
|||
package abstraction;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A Decorator to intercept all state changes on a set and sync them with the persistence layer of the
|
||||
* application.
|
||||
* @param <T> The data type that should be stored and updated in the persistence layer
|
||||
*/
|
||||
public class CustomSetPersistenceDecorator<T> implements CustomSet<T> {
|
||||
|
||||
private final CustomSet<T> set;
|
||||
private final PersistenceAdapter<T> persistenceAdapter;
|
||||
|
||||
public CustomSetPersistenceDecorator(PersistenceAdapter<T> persistenceAdapter) {
|
||||
this.persistenceAdapter = persistenceAdapter;
|
||||
this.set = new CustomStrictSet(persistenceAdapter.getAll());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream(){
|
||||
return set.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(T value){
|
||||
persistenceAdapter.add(value);
|
||||
return set.add(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(T value) {
|
||||
persistenceAdapter.remove(value);
|
||||
return this.set.remove(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> getSet() {
|
||||
return this.set.getSet();
|
||||
}
|
||||
|
||||
}
|
47
3-Domain/src/main/java/abstraction/CustomStrictSet.java
Normal file
47
3-Domain/src/main/java/abstraction/CustomStrictSet.java
Normal file
|
@ -0,0 +1,47 @@
|
|||
package abstraction;
|
||||
|
||||
import exeptions.ElementAlreadyInSet;
|
||||
import exeptions.ElementNotInSet;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
11
3-Domain/src/main/java/abstraction/PersistenceAdapter.java
Normal file
11
3-Domain/src/main/java/abstraction/PersistenceAdapter.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
package abstraction;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface PersistenceAdapter<T> {
|
||||
|
||||
Set<T> getAll();
|
||||
Boolean add(T value);
|
||||
Boolean remove(T value);
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package category;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface CategoryPersistenceAdapter {
|
||||
|
||||
Set<Category> getAll();
|
||||
Boolean add(Category category);
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package link;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface LinkPersistenceAdapter {
|
||||
|
||||
Set<Link> getAll();
|
||||
Boolean add(Link link);
|
||||
|
||||
}
|
Reference in a new issue