Code Smell: Remove duplicate logic for searching in sets
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
aef1acf43e
commit
e4f1670742
|
@ -3,12 +3,15 @@ package abstraction;
|
|||
import datastructures.set.CustomSet;
|
||||
import datastructures.set.CustomStrictSet;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
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> {
|
||||
|
@ -22,12 +25,17 @@ public class CustomSetPersistenceDecorator<T> implements CustomSet<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream(){
|
||||
public Optional<T> find(Predicate<T> predicate) {
|
||||
return set.find(predicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream() {
|
||||
return set.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(T value){
|
||||
public boolean add(T value) {
|
||||
persistenceAdapter.add(value);
|
||||
return set.add(value);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ public class CategoryRepository {
|
|||
}
|
||||
|
||||
public Optional<Category> getByName(CategoryName name) {
|
||||
return categories.stream().filter(category -> category.getName().equals(name)).findFirst();
|
||||
return categories.find(category -> category.getName().equals(name));
|
||||
}
|
||||
|
||||
public CategoryId getIdByName(CategoryName name) {
|
||||
|
@ -31,7 +31,7 @@ public class CategoryRepository {
|
|||
}
|
||||
|
||||
public Optional<Category> getById(CategoryId id) {
|
||||
return categories.stream().filter(category -> category.getId().equals(id)).findFirst();
|
||||
return categories.find(category -> category.getId().equals(id));
|
||||
}
|
||||
|
||||
public Set<Category> getAll() {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package link;
|
||||
|
||||
import datastructures.set.CustomSet;
|
||||
import abstraction.CustomSetPersistenceDecorator;
|
||||
import abstraction.PersistenceAdapter;
|
||||
import datastructures.set.CustomSet;
|
||||
import exeptions.LinkAlreadyExists;
|
||||
import exeptions.LinkDoesNotExist;
|
||||
|
||||
|
@ -17,11 +17,11 @@ public class LinkRepository {
|
|||
}
|
||||
|
||||
public Optional<Link> getById(LinkId id) {
|
||||
return links.stream().filter(link -> link.getId().equals(id)).findFirst();
|
||||
return links.find(link -> link.getId().equals(id));
|
||||
}
|
||||
|
||||
public Optional<Link> getByUrl(LinkUrl url) {
|
||||
return links.stream().filter(link -> link.getUrl().equals(url)).findFirst();
|
||||
return links.find(link -> link.getUrl().equals(url));
|
||||
}
|
||||
|
||||
public void removeById(LinkId id) {
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
package datastructures.set;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface CustomSet<T> {
|
||||
|
||||
|
||||
|
||||
Stream<T> stream();
|
||||
|
||||
boolean add(T value);
|
||||
|
||||
boolean remove(T value);
|
||||
|
||||
Optional<T> find(Predicate<T> predicate);
|
||||
|
||||
Set<T> getSet();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package datastructures.set;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CustomStrictSet<T> implements CustomSet<T> {
|
||||
|
@ -16,6 +18,11 @@ public class CustomStrictSet<T> implements CustomSet<T> {
|
|||
return this.set.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> find(Predicate<T> predicate) {
|
||||
return set.stream().filter(predicate).findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> getSet() {
|
||||
return this.set;
|
||||
|
|
Reference in a new issue