diff --git a/filterer/README.MD b/filterer/README.MD new file mode 100644 index 000000000..8a2526048 --- /dev/null +++ b/filterer/README.MD @@ -0,0 +1,45 @@ +--- # this is so called 'Yaml Front Matter', read up on it here: http://jekyllrb.com/docs/frontmatter/ +layout: pattern +title: Filterer Pattern +folder: filterer +permalink: /patterns/filterer/ +description: Design pattern that helps container-like objects to return filtered version of themselves.# short meta description that shows in Google search results +categories: + - Functional +tags: + - Extensibility +--- + +## Name / classification +Filterer Pattern + +## Intent +The intent of this design pattern is to to introduce a functional interface that will add a functionality for container-like objects to easily return filtered versions of themselves. + +## Explanation +The container-like object needs to have a method that returns an instance of `Filterer`. This helper interface gives +ability to covariantly specify a lower bound of contravariant `Predicate` in the subinterfaces of interfaces representing the container-like objects. + +## Class diagram +![Filterer](./etc/filterer.png "Filterer") + +## Applicability +Pattern can be used when working with container-like objects that use subtyping, instead of parametrizing(generics) for extensible class structure. +It enables you to easily extend filtering ability of container-like objects as business requirements change. + +## Tutorials +* [Article about Filterer pattern posted on it's author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/) +* [Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html) + +## Known uses +One of the uses is present on the blog presented in this link. It presents how to use `Filterer` pattern to create text issue anaylyzer with support for test cases used for unit testing. + +## Consequences (the good and the bad, add criticism here) +Good : + * you can easily introduce new subtypes for container-like objects and subtypes for objects that are contained within them and still be able to filter easily be new properties of those new subtypes. + +Bad : + * covariant return types mixed with generics can be sometimes tricky + +## Credits +* Author of the pattern : [Tomasz Linkowski](https://tlinkowski.pl/) \ No newline at end of file diff --git a/filterer/etc/filterer.png b/filterer/etc/filterer.png new file mode 100644 index 000000000..f86764aa3 Binary files /dev/null and b/filterer/etc/filterer.png differ diff --git a/filterer/etc/filterer.urm.puml b/filterer/etc/filterer.urm.puml new file mode 100644 index 000000000..24060b6aa --- /dev/null +++ b/filterer/etc/filterer.urm.puml @@ -0,0 +1,132 @@ +@startuml +package com.iluwatar.filterer.domain { + interface Filterer { + + by(Predicate) : G {abstract} + } +} +package com.iluwatar.filterer.issue { + interface Issue { + + endOffset() : int {abstract} + + startOffset() : int {abstract} + + type() : IssueType {abstract} + } + interface IssueAwareText { + + filtered() : Filterer {abstract} + + issues() : List {abstract} + + text() : String {abstract} + } + class IssuePosition { + - endOffset : int + - startOffset : int + - IssuePosition(startOffset : int, endOffset : int) + ~ endOffset() : int + + equals(o : Object) : boolean + + hashCode() : int + + of(startOffset : int, endOffset : int) : IssuePosition {static} + ~ startOffset() : int + } + ~enum IssueType { + + GRAMMAR {static} + + SPELLING {static} + + valueOf(name : String) : IssueType {static} + + values() : IssueType[] {static} + } + interface IssueWiseText { + + filtered() : Filterer {abstract} + + issues() : List {abstract} + + text() : String {abstract} + } + interface ProbabilisticIssueAwareText { + + filtered() : Filterer {abstract} + + issues() : List {abstract} + } + interface ProbabilisticIssueWiseText { + + filtered() : Filterer {abstract} + + issues() : List {abstract} + } + interface ProbableIssue { + + probability() : double {abstract} + } + class SimpleIssue { + - issuePosition : IssuePosition + - issueType : IssueType + ~ SimpleIssue(issuePosition : IssuePosition, issueType : IssueType) + + endOffset() : int + + equals(o : Object) : boolean + + hashCode() : int + + startOffset() : int + + type() : IssueType + } + class SimpleIssueAwareText { + - issues : ImmutableList + - text : String + ~ SimpleIssueAwareText(text : String, issues : List) + + equals(o : Object) : boolean + + filtered() : Filterer + - filteredGroup(predicate : Predicate) : IssueAwareText + - filteredItems(predicate : Predicate) : ImmutableList + + hashCode() : int + + issues() : List + + text() : String + } + class SimpleIssueWiseText { + - issues : ImmutableList + - text : String + + SimpleIssueWiseText(text : String, issues : List) + + equals(o : Object) : boolean + + filtered() : Filterer + - filteredGroup(predicate : Predicate) : IssueWiseText + - filteredItems(predicate : Predicate) : ImmutableList + + hashCode() : int + + issues() : List + + text() : String + } + class SimpleProbabilisticIssueAwareText { + - issues : ImmutableList + - text : String + ~ SimpleProbabilisticIssueAwareText(text : String, issues : List) + + equals(o : Object) : boolean + + filtered() : Filterer + - filteredGroup(predicate : Predicate) : ProbabilisticIssueAwareText + - filteredItems(predicate : Predicate) : ImmutableList + + hashCode() : int + + issues() : List + + text() : String + } + class SimpleProbabilisticIssueWiseText { + - issues : ImmutableList + - text : String + + SimpleProbabilisticIssueWiseText(text : String, issues : List) + + equals(o : Object) : boolean + + filtered() : Filterer + - filteredGroup(predicate : Predicate) : ProbabilisticIssueWiseText + - filteredItems(predicate : Predicate) : ImmutableList + + hashCode() : int + + issues() : List + + text() : String + } + class SimpleProbableIssue { + - probability : double + ~ SimpleProbableIssue(issuePosition : IssuePosition, issueType : IssueType, probability : double) + + equals(o : Object) : boolean + + hashCode() : int + + probability() : double + } +} +SimpleIssueWiseText --> "-issues" Issue +SimpleProbabilisticIssueAwareText --> "-issues" ProbableIssue +SimpleIssue --> "-issueType" IssueType +SimpleIssueAwareText --> "-issues" Issue +SimpleProbabilisticIssueWiseText --> "-issues" ProbableIssue +SimpleIssue --> "-issuePosition" IssuePosition +ProbabilisticIssueAwareText --|> IssueAwareText +ProbabilisticIssueWiseText --|> IssueWiseText +ProbableIssue --|> Issue +SimpleIssue ..|> Issue +SimpleIssueAwareText ..|> IssueAwareText +SimpleIssueWiseText ..|> IssueWiseText +SimpleProbabilisticIssueAwareText ..|> ProbabilisticIssueAwareText +SimpleProbabilisticIssueWiseText ..|> ProbabilisticIssueWiseText +SimpleProbableIssue ..|> ProbableIssue +SimpleProbableIssue --|> SimpleIssue +@enduml \ No newline at end of file diff --git a/filterer/pom.xml b/filterer/pom.xml new file mode 100644 index 000000000..24dae571e --- /dev/null +++ b/filterer/pom.xml @@ -0,0 +1,76 @@ + + + + + java-design-patterns + com.iluwatar + 1.23.0-SNAPSHOT + + 4.0.0 + + filterer + + + + com.google.guava + guava + 29.0-jre + + + org.junit.jupiter + junit-jupiter-api + 5.6.2 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.6.2 + test + + + org.assertj + assertj-core + 3.16.1 + test + + + + + + + maven-surefire-plugin + 2.22.2 + + + maven-failsafe-plugin + 2.22.2 + + + + \ No newline at end of file diff --git a/filterer/src/main/java/com/iluwatar/filterer/domain/Filterer.java b/filterer/src/main/java/com/iluwatar/filterer/domain/Filterer.java new file mode 100644 index 000000000..17970c115 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/domain/Filterer.java @@ -0,0 +1,36 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.domain; + +import java.util.function.Predicate; + +/** + * Filterer helper interface. + * @param type of the container-like object. + * @param type of the elements contained within this container-like object. + */ +@FunctionalInterface +public interface Filterer { + G by(Predicate predicate); +} \ No newline at end of file diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/Issue.java b/filterer/src/main/java/com/iluwatar/filterer/issue/Issue.java new file mode 100644 index 000000000..957ade6e5 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/Issue.java @@ -0,0 +1,49 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +/** + * Represents an issue that can be detected in given text. + */ +public interface Issue { + /** + * Returns starting position where the issue begins. + * + * @return value representing starting position of the issue. + */ + int startOffset(); + + /** + * Returns ending position where the issue ends. + * + * @return value representing ending position of the issue. + */ + int endOffset(); + + /** + * Returns issue type. + * @return {@link IssueType} + */ + IssueType type(); +} diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/IssueAwareText.java b/filterer/src/main/java/com/iluwatar/filterer/issue/IssueAwareText.java new file mode 100644 index 000000000..8141ae849 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/IssueAwareText.java @@ -0,0 +1,55 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import com.iluwatar.filterer.domain.Filterer; + +import java.util.List; + +/** + * Represents text that is aware of issues that are present in it. + */ +public interface IssueAwareText { + + /** + * Returns the analyzed text. + * + * @return the analyzed text. + */ + String text(); + + /** + * Returns list of issues for this text. + * @return list of issues for this text. + */ + List issues(); + + /** + * Returns the instance of {@link Filterer} helper interface that allows to covariantly + * specify lower bound for predicate that we want to filter by. + * @return an instance of {@link Filterer} helper interface. + */ + Filterer filtered(); + +} diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/IssuePosition.java b/filterer/src/main/java/com/iluwatar/filterer/issue/IssuePosition.java new file mode 100644 index 000000000..a771c1a82 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/IssuePosition.java @@ -0,0 +1,76 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import java.util.Objects; + +/** + * Represents position of an issue. Takes starting and ending offset of issue in given text. + */ +public final class IssuePosition { + + private final int startOffset; + private final int endOffset; + + /** + * Factory method for constructing `IssuePosition` instances. + * @param startOffset starting offset of where the issue begins. + * @param endOffset ending offset of where the issue ends. + * @return new IssuePosition instance. + */ + public static IssuePosition of(final int startOffset, final int endOffset) { + return new IssuePosition(startOffset, endOffset); + } + + private IssuePosition(int startOffset, int endOffset) { + this.startOffset = startOffset; + this.endOffset = endOffset; + } + + int startOffset() { + return startOffset; + } + + int endOffset() { + return endOffset; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + IssuePosition that = (IssuePosition) o; + return startOffset == that.startOffset + && endOffset == that.endOffset; + } + + @Override + public int hashCode() { + return Objects.hash(startOffset, endOffset); + } +} diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/IssueType.java b/filterer/src/main/java/com/iluwatar/filterer/issue/IssueType.java new file mode 100644 index 000000000..ee2c12ce5 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/IssueType.java @@ -0,0 +1,26 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +enum IssueType { GRAMMAR, SPELLING } diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/ProbabilisticIssueAwareText.java b/filterer/src/main/java/com/iluwatar/filterer/issue/ProbabilisticIssueAwareText.java new file mode 100644 index 000000000..da15bdd99 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/ProbabilisticIssueAwareText.java @@ -0,0 +1,49 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import com.iluwatar.filterer.domain.Filterer; + +import java.util.List; + +/** + * Represents text that is aware of it's issues with given probability of their occurrence. + */ +public interface ProbabilisticIssueAwareText extends IssueAwareText { + + /** + * {@inheritDoc} + * @return + */ + @Override + List issues(); + + /** + * {@inheritDoc} + * @return + */ + @Override + Filterer filtered(); +} + diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/ProbableIssue.java b/filterer/src/main/java/com/iluwatar/filterer/issue/ProbableIssue.java new file mode 100644 index 000000000..ccb047fa4 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/ProbableIssue.java @@ -0,0 +1,35 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +/** + * Represents issue that is an issue with given probability. + */ +public interface ProbableIssue extends Issue { + /** + * Returns probability of occurrence of given issue. + * @return probability of occurrence of given issue. + */ + double probability(); +} \ No newline at end of file diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleIssue.java b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleIssue.java new file mode 100644 index 000000000..fde5b8d8e --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleIssue.java @@ -0,0 +1,79 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import java.util.Objects; + +public class SimpleIssue implements Issue { + + private final IssuePosition issuePosition; + private final IssueType issueType; + + SimpleIssue(final IssuePosition issuePosition, IssueType issueType) { + this.issuePosition = issuePosition; + this.issueType = issueType; + } + + /** + * {@inheritDoc} + */ + @Override + public int startOffset() { + return issuePosition.startOffset(); + } + + /** + * {@inheritDoc} + */ + @Override + public int endOffset() { + return issuePosition.endOffset(); + } + + /** + * {@inheritDoc} + */ + @Override + public IssueType type() { + return issueType; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SimpleIssue that = (SimpleIssue) o; + return issuePosition.equals(that.issuePosition) + && issueType == that.issueType; + } + + @Override + public int hashCode() { + return Objects.hash(issuePosition, issueType); + } +} diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleIssueAwareText.java b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleIssueAwareText.java new file mode 100644 index 000000000..c654a3aaa --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleIssueAwareText.java @@ -0,0 +1,98 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import com.google.common.collect.ImmutableList; +import com.iluwatar.filterer.domain.Filterer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; + +/** + * {@inheritDoc} + */ +public class SimpleIssueAwareText implements IssueAwareText { + + private final String text; + private final ImmutableList issues; + + SimpleIssueAwareText(final String text, final List issues) { + this.text = text; + this.issues = ImmutableList.copyOf(issues); + } + + /** + * {@inheritDoc} + */ + @Override + public String text() { + return text; + } + + /** + * {@inheritDoc} + */ + @Override + public List issues() { + return new ArrayList<>(issues); + } + + /** + * {@inheritDoc} + */ + @Override + public Filterer filtered() { + return this::filteredGroup; + } + + private IssueAwareText filteredGroup(Predicate predicate) { + return new SimpleIssueAwareText(this.text, filteredItems(predicate)); + } + + private ImmutableList filteredItems(Predicate predicate) { + return this.issues.stream() + .filter(predicate) + .collect(ImmutableList.toImmutableList()); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SimpleIssueAwareText that = (SimpleIssueAwareText) o; + return text.equals(that.text) + && issues.equals(that.issues); + } + + @Override + public int hashCode() { + return Objects.hash(text, issues); + } +} diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleProbabilisticIssueAwareText.java b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleProbabilisticIssueAwareText.java new file mode 100644 index 000000000..e1b4afc82 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleProbabilisticIssueAwareText.java @@ -0,0 +1,101 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import com.google.common.collect.ImmutableList; +import com.iluwatar.filterer.domain.Filterer; + +import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; + +/** + * {@inheritDoc} + */ +public class SimpleProbabilisticIssueAwareText implements ProbabilisticIssueAwareText { + + private final String text; + private final ImmutableList issues; + + SimpleProbabilisticIssueAwareText(final String text, final List issues) { + this.text = text; + this.issues = ImmutableList.copyOf(issues); + } + + /** + * {@inheritDoc} + */ + @Override + public String text() { + return text; + } + + /** + * {@inheritDoc} + */ + @Override + public List issues() { + return issues; + } + + /** + * {@inheritDoc} + */ + @Override + public Filterer filtered() { + return this::filteredGroup; + } + + private ProbabilisticIssueAwareText filteredGroup( + final Predicate predicate + ) { + return new SimpleProbabilisticIssueAwareText(this.text, filteredItems(predicate)); + } + + private ImmutableList filteredItems( + final Predicate predicate + ) { + return this.issues.stream() + .filter(predicate) + .collect(ImmutableList.toImmutableList()); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SimpleProbabilisticIssueAwareText that = (SimpleProbabilisticIssueAwareText) o; + return text.equals(that.text) + && issues.equals(that.issues); + } + + @Override + public int hashCode() { + return Objects.hash(text, issues); + } +} diff --git a/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleProbableIssue.java b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleProbableIssue.java new file mode 100644 index 000000000..2b7672256 --- /dev/null +++ b/filterer/src/main/java/com/iluwatar/filterer/issue/SimpleProbableIssue.java @@ -0,0 +1,70 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import java.util.Objects; + +/** + * {@inheritDoc} + */ +public class SimpleProbableIssue extends SimpleIssue implements ProbableIssue { + + private final double probability; + + SimpleProbableIssue(final IssuePosition issuePosition, + final IssueType issueType, + final double probability + ) { + super(issuePosition, issueType); + this.probability = probability; + } + + /** + * {@inheritDoc} + */ + @Override + public double probability() { + return probability; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + if (!super.equals(o)) { + return false; + } + SimpleProbableIssue that = (SimpleProbableIssue) o; + return Double.compare(that.probability, probability) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), probability); + } +} diff --git a/filterer/src/test/java/com/iluwatar/filterer/issue/SimpleIssueAwareTextTest.java b/filterer/src/test/java/com/iluwatar/filterer/issue/SimpleIssueAwareTextTest.java new file mode 100644 index 000000000..1278128ae --- /dev/null +++ b/filterer/src/test/java/com/iluwatar/filterer/issue/SimpleIssueAwareTextTest.java @@ -0,0 +1,52 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class SimpleIssueAwareTextTest { + + @Test + void shouldFilterByStartOffset() { + //given + SimpleIssue spellingIssue = new SimpleIssue(IssuePosition.of(4, 5), IssueType.SPELLING); + SimpleIssue grammarIssue = new SimpleIssue(IssuePosition.of(8, 12), IssueType.GRAMMAR); + List issues = List.of(spellingIssue, grammarIssue); + + SimpleIssueAwareText simpleIssueWiseText = new SimpleIssueAwareText("I mihgt gone there", issues); + + //when + IssueAwareText filtered = simpleIssueWiseText.filtered() + .by(issue1 -> issue1.startOffset() == 4); + + //then + assertThat(filtered.issues()).hasSize(1); + assertThat(filtered.issues()).element(0).isEqualTo(spellingIssue); + } + +} diff --git a/filterer/src/test/java/com/iluwatar/filterer/issue/SimpleProbabilisticIssueAwareTextTest.java b/filterer/src/test/java/com/iluwatar/filterer/issue/SimpleProbabilisticIssueAwareTextTest.java new file mode 100644 index 000000000..142415111 --- /dev/null +++ b/filterer/src/test/java/com/iluwatar/filterer/issue/SimpleProbabilisticIssueAwareTextTest.java @@ -0,0 +1,52 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.filterer.issue; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class SimpleProbabilisticIssueAwareTextTest { + + @Test + void shouldFilterByProbability() { + //given + ProbableIssue spellingIssue = new SimpleProbableIssue(IssuePosition.of(4, 5), IssueType.SPELLING, 100); + ProbableIssue grammarIssue = new SimpleProbableIssue(IssuePosition.of(8, 12), IssueType.GRAMMAR, 99); + List issues = List.of(spellingIssue, grammarIssue); + + SimpleProbabilisticIssueAwareText simpleIssueWiseText = new SimpleProbabilisticIssueAwareText("I mihgt gone there", issues); + + //when + ProbabilisticIssueAwareText filtered = simpleIssueWiseText.filtered() + .by(issue1 -> Double.compare(issue1.probability(), 99) == 0); + + //then + assertThat(filtered.issues()).hasSize(1); + assertThat(filtered.issues()).element(0).isEqualTo(grammarIssue); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0718ff045..1e34bcb67 100644 --- a/pom.xml +++ b/pom.xml @@ -193,6 +193,7 @@ strangler arrange-act-assert transaction-script + filterer