add custom tag repo and usecase
This commit is contained in:
parent
d6efbf757f
commit
c4ef8b9181
16
2-Application/src/main/java/tag/CustomTagsUseCase.java
Normal file
16
2-Application/src/main/java/tag/CustomTagsUseCase.java
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package tag;
|
||||||
|
|
||||||
|
import tag.matcherImplementations.CustomTagMatcher;
|
||||||
|
|
||||||
|
public class CustomTagsUseCase {
|
||||||
|
|
||||||
|
private final TagMatcherRepository tagMatcherRepository;
|
||||||
|
|
||||||
|
public CustomTagsUseCase(TagMatcherRepository tagMatcherRepository) {
|
||||||
|
this.tagMatcherRepository = tagMatcherRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
|
||||||
|
tagMatcherRepository.addCustomTagMatcher(customTagMatcher);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ import java.util.Set;
|
||||||
public interface PersistenceAdapter<T> {
|
public interface PersistenceAdapter<T> {
|
||||||
|
|
||||||
Set<T> getAll();
|
Set<T> getAll();
|
||||||
Boolean add(T value);
|
void add(T value);
|
||||||
Boolean remove(T value);
|
void remove(T value);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package exeptions;
|
||||||
|
|
||||||
|
import tag.TagName;
|
||||||
|
|
||||||
|
public class TagMatcherAlreadyExists extends RuntimeException {
|
||||||
|
|
||||||
|
public TagMatcherAlreadyExists(TagName name) {
|
||||||
|
super("A Tag Matcher with the name " + name + " already exists.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,11 +2,28 @@ package tag;
|
||||||
|
|
||||||
import link.LinkUrl;
|
import link.LinkUrl;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface TagMatcher {
|
public abstract class TagMatcher {
|
||||||
|
|
||||||
OptionalTag ifMatches(LinkUrl linkUrl);
|
protected TagName tagName;
|
||||||
|
|
||||||
|
public TagName getTagName() {
|
||||||
|
return tagName;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract OptionalTag ifMatches(LinkUrl linkUrl);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof TagMatcher that)) return false;
|
||||||
|
|
||||||
|
return Objects.equals(tagName, that.tagName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return tagName != null ? tagName.hashCode() : 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,49 @@
|
||||||
package tag;
|
package tag;
|
||||||
|
|
||||||
|
import abstraction.CustomSet;
|
||||||
|
import abstraction.CustomSetPersistenceDecorator;
|
||||||
|
import abstraction.PersistenceAdapter;
|
||||||
|
import exeptions.TagMatcherAlreadyExists;
|
||||||
|
import tag.matcherImplementations.CustomTagMatcher;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class TagMatcherRepository {
|
public class TagMatcherRepository {
|
||||||
|
|
||||||
Set<TagMatcher> tagMatchers = Set.of();
|
final Set<TagMatcher> staticTagMatchers;
|
||||||
|
|
||||||
public TagMatcherRepository(final TagPersistenceAdapter tagPersistenceAdapter, final Set<TagMatcher> staticTagMatchers) {
|
final private CustomSet<CustomTagMatcher> customTagMatchers;
|
||||||
tagMatchers.addAll(staticTagMatchers);
|
|
||||||
tagMatchers.addAll(tagPersistenceAdapter.getCustomTagMatchers());
|
public TagMatcherRepository(final PersistenceAdapter<CustomTagMatcher> customTagMatcherPersistenceAdapter,
|
||||||
|
final Set<TagMatcher> staticTagMatchers) {
|
||||||
|
this.staticTagMatchers = staticTagMatchers;
|
||||||
|
this.customTagMatchers =
|
||||||
|
new CustomSetPersistenceDecorator<CustomTagMatcher>(customTagMatcherPersistenceAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<TagMatcher> getTagMatchers() {
|
public Set<TagMatcher> getTagMatchers() {
|
||||||
return new HashSet<>(tagMatchers);
|
var set = new HashSet<TagMatcher>(staticTagMatchers);
|
||||||
|
set.addAll(customTagMatchers.getSet());
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) {
|
||||||
|
checkDuplicates(customTagMatcher);
|
||||||
|
|
||||||
|
customTagMatchers.add(customTagMatcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkDuplicates(CustomTagMatcher customTagMatcher) {
|
||||||
|
Predicate<TagMatcher> predicate = matcher -> (matcher.tagName.equals(customTagMatcher.getTagName()));
|
||||||
|
var alreadyExists = staticTagMatchers.stream().anyMatch(predicate) || customTagMatchers
|
||||||
|
.getSet()
|
||||||
|
.stream()
|
||||||
|
.anyMatch(predicate);
|
||||||
|
|
||||||
|
if (alreadyExists) {
|
||||||
|
throw new TagMatcherAlreadyExists(customTagMatcher.tagName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,9 @@ import tag.TagName;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class CustomTagMatcher implements TagMatcher {
|
public class CustomTagMatcher extends TagMatcher {
|
||||||
|
|
||||||
private final Pattern regex;
|
private final Pattern regex;
|
||||||
private final TagName tagName;
|
|
||||||
|
|
||||||
public CustomTagMatcher(TagName name, String regexString) {
|
public CustomTagMatcher(TagName name, String regexString) {
|
||||||
this.regex = Pattern.compile(regexString); // TODO this could throw??
|
this.regex = Pattern.compile(regexString); // TODO this could throw??
|
||||||
|
@ -28,5 +27,4 @@ public class CustomTagMatcher implements TagMatcher {
|
||||||
}
|
}
|
||||||
return OptionalTag.empty();
|
return OptionalTag.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,12 @@ import tag.Tag;
|
||||||
import tag.TagMatcher;
|
import tag.TagMatcher;
|
||||||
import tag.TagName;
|
import tag.TagName;
|
||||||
|
|
||||||
public class GitHubTagMatcher implements TagMatcher {
|
public class GitHubTagMatcher extends TagMatcher {
|
||||||
|
|
||||||
|
private final TagName tagName = new TagName("github");
|
||||||
|
|
||||||
public TagName getName() {
|
public TagName getName() {
|
||||||
return new TagName("github");
|
return tagName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAdditionInfo() {
|
public String getAdditionInfo() {
|
||||||
|
|
Reference in a new issue