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.CustomSet;
|
||||||
import datastructures.set.CustomStrictSet;
|
import datastructures.set.CustomStrictSet;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Decorator to intercept all state changes on a set and sync them with the persistence layer of the
|
* A Decorator to intercept all state changes on a set and sync them with the persistence layer of the
|
||||||
* application.
|
* application.
|
||||||
|
*
|
||||||
* @param <T> The data type that should be stored and updated in the persistence layer
|
* @param <T> The data type that should be stored and updated in the persistence layer
|
||||||
*/
|
*/
|
||||||
public class CustomSetPersistenceDecorator<T> implements CustomSet<T> {
|
public class CustomSetPersistenceDecorator<T> implements CustomSet<T> {
|
||||||
|
@ -21,6 +24,11 @@ public class CustomSetPersistenceDecorator<T> implements CustomSet<T> {
|
||||||
this.set = new CustomStrictSet(persistenceAdapter.getAll());
|
this.set = new CustomStrictSet(persistenceAdapter.getAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<T> find(Predicate<T> predicate) {
|
||||||
|
return set.find(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<T> stream() {
|
public Stream<T> stream() {
|
||||||
return set.stream();
|
return set.stream();
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class CategoryRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Category> getByName(CategoryName name) {
|
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) {
|
public CategoryId getIdByName(CategoryName name) {
|
||||||
|
@ -31,7 +31,7 @@ public class CategoryRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Category> getById(CategoryId id) {
|
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() {
|
public Set<Category> getAll() {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package link;
|
package link;
|
||||||
|
|
||||||
import datastructures.set.CustomSet;
|
|
||||||
import abstraction.CustomSetPersistenceDecorator;
|
import abstraction.CustomSetPersistenceDecorator;
|
||||||
import abstraction.PersistenceAdapter;
|
import abstraction.PersistenceAdapter;
|
||||||
|
import datastructures.set.CustomSet;
|
||||||
import exeptions.LinkAlreadyExists;
|
import exeptions.LinkAlreadyExists;
|
||||||
import exeptions.LinkDoesNotExist;
|
import exeptions.LinkDoesNotExist;
|
||||||
|
|
||||||
|
@ -17,11 +17,11 @@ public class LinkRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Link> getById(LinkId id) {
|
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) {
|
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) {
|
public void removeById(LinkId id) {
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
package datastructures.set;
|
package datastructures.set;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public interface CustomSet<T> {
|
public interface CustomSet<T> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Stream<T> stream();
|
Stream<T> stream();
|
||||||
|
|
||||||
boolean add(T value);
|
boolean add(T value);
|
||||||
|
|
||||||
boolean remove(T value);
|
boolean remove(T value);
|
||||||
|
|
||||||
|
Optional<T> find(Predicate<T> predicate);
|
||||||
|
|
||||||
Set<T> getSet();
|
Set<T> getSet();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package datastructures.set;
|
package datastructures.set;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class CustomStrictSet<T> implements CustomSet<T> {
|
public class CustomStrictSet<T> implements CustomSet<T> {
|
||||||
|
@ -16,6 +18,11 @@ public class CustomStrictSet<T> implements CustomSet<T> {
|
||||||
return this.set.stream();
|
return this.set.stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<T> find(Predicate<T> predicate) {
|
||||||
|
return set.stream().filter(predicate).findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<T> getSet() {
|
public Set<T> getSet() {
|
||||||
return this.set;
|
return this.set;
|
||||||
|
|
Reference in a new issue