diff --git a/filterer/README.MD b/filterer/README.MD index b8207af6f..0c03e21a9 100644 --- a/filterer/README.MD +++ b/filterer/README.MD @@ -14,7 +14,7 @@ tags: Filterer ## 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 Real world example @@ -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. 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 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. -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 public interface ProbableThreat extends Threat { double probability(); @@ -116,7 +116,7 @@ public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem { } ```` 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 : ```java public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem { diff --git a/filterer/src/main/java/com/iluwatar/filterer/App.java b/filterer/src/main/java/com/iluwatar/filterer/App.java index 97ed24837..43de5a646 100644 --- a/filterer/src/main/java/com/iluwatar/filterer/App.java +++ b/filterer/src/main/java/com/iluwatar/filterer/App.java @@ -23,7 +23,6 @@ package com.iluwatar.filterer; -import com.iluwatar.filterer.threat.ProbabilisticThreatAwareSystem; import com.iluwatar.filterer.threat.ProbableThreat; import com.iluwatar.filterer.threat.SimpleProbabilisticThreatAwareSystem; import com.iluwatar.filterer.threat.SimpleProbableThreat; @@ -65,24 +64,22 @@ public class App { private static void filteringSimpleProbableThreats() { LOGGER.info(" ### Filtering ProbabilisticThreatAwareSystem by probability ###"); - ProbableThreat trojanArcBomb = - new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99); - ProbableThreat rootkit = - new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8); + var trojanArcBomb = new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99); + var rootkit = new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8); List probableThreats = List.of(trojanArcBomb, rootkit); - ProbabilisticThreatAwareSystem probabilisticThreatAwareSystem = - new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats); + var probabilisticThreatAwareSystem = + new SimpleProbabilisticThreatAwareSystem("Sys-1", probableThreats); LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : " + probabilisticThreatAwareSystem); //Filtering using filterer - ProbabilisticThreatAwareSystem filtered = probabilisticThreatAwareSystem.filtered() + var filteredThreatAwareSystem = probabilisticThreatAwareSystem.filtered() .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 { private static void filteringSimpleThreats() { LOGGER.info("### Filtering ThreatAwareSystem by ThreatType ###"); - Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit"); - Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan"); + var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit"); + var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan"); List 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); //Filtering using Filterer - ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered() + var rootkitThreatAwareSystem = threatAwareSystem.filtered() .by(threat -> threat.type() == ThreatType.ROOTKIT); LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem); diff --git a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystem.java b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystem.java index 37fae6a74..3991d975e 100644 --- a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystem.java +++ b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystem.java @@ -93,7 +93,7 @@ public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreat if (o == null || getClass() != o.getClass()) { return false; } - SimpleProbabilisticThreatAwareSystem that = (SimpleProbabilisticThreatAwareSystem) o; + var that = (SimpleProbabilisticThreatAwareSystem) o; return systemId.equals(that.systemId) && threats.equals(that.threats); } diff --git a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbableThreat.java b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbableThreat.java index 27efc740a..54da07873 100644 --- a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbableThreat.java +++ b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleProbableThreat.java @@ -60,7 +60,7 @@ public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat if (!super.equals(o)) { return false; } - SimpleProbableThreat that = (SimpleProbableThreat) o; + var that = (SimpleProbableThreat) o; return Double.compare(that.probability, probability) == 0; } diff --git a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreat.java b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreat.java index fe7f155d4..08a8b0e17 100644 --- a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreat.java +++ b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreat.java @@ -79,7 +79,7 @@ public class SimpleThreat implements Threat { if (o == null || getClass() != o.getClass()) { return false; } - SimpleThreat that = (SimpleThreat) o; + var that = (SimpleThreat) o; return id == that.id && threatType == that.threatType && Objects.equals(name, that.name); diff --git a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystem.java b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystem.java index 49707cf50..f1dec40ae 100644 --- a/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystem.java +++ b/filterer/src/main/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystem.java @@ -87,7 +87,7 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem { if (o == null || getClass() != o.getClass()) { return false; } - SimpleThreatAwareSystem that = (SimpleThreatAwareSystem) o; + var that = (SimpleThreatAwareSystem) o; return systemId.equals(that.systemId) && issues.equals(that.issues); } diff --git a/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystemTest.java b/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystemTest.java index 592fabe88..2f14ca057 100644 --- a/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystemTest.java +++ b/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleProbabilisticThreatAwareSystemTest.java @@ -33,15 +33,15 @@ class SimpleProbabilisticThreatAwareSystemTest { @Test void shouldFilterByProbability() { //given - ProbableThreat trojan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99); - ProbableThreat rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8); + var trojan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99); + var rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8); List probableThreats = List.of(trojan, rootkit); - ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem = + var simpleProbabilisticThreatAwareSystem = new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats); //when - ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered() + var filtered = simpleProbabilisticThreatAwareSystem.filtered() .by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0); //then diff --git a/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystemTest.java b/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystemTest.java index 91fb62718..ea918c9ec 100644 --- a/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystemTest.java +++ b/filterer/src/test/java/com/iluwatar/filterer/threat/SimpleThreatAwareSystemTest.java @@ -33,14 +33,14 @@ class SimpleThreatAwareSystemTest { @Test void shouldFilterByThreatType() { //given - Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit"); - Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan"); + var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit"); + var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan"); List threats = List.of(rootkit, trojan); - ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats); + var threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats); //when - ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered() + var rootkitThreatAwareSystem = threatAwareSystem.filtered() .by(threat -> threat.type() == ThreatType.ROOTKIT); //then