Skip to content

Commit 905b5dc

Browse files
committed
Implemented filterer pattern
1 parent 8475853 commit 905b5dc

18 files changed

Lines changed: 1032 additions & 0 deletions

‎filterer/README.MD‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--- # this is so called 'Yaml Front Matter', read up on it here: http://jekyllrb.com/docs/frontmatter/
2+
layout: pattern
3+
title: Filterer Pattern
4+
folder: filterer
5+
permalink: /patterns/filterer/
6+
description: Design pattern that helps container-like objects to return filtered version of themselves.# short meta description that shows in Google search results
7+
categories:
8+
- Functional
9+
tags:
10+
- Extensibility
11+
---
12+
13+
## Name / classification
14+
Filterer Pattern
15+
16+
## Intent
17+
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.
18+
19+
## Explanation
20+
The container-like object needs to have a method that returns an instance of `Filterer`. This helper interface gives
21+
ability to covariantly specify a lower bound of contravariant `Predicate` in the subinterfaces of interfaces representing the container-like objects.
22+
23+
## Class diagram
24+
![Filterer](./etc/filterer.png "Filterer")
25+
26+
## Applicability
27+
Pattern can be used when working with container-like objects that use subtyping, instead of parametrizing(generics) for extensible class structure.
28+
It enables you to easily extend filtering ability of container-like objects as business requirements change.
29+
30+
## Tutorials
31+
* [Article about Filterer pattern posted on it's author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/)
32+
* [Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
33+
34+
## Known uses
35+
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.
36+
37+
## Consequences (the good and the bad, add criticism here)
38+
Good :
39+
* 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.
40+
41+
Bad :
42+
* covariant return types mixed with generics can be sometimes tricky
43+
44+
## Credits
45+
* Author of the pattern : [Tomasz Linkowski](https://tlinkowski.pl/)

‎filterer/etc/filterer.png‎

138 KB
Loading

‎filterer/etc/filterer.urm.puml‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
@startuml
2+
package com.iluwatar.filterer.domain {
3+
interface Filterer<G, E> {
4+
+ by(Predicate<? super E>) : G {abstract}
5+
}
6+
}
7+
package com.iluwatar.filterer.issue {
8+
interface Issue {
9+
+ endOffset() : int {abstract}
10+
+ startOffset() : int {abstract}
11+
+ type() : IssueType {abstract}
12+
}
13+
interface IssueAwareText {
14+
+ filtered() : Filterer<? extends IssueAwareText, ? extends Issue> {abstract}
15+
+ issues() : List<? extends Issue> {abstract}
16+
+ text() : String {abstract}
17+
}
18+
class IssuePosition {
19+
- endOffset : int
20+
- startOffset : int
21+
- IssuePosition(startOffset : int, endOffset : int)
22+
~ endOffset() : int
23+
+ equals(o : Object) : boolean
24+
+ hashCode() : int
25+
+ of(startOffset : int, endOffset : int) : IssuePosition {static}
26+
~ startOffset() : int
27+
}
28+
~enum IssueType {
29+
+ GRAMMAR {static}
30+
+ SPELLING {static}
31+
+ valueOf(name : String) : IssueType {static}
32+
+ values() : IssueType[] {static}
33+
}
34+
interface IssueWiseText {
35+
+ filtered() : Filterer<? extends IssueWiseText, ? extends Issue> {abstract}
36+
+ issues() : List<? extends Issue> {abstract}
37+
+ text() : String {abstract}
38+
}
39+
interface ProbabilisticIssueAwareText {
40+
+ filtered() : Filterer<? extends ProbabilisticIssueAwareText, ? extends ProbableIssue> {abstract}
41+
+ issues() : List<? extends ProbableIssue> {abstract}
42+
}
43+
interface ProbabilisticIssueWiseText {
44+
+ filtered() : Filterer<? extends ProbabilisticIssueWiseText, ? extends ProbableIssue> {abstract}
45+
+ issues() : List<? extends ProbableIssue> {abstract}
46+
}
47+
interface ProbableIssue {
48+
+ probability() : double {abstract}
49+
}
50+
class SimpleIssue {
51+
- issuePosition : IssuePosition
52+
- issueType : IssueType
53+
~ SimpleIssue(issuePosition : IssuePosition, issueType : IssueType)
54+
+ endOffset() : int
55+
+ equals(o : Object) : boolean
56+
+ hashCode() : int
57+
+ startOffset() : int
58+
+ type() : IssueType
59+
}
60+
class SimpleIssueAwareText {
61+
- issues : ImmutableList<Issue>
62+
- text : String
63+
~ SimpleIssueAwareText(text : String, issues : List<Issue>)
64+
+ equals(o : Object) : boolean
65+
+ filtered() : Filterer<? extends IssueAwareText, ? extends Issue>
66+
- filteredGroup(predicate : Predicate<? super Issue>) : IssueAwareText
67+
- filteredItems(predicate : Predicate<? super Issue>) : ImmutableList<Issue>
68+
+ hashCode() : int
69+
+ issues() : List<? extends Issue>
70+
+ text() : String
71+
}
72+
class SimpleIssueWiseText {
73+
- issues : ImmutableList<Issue>
74+
- text : String
75+
+ SimpleIssueWiseText(text : String, issues : List<Issue>)
76+
+ equals(o : Object) : boolean
77+
+ filtered() : Filterer<? extends IssueWiseText, ? extends Issue>
78+
- filteredGroup(predicate : Predicate<? super Issue>) : IssueWiseText
79+
- filteredItems(predicate : Predicate<? super Issue>) : ImmutableList<Issue>
80+
+ hashCode() : int
81+
+ issues() : List<? extends Issue>
82+
+ text() : String
83+
}
84+
class SimpleProbabilisticIssueAwareText {
85+
- issues : ImmutableList<ProbableIssue>
86+
- text : String
87+
~ SimpleProbabilisticIssueAwareText(text : String, issues : List<ProbableIssue>)
88+
+ equals(o : Object) : boolean
89+
+ filtered() : Filterer<? extends ProbabilisticIssueAwareText, ? extends ProbableIssue>
90+
- filteredGroup(predicate : Predicate<? super ProbableIssue>) : ProbabilisticIssueAwareText
91+
- filteredItems(predicate : Predicate<? super ProbableIssue>) : ImmutableList<ProbableIssue>
92+
+ hashCode() : int
93+
+ issues() : List<? extends ProbableIssue>
94+
+ text() : String
95+
}
96+
class SimpleProbabilisticIssueWiseText {
97+
- issues : ImmutableList<ProbableIssue>
98+
- text : String
99+
+ SimpleProbabilisticIssueWiseText(text : String, issues : List<ProbableIssue>)
100+
+ equals(o : Object) : boolean
101+
+ filtered() : Filterer<? extends ProbabilisticIssueWiseText, ? extends ProbableIssue>
102+
- filteredGroup(predicate : Predicate<? super ProbableIssue>) : ProbabilisticIssueWiseText
103+
- filteredItems(predicate : Predicate<? super ProbableIssue>) : ImmutableList<ProbableIssue>
104+
+ hashCode() : int
105+
+ issues() : List<? extends ProbableIssue>
106+
+ text() : String
107+
}
108+
class SimpleProbableIssue {
109+
- probability : double
110+
~ SimpleProbableIssue(issuePosition : IssuePosition, issueType : IssueType, probability : double)
111+
+ equals(o : Object) : boolean
112+
+ hashCode() : int
113+
+ probability() : double
114+
}
115+
}
116+
SimpleIssueWiseText --> "-issues" Issue
117+
SimpleProbabilisticIssueAwareText --> "-issues" ProbableIssue
118+
SimpleIssue --> "-issueType" IssueType
119+
SimpleIssueAwareText --> "-issues" Issue
120+
SimpleProbabilisticIssueWiseText --> "-issues" ProbableIssue
121+
SimpleIssue --> "-issuePosition" IssuePosition
122+
ProbabilisticIssueAwareText --|> IssueAwareText
123+
ProbabilisticIssueWiseText --|> IssueWiseText
124+
ProbableIssue --|> Issue
125+
SimpleIssue ..|> Issue
126+
SimpleIssueAwareText ..|> IssueAwareText
127+
SimpleIssueWiseText ..|> IssueWiseText
128+
SimpleProbabilisticIssueAwareText ..|> ProbabilisticIssueAwareText
129+
SimpleProbabilisticIssueWiseText ..|> ProbabilisticIssueWiseText
130+
SimpleProbableIssue ..|> ProbableIssue
131+
SimpleProbableIssue --|> SimpleIssue
132+
@enduml

‎filterer/pom.xml‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.23.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>filterer</artifactId>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.guava</groupId>
41+
<artifactId>guava</artifactId>
42+
<version>29.0-jre</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.junit.jupiter</groupId>
46+
<artifactId>junit-jupiter-api</artifactId>
47+
<version>5.6.2</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter-engine</artifactId>
53+
<version>5.6.2</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.assertj</groupId>
58+
<artifactId>assertj-core</artifactId>
59+
<version>3.16.1</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<artifactId>maven-surefire-plugin</artifactId>
68+
<version>2.22.2</version>
69+
</plugin>
70+
<plugin>
71+
<artifactId>maven-failsafe-plugin</artifactId>
72+
<version>2.22.2</version>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.filterer.domain;
25+
26+
import java.util.function.Predicate;
27+
28+
/**
29+
* Filterer helper interface.
30+
* @param <G> type of the container-like object.
31+
* @param <E> type of the elements contained within this container-like object.
32+
*/
33+
@FunctionalInterface
34+
public interface Filterer<G, E> {
35+
G by(Predicate<? super E> predicate);
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.filterer.issue;
25+
26+
/**
27+
* Represents an issue that can be detected in given text.
28+
*/
29+
public interface Issue {
30+
/**
31+
* Returns starting position where the issue begins.
32+
*
33+
* @return value representing starting position of the issue.
34+
*/
35+
int startOffset();
36+
37+
/**
38+
* Returns ending position where the issue ends.
39+
*
40+
* @return value representing ending position of the issue.
41+
*/
42+
int endOffset();
43+
44+
/**
45+
* Returns issue type.
46+
* @return {@link IssueType}
47+
*/
48+
IssueType type();
49+
}

0 commit comments

Comments
 (0)