提交 e65b6525 编写于 作者: M Michal Krzywanski

fixing typos in readme file, introducing var local type inference where possible

上级 3d9afbae
...@@ -14,7 +14,7 @@ tags: ...@@ -14,7 +14,7 @@ tags:
Filterer Filterer
## Intent ## 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. 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 ## Explanation
Real world example Real world example
...@@ -59,7 +59,7 @@ The container-like object (`ThreatAwareSystem` in our case) needs to have a meth ...@@ -59,7 +59,7 @@ The container-like object (`ThreatAwareSystem` in our case) needs to have a meth
ability to covariantly specify a lower bound of contravariant `Predicate` in the subinterfaces of interfaces representing the container-like objects. 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` 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 `ThreadAwareSystem` : from `Filtered::by` method. A simple implementation of `ThreatAwareSystem` :
```java ```java
public class SimpleThreatAwareSystem implements ThreatAwareSystem { public class SimpleThreatAwareSystem implements ThreatAwareSystem {
...@@ -99,7 +99,7 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem { ...@@ -99,7 +99,7 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem {
``` ```
the `filtered` method is overridden to filter the threats list by given predicate. the `filtered` method is overridden to filter the threats list by given predicate.
Now if we introduce new subtype of `Thread` interface that adds probability with which given thread can appear : Now if we introduce a new subtype of `Threat` interface that adds probability with which given threat can appear :
```java ```java
public interface ProbableThreat extends Threat { public interface ProbableThreat extends Threat {
double probability(); double probability();
...@@ -116,7 +116,7 @@ public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem { ...@@ -116,7 +116,7 @@ public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
} }
```` ````
Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify different return covariant type Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify different return covariant type
by specifing different generic types. Our interfaces are clean and not cluttered by default implementations. We 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 : we will be able to filter `ProbabilisticThreatAwareSystem` by `ProbableThreat` properties :
```java ```java
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem { public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
package com.iluwatar.filterer; package com.iluwatar.filterer;
import com.iluwatar.filterer.threat.ProbabilisticThreatAwareSystem;
import com.iluwatar.filterer.threat.ProbableThreat; import com.iluwatar.filterer.threat.ProbableThreat;
import com.iluwatar.filterer.threat.SimpleProbabilisticThreatAwareSystem; import com.iluwatar.filterer.threat.SimpleProbabilisticThreatAwareSystem;
import com.iluwatar.filterer.threat.SimpleProbableThreat; import com.iluwatar.filterer.threat.SimpleProbableThreat;
...@@ -65,24 +64,22 @@ public class App { ...@@ -65,24 +64,22 @@ public class App {
private static void filteringSimpleProbableThreats() { private static void filteringSimpleProbableThreats() {
LOGGER.info(" ### Filtering ProbabilisticThreatAwareSystem by probability ###"); LOGGER.info(" ### Filtering ProbabilisticThreatAwareSystem by probability ###");
ProbableThreat trojanArcBomb = var trojanArcBomb = new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99); var rootkit = new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8);
ProbableThreat rootkit =
new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8);
List<ProbableThreat> probableThreats = List.of(trojanArcBomb, rootkit); List<ProbableThreat> probableThreats = List.of(trojanArcBomb, rootkit);
ProbabilisticThreatAwareSystem probabilisticThreatAwareSystem = var probabilisticThreatAwareSystem =
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats); new SimpleProbabilisticThreatAwareSystem("Sys-1", probableThreats);
LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : " LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : "
+ probabilisticThreatAwareSystem); + probabilisticThreatAwareSystem);
//Filtering using filterer //Filtering using filterer
ProbabilisticThreatAwareSystem filtered = probabilisticThreatAwareSystem.filtered() var filteredThreatAwareSystem = probabilisticThreatAwareSystem.filtered()
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0); .by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
LOGGER.info("Filtered by probability = 0.99 : " + filtered); LOGGER.info("Filtered by probability = 0.99 : " + filteredThreatAwareSystem);
} }
/** /**
...@@ -93,16 +90,16 @@ public class App { ...@@ -93,16 +90,16 @@ public class App {
private static void filteringSimpleThreats() { private static void filteringSimpleThreats() {
LOGGER.info("### Filtering ThreatAwareSystem by ThreatType ###"); LOGGER.info("### Filtering ThreatAwareSystem by ThreatType ###");
Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit"); var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan"); var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
List<Threat> threats = List.of(rootkit, trojan); List<Threat> threats = List.of(rootkit, trojan);
ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats); var threatAwareSystem = new SimpleThreatAwareSystem("Sys-1", threats);
LOGGER.info("Filtering ThreatAwareSystem. Initial : " + threatAwareSystem); LOGGER.info("Filtering ThreatAwareSystem. Initial : " + threatAwareSystem);
//Filtering using Filterer //Filtering using Filterer
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered() var rootkitThreatAwareSystem = threatAwareSystem.filtered()
.by(threat -> threat.type() == ThreatType.ROOTKIT); .by(threat -> threat.type() == ThreatType.ROOTKIT);
LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem); LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem);
......
...@@ -93,7 +93,7 @@ public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreat ...@@ -93,7 +93,7 @@ public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreat
if (o == null || getClass() != o.getClass()) { if (o == null || getClass() != o.getClass()) {
return false; return false;
} }
SimpleProbabilisticThreatAwareSystem that = (SimpleProbabilisticThreatAwareSystem) o; var that = (SimpleProbabilisticThreatAwareSystem) o;
return systemId.equals(that.systemId) return systemId.equals(that.systemId)
&& threats.equals(that.threats); && threats.equals(that.threats);
} }
......
...@@ -60,7 +60,7 @@ public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat ...@@ -60,7 +60,7 @@ public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat
if (!super.equals(o)) { if (!super.equals(o)) {
return false; return false;
} }
SimpleProbableThreat that = (SimpleProbableThreat) o; var that = (SimpleProbableThreat) o;
return Double.compare(that.probability, probability) == 0; return Double.compare(that.probability, probability) == 0;
} }
......
...@@ -79,7 +79,7 @@ public class SimpleThreat implements Threat { ...@@ -79,7 +79,7 @@ public class SimpleThreat implements Threat {
if (o == null || getClass() != o.getClass()) { if (o == null || getClass() != o.getClass()) {
return false; return false;
} }
SimpleThreat that = (SimpleThreat) o; var that = (SimpleThreat) o;
return id == that.id return id == that.id
&& threatType == that.threatType && threatType == that.threatType
&& Objects.equals(name, that.name); && Objects.equals(name, that.name);
......
...@@ -87,7 +87,7 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem { ...@@ -87,7 +87,7 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem {
if (o == null || getClass() != o.getClass()) { if (o == null || getClass() != o.getClass()) {
return false; return false;
} }
SimpleThreatAwareSystem that = (SimpleThreatAwareSystem) o; var that = (SimpleThreatAwareSystem) o;
return systemId.equals(that.systemId) return systemId.equals(that.systemId)
&& issues.equals(that.issues); && issues.equals(that.issues);
} }
......
...@@ -33,15 +33,15 @@ class SimpleProbabilisticThreatAwareSystemTest { ...@@ -33,15 +33,15 @@ class SimpleProbabilisticThreatAwareSystemTest {
@Test @Test
void shouldFilterByProbability() { void shouldFilterByProbability() {
//given //given
ProbableThreat trojan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99); var trojan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
ProbableThreat rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8); var rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8);
List<ProbableThreat> probableThreats = List.of(trojan, rootkit); List<ProbableThreat> probableThreats = List.of(trojan, rootkit);
ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem = var simpleProbabilisticThreatAwareSystem =
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats); new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
//when //when
ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered() var filtered = simpleProbabilisticThreatAwareSystem.filtered()
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0); .by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
//then //then
......
...@@ -33,14 +33,14 @@ class SimpleThreatAwareSystemTest { ...@@ -33,14 +33,14 @@ class SimpleThreatAwareSystemTest {
@Test @Test
void shouldFilterByThreatType() { void shouldFilterByThreatType() {
//given //given
Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit"); var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan"); var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
List<Threat> threats = List.of(rootkit, trojan); List<Threat> threats = List.of(rootkit, trojan);
ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats); var threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
//when //when
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered() var rootkitThreatAwareSystem = threatAwareSystem.filtered()
.by(threat -> threat.type() == ThreatType.ROOTKIT); .by(threat -> threat.type() == ThreatType.ROOTKIT);
//then //then
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册