Skip to content

Commit

Permalink
fixing typos in readme file, introducing var local type inference whe…
Browse files Browse the repository at this point in the history
…re possible
  • Loading branch information
mkrzywanski committed Aug 23, 2020
1 parent 3d9afba commit e65b652
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 deletions.
8 changes: 4 additions & 4 deletions filterer/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down
23 changes: 10 additions & 13 deletions filterer/src/main/java/com/iluwatar/filterer/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,24 +64,22 @@ public static void main(String[] args) {
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);
}

/**
Expand All @@ -93,16 +90,16 @@ private static void filteringSimpleProbableThreats() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public boolean equals(Object o) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean equals(Object o) {
if (!super.equals(o)) {
return false;
}
SimpleProbableThreat that = (SimpleProbableThreat) o;
var that = (SimpleProbableThreat) o;
return Double.compare(that.probability, probability) == 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean equals(Object o) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public boolean equals(Object o) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e65b652

Please sign in to comment.