diff --git a/2-Application/src/main/java/tag/CustomTagsUseCase.java b/2-Application/src/main/java/tag/CustomTagsUseCase.java new file mode 100644 index 0000000..8438b9b --- /dev/null +++ b/2-Application/src/main/java/tag/CustomTagsUseCase.java @@ -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); + } +} diff --git a/3-Domain/src/main/java/abstraction/PersistenceAdapter.java b/3-Domain/src/main/java/abstraction/PersistenceAdapter.java index 98ee6b8..199b4aa 100644 --- a/3-Domain/src/main/java/abstraction/PersistenceAdapter.java +++ b/3-Domain/src/main/java/abstraction/PersistenceAdapter.java @@ -5,7 +5,7 @@ import java.util.Set; public interface PersistenceAdapter { Set getAll(); - Boolean add(T value); - Boolean remove(T value); + void add(T value); + void remove(T value); } diff --git a/3-Domain/src/main/java/exeptions/TagMatcherAlreadyExists.java b/3-Domain/src/main/java/exeptions/TagMatcherAlreadyExists.java new file mode 100644 index 0000000..5f9368c --- /dev/null +++ b/3-Domain/src/main/java/exeptions/TagMatcherAlreadyExists.java @@ -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."); + } +} diff --git a/3-Domain/src/main/java/tag/TagMatcher.java b/3-Domain/src/main/java/tag/TagMatcher.java index 86177b7..8122d6b 100644 --- a/3-Domain/src/main/java/tag/TagMatcher.java +++ b/3-Domain/src/main/java/tag/TagMatcher.java @@ -2,11 +2,28 @@ package tag; import link.LinkUrl; -import java.util.Collection; -import java.util.Optional; +import java.util.Objects; -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; + } } diff --git a/3-Domain/src/main/java/tag/TagMatcherRepository.java b/3-Domain/src/main/java/tag/TagMatcherRepository.java index c609884..6577cf7 100644 --- a/3-Domain/src/main/java/tag/TagMatcherRepository.java +++ b/3-Domain/src/main/java/tag/TagMatcherRepository.java @@ -1,18 +1,49 @@ 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.Set; +import java.util.function.Predicate; public class TagMatcherRepository { - Set tagMatchers = Set.of(); + final Set staticTagMatchers; - public TagMatcherRepository(final TagPersistenceAdapter tagPersistenceAdapter, final Set staticTagMatchers) { - tagMatchers.addAll(staticTagMatchers); - tagMatchers.addAll(tagPersistenceAdapter.getCustomTagMatchers()); + final private CustomSet customTagMatchers; + + public TagMatcherRepository(final PersistenceAdapter customTagMatcherPersistenceAdapter, + final Set staticTagMatchers) { + this.staticTagMatchers = staticTagMatchers; + this.customTagMatchers = + new CustomSetPersistenceDecorator(customTagMatcherPersistenceAdapter); } public Set getTagMatchers() { - return new HashSet<>(tagMatchers); + var set = new HashSet(staticTagMatchers); + set.addAll(customTagMatchers.getSet()); + return set; + } + + public void addCustomTagMatcher(CustomTagMatcher customTagMatcher) { + checkDuplicates(customTagMatcher); + + customTagMatchers.add(customTagMatcher); + } + + private void checkDuplicates(CustomTagMatcher customTagMatcher) { + Predicate 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); + } } } diff --git a/3-Domain/src/main/java/tag/matcherImplementations/CustomTagMatcher.java b/3-Domain/src/main/java/tag/matcherImplementations/CustomTagMatcher.java index 083aaa6..ed64255 100644 --- a/3-Domain/src/main/java/tag/matcherImplementations/CustomTagMatcher.java +++ b/3-Domain/src/main/java/tag/matcherImplementations/CustomTagMatcher.java @@ -9,10 +9,9 @@ import tag.TagName; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class CustomTagMatcher implements TagMatcher { +public class CustomTagMatcher extends TagMatcher { private final Pattern regex; - private final TagName tagName; public CustomTagMatcher(TagName name, String regexString) { this.regex = Pattern.compile(regexString); // TODO this could throw?? @@ -28,5 +27,4 @@ public class CustomTagMatcher implements TagMatcher { } return OptionalTag.empty(); } - } diff --git a/3-Domain/src/main/java/tag/matcherImplementations/GitHubTagMatcher.java b/3-Domain/src/main/java/tag/matcherImplementations/GitHubTagMatcher.java index 5acf3e1..f9e299a 100644 --- a/3-Domain/src/main/java/tag/matcherImplementations/GitHubTagMatcher.java +++ b/3-Domain/src/main/java/tag/matcherImplementations/GitHubTagMatcher.java @@ -6,10 +6,12 @@ import tag.Tag; import tag.TagMatcher; import tag.TagName; -public class GitHubTagMatcher implements TagMatcher { +public class GitHubTagMatcher extends TagMatcher { + + private final TagName tagName = new TagName("github"); public TagName getName() { - return new TagName("github"); + return tagName; } public String getAdditionInfo() {