Compare commits
19 Commits
all-contri
...
1.23.0
Author | SHA1 | Date | |
---|---|---|---|
687648af0a | |||
96aa21d0e8 | |||
b8f83c326d | |||
15d795bf8a | |||
3754c66604 | |||
6d83ceba28 | |||
015b418114 | |||
0cba307844 | |||
a66edc84a0 | |||
c18282ad5d | |||
7411ea86bf | |||
ef033b66a6 | |||
e65b65257e | |||
3d9afbaeec | |||
61a819aab8 | |||
61b95c294b | |||
4068d1fead | |||
b284230ecf | |||
905b5dc6d8 |
@ -1158,6 +1158,24 @@
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "mkrzywanski",
|
||||
"name": "Michał Krzywański",
|
||||
"avatar_url": "https://avatars0.githubusercontent.com/u/15279585?v=4",
|
||||
"profile": "https://github.com/mkrzywanski",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "stefanbirkner",
|
||||
"name": "Stefan Birkner",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/711349?v=4",
|
||||
"profile": "https://www.stefan-birkner.de",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 4,
|
||||
|
2
.github/workflows/maven-ci.yml
vendored
2
.github/workflows/maven-ci.yml
vendored
@ -60,5 +60,5 @@ jobs:
|
||||
run: xvfb-run mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
|
||||
env:
|
||||
# These two env variables are needed for sonar analysis
|
||||
GITHUB_TOKEN: ${{ secrets.REPOSITORY_ACCESS_TOKEN }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
|
@ -6,10 +6,11 @@
|
||||
|
||||

|
||||
[](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||
[](#contributors-)
|
||||
[](#contributors-)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||
|
||||
# Introduction
|
||||
@ -258,6 +259,10 @@ This project is licensed under the terms of the MIT license.
|
||||
<td align="center"><a href="https://github.com/nahteb"><img src="https://avatars3.githubusercontent.com/u/13121570?v=4" width="100px;" alt=""/><br /><sub><b>Bethan Palmer</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=nahteb" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/ToxicDreamz"><img src="https://avatars0.githubusercontent.com/u/45225562?v=4" width="100px;" alt=""/><br /><sub><b>Toxic Dreamz</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ToxicDreamz" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://www.edycutjong.com"><img src="https://avatars1.githubusercontent.com/u/1098102?v=4" width="100px;" alt=""/><br /><sub><b>Edy Cu Tjong</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=edycutjong" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/mkrzywanski"><img src="https://avatars0.githubusercontent.com/u/15279585?v=4" width="100px;" alt=""/><br /><sub><b>Michał Krzywański</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mkrzywanski" title="Code">💻</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://www.stefan-birkner.de"><img src="https://avatars1.githubusercontent.com/u/711349?v=4" width="100px;" alt=""/><br /><sub><b>Stefan Birkner</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=stefanbirkner" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
@ -9,20 +9,26 @@ tags:
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
Handle/Body
|
||||
|
||||
## Intent
|
||||
|
||||
Decouple an abstraction from its implementation so that the two can vary independently.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> Consider you have a weapon with different enchantments and you are supposed to allow mixing different weapons with different enchantments. What would you do? Create multiple copies of each of the weapons for each of the enchantments or would you just create separate enchantment and set it for the weapon as needed? Bridge pattern allows you to do the second.
|
||||
> Consider you have a weapon with different enchantments, and you are supposed to allow mixing
|
||||
> different weapons with different enchantments. What would you do? Create multiple copies of each
|
||||
> of the weapons for each of the enchantments or would you just create separate enchantment and set
|
||||
> it for the weapon as needed? Bridge pattern allows you to do the second.
|
||||
|
||||
In Plain Words
|
||||
|
||||
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
|
||||
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed
|
||||
> from a hierarchy to another object with a separate hierarchy.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
@ -30,7 +36,7 @@ Wikipedia says
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Translating our weapon example from above. Here we have the `Weapon` hierarchy
|
||||
Translating our weapon example from above. Here we have the `Weapon` hierarchy:
|
||||
|
||||
```java
|
||||
public interface Weapon {
|
||||
@ -105,7 +111,7 @@ public class Hammer implements Weapon {
|
||||
}
|
||||
```
|
||||
|
||||
And the separate enchantment hierarchy
|
||||
Here's the separate enchantment hierarchy:
|
||||
|
||||
```java
|
||||
public interface Enchantment {
|
||||
@ -151,7 +157,7 @@ public class SoulEatingEnchantment implements Enchantment {
|
||||
}
|
||||
```
|
||||
|
||||
And both the hierarchies in action
|
||||
Here are both hierarchies in action:
|
||||
|
||||
```java
|
||||
var enchantedSword = new Sword(new SoulEatingEnchantment());
|
||||
@ -178,18 +184,21 @@ hammer.unwield();
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Bridge pattern when
|
||||
|
||||
* you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
|
||||
* both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently
|
||||
* changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
|
||||
* you have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies
|
||||
* you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
|
||||
* You want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
|
||||
* Both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.
|
||||
* Changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
|
||||
* You have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies.
|
||||
* You want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
|
||||
|
||||
## Tutorial
|
||||
|
||||
* [Bridge Pattern Tutorial](https://www.journaldev.com/1491/bridge-design-pattern-java)
|
||||
|
||||
## Credits
|
||||
|
239
filterer/README.md
Normal file
239
filterer/README.md
Normal file
@ -0,0 +1,239 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Filterer
|
||||
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
|
||||
|
||||
## Intent
|
||||
|
||||
The intent of this design pattern is to introduce a functional interface that will add a
|
||||
functionality for container-like objects to easily return filtered versions of themselves.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> We are designing a threat (malware) detection software which can analyze target systems for
|
||||
> threats that are present in it. In the design we have to take into consideration that new
|
||||
> Threat types can be added later. Additionally, there is a requirement that the threat detection
|
||||
> system can filter the detected threats based on different criteria (the target system acts as
|
||||
> container-like object for threats).
|
||||
|
||||
In plain words
|
||||
|
||||
> Filterer pattern is a design pattern that helps container-like objects return filtered versions
|
||||
> of themselves.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
To model the threat detection example presented above we introduce `Threat` and `ThreatAwareSystem`
|
||||
interfaces.
|
||||
|
||||
```java
|
||||
public interface Threat {
|
||||
String name();
|
||||
int id();
|
||||
ThreatType type();
|
||||
}
|
||||
|
||||
public interface ThreatAwareSystem {
|
||||
String systemId();
|
||||
List<? extends Threat> threats();
|
||||
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Notice the `filtered` method that returns instance of `Filterer` interface which is defined as:
|
||||
|
||||
```java
|
||||
@FunctionalInterface
|
||||
public interface Filterer<G, E> {
|
||||
G by(Predicate<? super E> predicate);
|
||||
}
|
||||
```
|
||||
|
||||
It is used to fulfill the requirement for system to be able to filter itself based on threat
|
||||
properties. The container-like object (`ThreatAwareSystem` in our case) 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.
|
||||
|
||||
In our example we will be able to pass a predicate that takes `? extends Threat` object and
|
||||
return `? extends ThreatAwareSystem` from `Filtered::by` method. A simple implementation
|
||||
of `ThreatAwareSystem`:
|
||||
|
||||
```java
|
||||
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<Threat> issues;
|
||||
|
||||
public SimpleThreatAwareSystem(final String systemId, final List<Threat> issues) {
|
||||
this.systemId = systemId;
|
||||
this.issues = ImmutableList.copyOf(issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Threat> threats() {
|
||||
return new ArrayList<>(issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ThreatAwareSystem filteredGroup(Predicate<? super Threat> predicate) {
|
||||
return new SimpleThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
|
||||
return this.issues.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `filtered` method is overridden to filter the threats list by given predicate.
|
||||
|
||||
Now if we introduce a new subtype of `Threat` interface that adds probability with which given
|
||||
threat can appear:
|
||||
|
||||
```java
|
||||
public interface ProbableThreat extends Threat {
|
||||
double probability();
|
||||
}
|
||||
```
|
||||
|
||||
We can also introduce a new interface that represents a system that is aware of threats with their
|
||||
probabilities:
|
||||
|
||||
````java
|
||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||
@Override
|
||||
List<? extends ProbableThreat> threats();
|
||||
|
||||
@Override
|
||||
Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
|
||||
}
|
||||
````
|
||||
|
||||
Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify
|
||||
different return covariant type by specifying different generic types. Our interfaces are clean and
|
||||
not cluttered by default implementations. We we will be able to filter
|
||||
`ProbabilisticThreatAwareSystem` by `ProbableThreat` properties:
|
||||
|
||||
```java
|
||||
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<ProbableThreat> threats;
|
||||
|
||||
public SimpleProbabilisticThreatAwareSystem(final String systemId, final List<ProbableThreat> threats) {
|
||||
this.systemId = systemId;
|
||||
this.threats = ImmutableList.copyOf(threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends ProbableThreat> threats() {
|
||||
return threats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ProbabilisticThreatAwareSystem filteredGroup(final Predicate<? super ProbableThreat> predicate) {
|
||||
return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<ProbableThreat> filteredItems(final Predicate<? super ProbableThreat> predicate) {
|
||||
return this.threats.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now if we want filter `ThreatAwareSystem` by threat type we can do:
|
||||
|
||||
```java
|
||||
Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
|
||||
Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
|
||||
List<Threat> threats = List.of(rootkit, trojan);
|
||||
|
||||
ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
|
||||
|
||||
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
```
|
||||
|
||||
Or if we want to filter `ProbabilisticThreatAwareSystem`:
|
||||
|
||||
```java
|
||||
ProbableThreat malwareTroyan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
|
||||
ProbableThreat rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8);
|
||||
List<ProbableThreat> probableThreats = List.of(malwareTroyan, rootkit);
|
||||
|
||||
ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem =new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||
|
||||
ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
## 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](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html) link. It presents how
|
||||
to use `Filterer` pattern to create text issue analyzer with support for test cases used for unit
|
||||
testing.
|
||||
|
||||
## Consequences
|
||||
|
||||
Pros:
|
||||
* 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.
|
||||
|
||||
Cons:
|
||||
* Covariant return types mixed with generics can be sometimes tricky
|
||||
|
||||
## Credits
|
||||
|
||||
* Author of the pattern : [Tomasz Linkowski](https://tlinkowski.pl/)
|
BIN
filterer/etc/filterer.png
Normal file
BIN
filterer/etc/filterer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 128 KiB |
96
filterer/etc/filterer.urm.puml
Normal file
96
filterer/etc/filterer.urm.puml
Normal file
@ -0,0 +1,96 @@
|
||||
@startuml
|
||||
package com.iluwatar.filterer.domain {
|
||||
interface Filterer<G, E> {
|
||||
+ by(Predicate<? super E>) : G {abstract}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.filterer {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
- filteringSimpleProbableThreats() {static}
|
||||
- filteringSimpleThreats() {static}
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.filterer.threat {
|
||||
interface ProbabilisticThreatAwareSystem {
|
||||
+ filtered() : Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> {abstract}
|
||||
+ threats() : List<? extends ProbableThreat> {abstract}
|
||||
}
|
||||
interface ProbableThreat {
|
||||
+ probability() : double {abstract}
|
||||
}
|
||||
class SimpleProbabilisticThreatAwareSystem {
|
||||
- systemId : String
|
||||
- threats : ImmutableList<ProbableThreat>
|
||||
+ SimpleProbabilisticThreatAwareSystem(systemId : String, threats : List<ProbableThreat>)
|
||||
+ equals(o : Object) : boolean
|
||||
+ filtered() : Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat>
|
||||
- filteredGroup(predicate : Predicate<? super ProbableThreat>) : ProbabilisticThreatAwareSystem
|
||||
- filteredItems(predicate : Predicate<? super ProbableThreat>) : List<ProbableThreat>
|
||||
+ hashCode() : int
|
||||
+ systemId() : String
|
||||
+ threats() : List<? extends ProbableThreat>
|
||||
+ toString() : String
|
||||
}
|
||||
class SimpleProbableThreat {
|
||||
- probability : double
|
||||
+ SimpleProbableThreat(name : String, id : int, threatType : ThreatType, probability : double)
|
||||
+ equals(o : Object) : boolean
|
||||
+ hashCode() : int
|
||||
+ probability() : double
|
||||
+ toString() : String
|
||||
}
|
||||
class SimpleThreat {
|
||||
- id : int
|
||||
- name : String
|
||||
- threatType : ThreatType
|
||||
+ SimpleThreat(threatType : ThreatType, id : int, name : String)
|
||||
+ id() : int
|
||||
+ name() : String
|
||||
+ toString() : String
|
||||
+ type() : ThreatType
|
||||
}
|
||||
class SimpleThreatAwareSystem {
|
||||
- issues : ImmutableList<Threat>
|
||||
- systemId : String
|
||||
+ SimpleThreatAwareSystem(systemId : String, issues : List<Threat>)
|
||||
+ equals(o : Object) : boolean
|
||||
+ filtered() : Filterer<? extends ThreatAwareSystem, ? extends Threat>
|
||||
- filteredGroup(predicate : Predicate<? super Threat>) : ThreatAwareSystem
|
||||
- filteredItems(predicate : Predicate<? super Threat>) : List<Threat>
|
||||
+ hashCode() : int
|
||||
+ systemId() : String
|
||||
+ threats() : List<? extends Threat>
|
||||
+ toString() : String
|
||||
}
|
||||
interface Threat {
|
||||
+ id() : int {abstract}
|
||||
+ name() : String {abstract}
|
||||
+ type() : ThreatType {abstract}
|
||||
}
|
||||
interface ThreatAwareSystem {
|
||||
+ filtered() : Filterer<? extends ThreatAwareSystem, ? extends Threat> {abstract}
|
||||
+ systemId() : String {abstract}
|
||||
+ threats() : List<? extends Threat> {abstract}
|
||||
}
|
||||
enum ThreatType {
|
||||
+ ROOTKIT {static}
|
||||
+ TROJAN {static}
|
||||
+ WORM {static}
|
||||
+ valueOf(name : String) : ThreatType {static}
|
||||
+ values() : ThreatType[] {static}
|
||||
}
|
||||
}
|
||||
SimpleThreatAwareSystem --> "-issues" Threat
|
||||
SimpleThreat --> "-threatType" ThreatType
|
||||
SimpleProbabilisticThreatAwareSystem --> "-threats" ProbableThreat
|
||||
ProbabilisticThreatAwareSystem --|> ThreatAwareSystem
|
||||
ProbableThreat --|> Threat
|
||||
SimpleProbabilisticThreatAwareSystem ..|> ProbabilisticThreatAwareSystem
|
||||
SimpleProbableThreat ..|> ProbableThreat
|
||||
SimpleProbableThreat --|> SimpleThreat
|
||||
SimpleThreat ..|> Threat
|
||||
SimpleThreatAwareSystem ..|> ThreatAwareSystem
|
||||
@enduml
|
73
filterer/pom.xml
Normal file
73
filterer/pom.xml
Normal file
@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
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.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.23.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>filterer</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.iluwatar.filterer.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
108
filterer/src/main/java/com/iluwatar/filterer/App.java
Normal file
108
filterer/src/main/java/com/iluwatar/filterer/App.java
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.iluwatar.filterer.threat.ProbableThreat;
|
||||
import com.iluwatar.filterer.threat.SimpleProbabilisticThreatAwareSystem;
|
||||
import com.iluwatar.filterer.threat.SimpleProbableThreat;
|
||||
import com.iluwatar.filterer.threat.SimpleThreat;
|
||||
import com.iluwatar.filterer.threat.SimpleThreatAwareSystem;
|
||||
import com.iluwatar.filterer.threat.Threat;
|
||||
import com.iluwatar.filterer.threat.ThreatAwareSystem;
|
||||
import com.iluwatar.filterer.threat.ThreatType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* This demo class represent how {@link com.iluwatar.filterer.domain.Filterer} pattern is used to
|
||||
* filter container-like objects to return filtered versions of themselves. The container like
|
||||
* objects are systems that are aware of threats that they can be vulnerable to. We would like
|
||||
* to have a way to create copy of different system objects but with filtered threats.
|
||||
* The thing is to keep it simple if we add new subtype of {@link Threat}
|
||||
* (for example {@link ProbableThreat}) - we still need to be able to filter by it's properties.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
filteringSimpleThreats();
|
||||
filteringSimpleProbableThreats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrates how to filter {@link com.iluwatar.filterer.threat.ProbabilisticThreatAwareSystem}
|
||||
* based on probability property. The @{@link com.iluwatar.filterer.domain.Filterer#by(Predicate)}
|
||||
* method is able to use {@link com.iluwatar.filterer.threat.ProbableThreat}
|
||||
* as predicate argument.
|
||||
*/
|
||||
private static void filteringSimpleProbableThreats() {
|
||||
LOGGER.info(" ### Filtering ProbabilisticThreatAwareSystem by probability ###");
|
||||
|
||||
var trojanArcBomb = new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
|
||||
var rootkit = new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8);
|
||||
|
||||
List<ProbableThreat> probableThreats = List.of(trojanArcBomb, rootkit);
|
||||
|
||||
var probabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("Sys-1", probableThreats);
|
||||
|
||||
LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : "
|
||||
+ probabilisticThreatAwareSystem);
|
||||
|
||||
//Filtering using filterer
|
||||
var filteredThreatAwareSystem = probabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
LOGGER.info("Filtered by probability = 0.99 : " + filteredThreatAwareSystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrates how to filter {@link ThreatAwareSystem} based on startingOffset property
|
||||
* of {@link SimpleThreat}. The @{@link com.iluwatar.filterer.domain.Filterer#by(Predicate)}
|
||||
* method is able to use {@link Threat} as predicate argument.
|
||||
*/
|
||||
private static void filteringSimpleThreats() {
|
||||
LOGGER.info("### Filtering ThreatAwareSystem by ThreatType ###");
|
||||
|
||||
var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
|
||||
var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
|
||||
List<Threat> threats = List.of(rootkit, trojan);
|
||||
|
||||
var threatAwareSystem = new SimpleThreatAwareSystem("Sys-1", threats);
|
||||
|
||||
LOGGER.info("Filtering ThreatAwareSystem. Initial : " + threatAwareSystem);
|
||||
|
||||
//Filtering using Filterer
|
||||
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem);
|
||||
}
|
||||
|
||||
}
|
@ -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 <G> type of the container-like object.
|
||||
* @param <E> type of the elements contained within this container-like object.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Filterer<G, E> {
|
||||
G by(Predicate<? super E> predicate);
|
||||
}
|
@ -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.threat;
|
||||
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents system that is aware of it's threats with given probability of their occurrence.
|
||||
*/
|
||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
List<? extends ProbableThreat> threats();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
|
||||
}
|
||||
|
@ -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.threat;
|
||||
|
||||
/**
|
||||
* Represents threat that might be a threat with given probability.
|
||||
*/
|
||||
public interface ProbableThreat extends Threat {
|
||||
/**
|
||||
* Returns probability of occurrence of given threat.
|
||||
* @return probability of occurrence of given threat.
|
||||
*/
|
||||
double probability();
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.threat;
|
||||
|
||||
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;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<ProbableThreat> threats;
|
||||
|
||||
public SimpleProbabilisticThreatAwareSystem(
|
||||
final String systemId,
|
||||
final List<ProbableThreat> threats
|
||||
) {
|
||||
this.systemId = systemId;
|
||||
this.threats = ImmutableList.copyOf(threats);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<? extends ProbableThreat> threats() {
|
||||
return threats;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ProbabilisticThreatAwareSystem filteredGroup(
|
||||
final Predicate<? super ProbableThreat> predicate
|
||||
) {
|
||||
return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<ProbableThreat> filteredItems(
|
||||
final Predicate<? super ProbableThreat> predicate
|
||||
) {
|
||||
return this.threats.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
var that = (SimpleProbabilisticThreatAwareSystem) o;
|
||||
return systemId.equals(that.systemId)
|
||||
&& threats.equals(that.threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(systemId, threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleProbabilisticThreatAwareSystem{"
|
||||
+ "systemId='" + systemId + '\''
|
||||
+ ", threats=" + threats
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -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.threat;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat {
|
||||
|
||||
private final double probability;
|
||||
|
||||
public SimpleProbableThreat(final String name,
|
||||
final int id,
|
||||
final ThreatType threatType,
|
||||
final double probability
|
||||
) {
|
||||
super(threatType, id, name);
|
||||
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;
|
||||
}
|
||||
var that = (SimpleProbableThreat) o;
|
||||
return Double.compare(that.probability, probability) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), probability);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleProbableThreat{"
|
||||
+ "probability=" + probability
|
||||
+ "} "
|
||||
+ super.toString();
|
||||
}
|
||||
}
|
@ -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.threat;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a simple threat.
|
||||
*/
|
||||
public class SimpleThreat implements Threat {
|
||||
|
||||
private final ThreatType threatType;
|
||||
private final int id;
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param threatType {@link ThreatType}.
|
||||
* @param id threat id.
|
||||
* @param name threat name.
|
||||
*/
|
||||
public SimpleThreat(final ThreatType threatType, final int id, String name) {
|
||||
this.threatType = threatType;
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public ThreatType type() {
|
||||
return threatType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
var that = (SimpleThreat) o;
|
||||
return id == that.id
|
||||
&& threatType == that.threatType
|
||||
&& Objects.equals(name, that.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(threatType, id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleThreat{"
|
||||
+ "threatType=" + threatType
|
||||
+ ", id=" + id
|
||||
+ ", name='" + name + '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* 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.threat;
|
||||
|
||||
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;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<Threat> issues;
|
||||
|
||||
public SimpleThreatAwareSystem(final String systemId, final List<Threat> issues) {
|
||||
this.systemId = systemId;
|
||||
this.issues = ImmutableList.copyOf(issues);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<? extends Threat> threats() {
|
||||
return new ArrayList<>(issues);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ThreatAwareSystem filteredGroup(Predicate<? super Threat> predicate) {
|
||||
return new SimpleThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
|
||||
return this.issues.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
var that = (SimpleThreatAwareSystem) o;
|
||||
return systemId.equals(that.systemId)
|
||||
&& issues.equals(that.issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(systemId, issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleThreatAwareSystem{"
|
||||
+ "systemId='" + systemId
|
||||
+ '\'' + ", issues=" + issues
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -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.threat;
|
||||
|
||||
/**
|
||||
* Represents a threat that can be detected in given system.
|
||||
*/
|
||||
public interface Threat {
|
||||
/**
|
||||
* Returns name of the threat.
|
||||
*
|
||||
* @return value representing name of the threat.
|
||||
*/
|
||||
String name();
|
||||
|
||||
/**
|
||||
* Returns unique id of the threat.
|
||||
*
|
||||
* @return value representing threat id.
|
||||
*/
|
||||
int id();
|
||||
|
||||
/**
|
||||
* Returns threat type.
|
||||
* @return {@link ThreatType}
|
||||
*/
|
||||
ThreatType type();
|
||||
}
|
@ -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.threat;
|
||||
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents system that is aware of threats that are present in it.
|
||||
*/
|
||||
public interface ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* Returns the system id.
|
||||
*
|
||||
* @return system id.
|
||||
*/
|
||||
String systemId();
|
||||
|
||||
/**
|
||||
* Returns list of threats for this system.
|
||||
* @return list of threats for this system.
|
||||
*/
|
||||
List<? extends Threat> threats();
|
||||
|
||||
/**
|
||||
* 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<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
||||
|
||||
}
|
@ -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.threat;
|
||||
|
||||
public enum ThreatType { TROJAN, WORM, ROOTKIT }
|
33
filterer/src/test/java/com/iluwatar/filterer/AppTest.java
Normal file
33
filterer/src/test/java/com/iluwatar/filterer/AppTest.java
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AppTest {
|
||||
@Test
|
||||
void shouldLaunchApp() {
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.threat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SimpleProbabilisticThreatAwareSystemTest {
|
||||
@Test
|
||||
void shouldFilterByProbability() {
|
||||
//given
|
||||
var trojan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
|
||||
var rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8);
|
||||
List<ProbableThreat> probableThreats = List.of(trojan, rootkit);
|
||||
|
||||
var simpleProbabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||
|
||||
//when
|
||||
var filtered = simpleProbabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
//then
|
||||
assertEquals(filtered.threats().size(), 1);
|
||||
assertEquals(filtered.threats().get(0), trojan);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.threat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SimpleThreatAwareSystemTest {
|
||||
@Test
|
||||
void shouldFilterByThreatType() {
|
||||
//given
|
||||
var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
|
||||
var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
|
||||
List<Threat> threats = List.of(rootkit, trojan);
|
||||
|
||||
var threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
|
||||
|
||||
//when
|
||||
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
//then
|
||||
assertEquals(rootkitThreatAwareSystem.threats().size(), 1);
|
||||
assertEquals(rootkitThreatAwareSystem.threats().get(0), rootkit);
|
||||
}
|
||||
}
|
7
pom.xml
7
pom.xml
@ -52,7 +52,7 @@
|
||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
||||
<jaxb-impl.version>2.3.2</jaxb-impl.version>
|
||||
<annotation-api.version>1.3.2</annotation-api.version>
|
||||
<system-rules.version>1.19.0</system-rules.version>
|
||||
<system-lambda.version>1.1.0</system-lambda.version>
|
||||
<urm.version>2.0.0</urm.version>
|
||||
<mockito-junit-jupiter.version>3.5.0</mockito-junit-jupiter.version>
|
||||
<!-- SonarCloud -->
|
||||
@ -194,6 +194,7 @@
|
||||
<module>strangler</module>
|
||||
<module>arrange-act-assert</module>
|
||||
<module>transaction-script</module>
|
||||
<module>filterer</module>
|
||||
</modules>
|
||||
|
||||
<repositories>
|
||||
@ -337,8 +338,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.stefanbirkner</groupId>
|
||||
<artifactId>system-rules</artifactId>
|
||||
<version>${system-rules.version}</version>
|
||||
<artifactId>system-lambda</artifactId>
|
||||
<version>${system-lambda.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -42,7 +42,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.stefanbirkner</groupId>
|
||||
<artifactId>system-rules</artifactId>
|
||||
<artifactId>system-lambda</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
@ -23,55 +23,48 @@
|
||||
|
||||
package com.iluwatar.subclasssandbox;
|
||||
|
||||
import com.github.stefanbirkner.systemlambda.Statement;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.SystemOutRule;
|
||||
|
||||
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized;
|
||||
|
||||
/**
|
||||
* GroundDive unit tests.
|
||||
*/
|
||||
public class GroundDiveTest {
|
||||
|
||||
@Rule
|
||||
public SystemOutRule log = new SystemOutRule().enableLog();
|
||||
|
||||
@Test
|
||||
public void testMove() {
|
||||
log.clearLog();
|
||||
public void testMove() throws Exception {
|
||||
var groundDive = new GroundDive();
|
||||
groundDive.move(1.0, 1.0, 1.0);
|
||||
var outputLog = getLogContent(log.getLog());
|
||||
var outputLog = getLogContent(() -> groundDive.move(1.0, 1.0, 1.0));
|
||||
var expectedLog = "Move to ( 1.0, 1.0, 1.0 )";
|
||||
Assert.assertEquals(outputLog, expectedLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlaySound() {
|
||||
log.clearLog();
|
||||
public void testPlaySound() throws Exception {
|
||||
var groundDive = new GroundDive();
|
||||
groundDive.playSound("SOUND_NAME", 1);
|
||||
var outputLog = getLogContent(log.getLog());
|
||||
var outputLog = getLogContent(() -> groundDive.playSound("SOUND_NAME", 1));
|
||||
var expectedLog = "Play SOUND_NAME with volumn 1";
|
||||
Assert.assertEquals(outputLog, expectedLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpawnParticles() {
|
||||
log.clearLog();
|
||||
public void testSpawnParticles() throws Exception {
|
||||
var groundDive = new GroundDive();
|
||||
groundDive.spawnParticles("PARTICLE_TYPE", 100);
|
||||
final var outputLog = getLogContent(log.getLog());
|
||||
final var outputLog = getLogContent(
|
||||
() -> groundDive.spawnParticles("PARTICLE_TYPE", 100));
|
||||
final var expectedLog = "Spawn 100 particle with type PARTICLE_TYPE";
|
||||
Assert.assertEquals(outputLog, expectedLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivate() {
|
||||
log.clearLog();
|
||||
public void testActivate() throws Exception {
|
||||
var groundDive = new GroundDive();
|
||||
groundDive.activate();
|
||||
var logs = log.getLog().split("\n");
|
||||
var logs = tapSystemOutNormalized(groundDive::activate)
|
||||
.split("\n");
|
||||
final var expectedSize = 3;
|
||||
final var log1 = logs[0].split("-")[1].trim() + " -" + logs[0].split("-")[2].trim();
|
||||
final var expectedLog1 = "Move to ( 0.0, 0.0, -20.0 )";
|
||||
@ -85,6 +78,11 @@ public class GroundDiveTest {
|
||||
Assert.assertEquals(log3, expectedLog3);
|
||||
}
|
||||
|
||||
private String getLogContent(Statement statement) throws Exception {
|
||||
var log = tapSystemOutNormalized(statement);
|
||||
return getLogContent(log);
|
||||
}
|
||||
|
||||
private String getLogContent(String log) {
|
||||
return log.split("-")[1].trim();
|
||||
}
|
||||
|
@ -23,55 +23,47 @@
|
||||
|
||||
package com.iluwatar.subclasssandbox;
|
||||
|
||||
import com.github.stefanbirkner.systemlambda.Statement;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.SystemOutRule;
|
||||
|
||||
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized;
|
||||
|
||||
/**
|
||||
* SkyLaunch unit tests.
|
||||
*/
|
||||
public class SkyLaunchTest {
|
||||
|
||||
@Rule
|
||||
public SystemOutRule log = new SystemOutRule().enableLog();
|
||||
|
||||
@Test
|
||||
public void testMove() {
|
||||
log.clearLog();
|
||||
public void testMove() throws Exception {
|
||||
var skyLaunch = new SkyLaunch();
|
||||
skyLaunch.move(1.0, 1.0, 1.0);
|
||||
var outputLog = getLogContent(log.getLog());
|
||||
var outputLog = getLogContent(() -> skyLaunch.move(1.0, 1.0, 1.0));
|
||||
var expectedLog = "Move to ( 1.0, 1.0, 1.0 )";
|
||||
Assert.assertEquals(outputLog, expectedLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlaySound() {
|
||||
log.clearLog();
|
||||
public void testPlaySound() throws Exception {
|
||||
var skyLaunch = new SkyLaunch();
|
||||
skyLaunch.playSound("SOUND_NAME", 1);
|
||||
var outputLog = getLogContent(log.getLog());
|
||||
var outputLog = getLogContent(() -> skyLaunch.playSound("SOUND_NAME", 1));
|
||||
var expectedLog = "Play SOUND_NAME with volumn 1";
|
||||
Assert.assertEquals(outputLog, expectedLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpawnParticles() {
|
||||
log.clearLog();
|
||||
public void testSpawnParticles() throws Exception {
|
||||
var skyLaunch = new SkyLaunch();
|
||||
skyLaunch.spawnParticles("PARTICLE_TYPE", 100);
|
||||
var outputLog = getLogContent(log.getLog());
|
||||
var outputLog = getLogContent(
|
||||
() -> skyLaunch.spawnParticles("PARTICLE_TYPE", 100));
|
||||
var expectedLog = "Spawn 100 particle with type PARTICLE_TYPE";
|
||||
Assert.assertEquals(outputLog, expectedLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivate() {
|
||||
log.clearLog();
|
||||
public void testActivate() throws Exception {
|
||||
var skyLaunch = new SkyLaunch();
|
||||
skyLaunch.activate();
|
||||
var logs = log.getLog().split("\n");
|
||||
var logs = tapSystemOutNormalized(skyLaunch::activate)
|
||||
.split("\n");
|
||||
final var expectedSize = 3;
|
||||
final var log1 = getLogContent(logs[0]);
|
||||
final var expectedLog1 = "Move to ( 0.0, 0.0, 20.0 )";
|
||||
@ -85,6 +77,11 @@ public class SkyLaunchTest {
|
||||
Assert.assertEquals(log3, expectedLog3);
|
||||
}
|
||||
|
||||
private String getLogContent(Statement statement) throws Exception {
|
||||
var log = tapSystemOutNormalized(statement);
|
||||
return getLogContent(log);
|
||||
}
|
||||
|
||||
private String getLogContent(String log) {
|
||||
return log.split("-")[1].trim();
|
||||
}
|
||||
|
Reference in New Issue
Block a user