fixing typos in readme file, introducing var local type inference where possible
This commit is contained in:
		@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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<ProbableThreat> 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<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);
 | 
			
		||||
 | 
			
		||||
    //Filtering using Filterer
 | 
			
		||||
    ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
 | 
			
		||||
    var rootkitThreatAwareSystem = threatAwareSystem.filtered()
 | 
			
		||||
            .by(threat -> threat.type() == ThreatType.ROOTKIT);
 | 
			
		||||
 | 
			
		||||
    LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem);
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -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<ProbableThreat> 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
 | 
			
		||||
 
 | 
			
		||||
@@ -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<Threat> 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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user