Added fixes after review. Changed example pattern application to threat detection domain
This commit is contained in:
111
filterer/src/main/java/com/iluwatar/filterer/App.java
Normal file
111
filterer/src/main/java/com/iluwatar/filterer/App.java
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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.ProbabilisticThreatAwareSystem;
|
||||
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 ###");
|
||||
|
||||
ProbableThreat trojanArcBomb =
|
||||
new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
|
||||
ProbableThreat rootkit =
|
||||
new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8);
|
||||
|
||||
List<ProbableThreat> probableThreats = List.of(trojanArcBomb, rootkit);
|
||||
|
||||
ProbabilisticThreatAwareSystem probabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||
|
||||
LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : "
|
||||
+ probabilisticThreatAwareSystem);
|
||||
|
||||
//Filtering using filterer
|
||||
ProbabilisticThreatAwareSystem filtered = probabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
LOGGER.info("Filtered by probability = 0.99 : " + filtered);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ###");
|
||||
|
||||
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);
|
||||
|
||||
LOGGER.info("Filtering ThreatAwareSystem. Initial : " + threatAwareSystem);
|
||||
|
||||
//Filtering using Filterer
|
||||
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem);
|
||||
}
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
/*
|
||||
* 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.issue;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents position of an issue. Takes starting and ending offset of issue in given text.
|
||||
*/
|
||||
public final class IssuePosition {
|
||||
|
||||
private final int startOffset;
|
||||
private final int endOffset;
|
||||
|
||||
/**
|
||||
* Factory method for constructing `IssuePosition` instances.
|
||||
* @param startOffset starting offset of where the issue begins.
|
||||
* @param endOffset ending offset of where the issue ends.
|
||||
* @return new IssuePosition instance.
|
||||
*/
|
||||
public static IssuePosition of(final int startOffset, final int endOffset) {
|
||||
return new IssuePosition(startOffset, endOffset);
|
||||
}
|
||||
|
||||
private IssuePosition(int startOffset, int endOffset) {
|
||||
this.startOffset = startOffset;
|
||||
this.endOffset = endOffset;
|
||||
}
|
||||
|
||||
int startOffset() {
|
||||
return startOffset;
|
||||
}
|
||||
|
||||
int endOffset() {
|
||||
return endOffset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
IssuePosition that = (IssuePosition) o;
|
||||
return startOffset == that.startOffset
|
||||
&& endOffset == that.endOffset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(startOffset, endOffset);
|
||||
}
|
||||
}
|
@ -21,29 +21,29 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents text that is aware of it's issues with given probability of their occurrence.
|
||||
* Represents system that is aware of it's threats with given probability of their occurrence.
|
||||
*/
|
||||
public interface ProbabilisticIssueAwareText extends IssueAwareText {
|
||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
List<? extends ProbableIssue> issues();
|
||||
List<? extends ProbableThreat> threats();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
Filterer<? extends ProbabilisticIssueAwareText, ? extends ProbableIssue> filtered();
|
||||
Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
|
||||
}
|
||||
|
@ -21,15 +21,15 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
/**
|
||||
* Represents issue that is an issue with given probability.
|
||||
* Represents threat that might be a threat with given probability.
|
||||
*/
|
||||
public interface ProbableIssue extends Issue {
|
||||
public interface ProbableThreat extends Threat {
|
||||
/**
|
||||
* Returns probability of occurrence of given issue.
|
||||
* @return probability of occurrence of given issue.
|
||||
* Returns probability of occurrence of given threat.
|
||||
* @return probability of occurrence of given threat.
|
||||
*/
|
||||
double probability();
|
||||
}
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
@ -29,56 +29,60 @@ 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 SimpleProbabilisticIssueAwareText implements ProbabilisticIssueAwareText {
|
||||
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
|
||||
|
||||
private final String text;
|
||||
private final ImmutableList<ProbableIssue> issues;
|
||||
private final String systemId;
|
||||
private final ImmutableList<ProbableThreat> threats;
|
||||
|
||||
SimpleProbabilisticIssueAwareText(final String text, final List<ProbableIssue> issues) {
|
||||
this.text = text;
|
||||
this.issues = ImmutableList.copyOf(issues);
|
||||
public SimpleProbabilisticThreatAwareSystem(
|
||||
final String systemId,
|
||||
final List<ProbableThreat> threats
|
||||
) {
|
||||
this.systemId = systemId;
|
||||
this.threats = ImmutableList.copyOf(threats);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String text() {
|
||||
return text;
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<? extends ProbableIssue> issues() {
|
||||
return issues;
|
||||
public List<? extends ProbableThreat> threats() {
|
||||
return threats;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Filterer<? extends ProbabilisticIssueAwareText, ? extends ProbableIssue> filtered() {
|
||||
public Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ProbabilisticIssueAwareText filteredGroup(
|
||||
final Predicate<? super ProbableIssue> predicate
|
||||
private ProbabilisticThreatAwareSystem filteredGroup(
|
||||
final Predicate<? super ProbableThreat> predicate
|
||||
) {
|
||||
return new SimpleProbabilisticIssueAwareText(this.text, filteredItems(predicate));
|
||||
return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private ImmutableList<ProbableIssue> filteredItems(
|
||||
final Predicate<? super ProbableIssue> predicate
|
||||
private List<ProbableThreat> filteredItems(
|
||||
final Predicate<? super ProbableThreat> predicate
|
||||
) {
|
||||
return this.issues.stream()
|
||||
return this.threats.stream()
|
||||
.filter(predicate)
|
||||
.collect(ImmutableList.toImmutableList());
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -89,13 +93,21 @@ public class SimpleProbabilisticIssueAwareText implements ProbabilisticIssueAwar
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
SimpleProbabilisticIssueAwareText that = (SimpleProbabilisticIssueAwareText) o;
|
||||
return text.equals(that.text)
|
||||
&& issues.equals(that.issues);
|
||||
SimpleProbabilisticThreatAwareSystem that = (SimpleProbabilisticThreatAwareSystem) o;
|
||||
return systemId.equals(that.systemId)
|
||||
&& threats.equals(that.threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(text, issues);
|
||||
return Objects.hash(systemId, threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleProbabilisticThreatAwareSystem{"
|
||||
+ "systemId='" + systemId + '\''
|
||||
+ ", threats=" + threats
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -21,22 +21,23 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public class SimpleProbableIssue extends SimpleIssue implements ProbableIssue {
|
||||
public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat {
|
||||
|
||||
private final double probability;
|
||||
|
||||
SimpleProbableIssue(final IssuePosition issuePosition,
|
||||
final IssueType issueType,
|
||||
final double probability
|
||||
public SimpleProbableThreat(final String name,
|
||||
final int id,
|
||||
final ThreatType threatType,
|
||||
final double probability
|
||||
) {
|
||||
super(issuePosition, issueType);
|
||||
super(threatType, id, name);
|
||||
this.probability = probability;
|
||||
}
|
||||
|
||||
@ -59,7 +60,7 @@ public class SimpleProbableIssue extends SimpleIssue implements ProbableIssue {
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
SimpleProbableIssue that = (SimpleProbableIssue) o;
|
||||
SimpleProbableThreat that = (SimpleProbableThreat) o;
|
||||
return Double.compare(that.probability, probability) == 0;
|
||||
}
|
||||
|
||||
@ -67,4 +68,12 @@ public class SimpleProbableIssue extends SimpleIssue implements ProbableIssue {
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), probability);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleProbableThreat{"
|
||||
+ "probability=" + probability
|
||||
+ "} "
|
||||
+ super.toString();
|
||||
}
|
||||
}
|
@ -21,42 +21,54 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class SimpleIssue implements Issue {
|
||||
/**
|
||||
* Represents a simple threat.
|
||||
*/
|
||||
public class SimpleThreat implements Threat {
|
||||
|
||||
private final IssuePosition issuePosition;
|
||||
private final IssueType issueType;
|
||||
private final ThreatType threatType;
|
||||
private final int id;
|
||||
private final String name;
|
||||
|
||||
SimpleIssue(final IssuePosition issuePosition, IssueType issueType) {
|
||||
this.issuePosition = issuePosition;
|
||||
this.issueType = issueType;
|
||||
/**
|
||||
* 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 int startOffset() {
|
||||
return issuePosition.startOffset();
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int endOffset() {
|
||||
return issuePosition.endOffset();
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public IssueType type() {
|
||||
return issueType;
|
||||
public ThreatType type() {
|
||||
return threatType;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -67,13 +79,23 @@ public class SimpleIssue implements Issue {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
SimpleIssue that = (SimpleIssue) o;
|
||||
return issuePosition.equals(that.issuePosition)
|
||||
&& issueType == that.issueType;
|
||||
SimpleThreat that = (SimpleThreat) o;
|
||||
return id == that.id
|
||||
&& threatType == that.threatType
|
||||
&& Objects.equals(name, that.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(issuePosition, issueType);
|
||||
return Objects.hash(threatType, id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleThreat{"
|
||||
+ "threatType=" + threatType
|
||||
+ ", id=" + id
|
||||
+ ", name='" + name + '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
@ -30,17 +30,18 @@ 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 SimpleIssueAwareText implements IssueAwareText {
|
||||
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||
|
||||
private final String text;
|
||||
private final ImmutableList<Issue> issues;
|
||||
private final String systemId;
|
||||
private final ImmutableList<Threat> issues;
|
||||
|
||||
SimpleIssueAwareText(final String text, final List<Issue> issues) {
|
||||
this.text = text;
|
||||
public SimpleThreatAwareSystem(final String systemId, final List<Threat> issues) {
|
||||
this.systemId = systemId;
|
||||
this.issues = ImmutableList.copyOf(issues);
|
||||
}
|
||||
|
||||
@ -48,15 +49,15 @@ public class SimpleIssueAwareText implements IssueAwareText {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String text() {
|
||||
return text;
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<? extends Issue> issues() {
|
||||
public List<? extends Threat> threats() {
|
||||
return new ArrayList<>(issues);
|
||||
}
|
||||
|
||||
@ -64,18 +65,18 @@ public class SimpleIssueAwareText implements IssueAwareText {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Filterer<? extends IssueAwareText, ? extends Issue> filtered() {
|
||||
public Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private IssueAwareText filteredGroup(Predicate<? super Issue> predicate) {
|
||||
return new SimpleIssueAwareText(this.text, filteredItems(predicate));
|
||||
private ThreatAwareSystem filteredGroup(Predicate<? super Threat> predicate) {
|
||||
return new SimpleThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private ImmutableList<Issue> filteredItems(Predicate<? super Issue> predicate) {
|
||||
private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
|
||||
return this.issues.stream()
|
||||
.filter(predicate)
|
||||
.collect(ImmutableList.toImmutableList());
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -86,13 +87,21 @@ public class SimpleIssueAwareText implements IssueAwareText {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
SimpleIssueAwareText that = (SimpleIssueAwareText) o;
|
||||
return text.equals(that.text)
|
||||
SimpleThreatAwareSystem that = (SimpleThreatAwareSystem) o;
|
||||
return systemId.equals(that.systemId)
|
||||
&& issues.equals(that.issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(text, issues);
|
||||
return Objects.hash(systemId, issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleThreatAwareSystem{"
|
||||
+ "systemId='" + systemId
|
||||
+ '\'' + ", issues=" + issues
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -21,29 +21,29 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
/**
|
||||
* Represents an issue that can be detected in given text.
|
||||
* Represents a threat that can be detected in given system.
|
||||
*/
|
||||
public interface Issue {
|
||||
public interface Threat {
|
||||
/**
|
||||
* Returns starting position where the issue begins.
|
||||
* Returns name of the threat.
|
||||
*
|
||||
* @return value representing starting position of the issue.
|
||||
* @return value representing name of the threat.
|
||||
*/
|
||||
int startOffset();
|
||||
String name();
|
||||
|
||||
/**
|
||||
* Returns ending position where the issue ends.
|
||||
* Returns unique id of the threat.
|
||||
*
|
||||
* @return value representing ending position of the issue.
|
||||
* @return value representing threat id.
|
||||
*/
|
||||
int endOffset();
|
||||
int id();
|
||||
|
||||
/**
|
||||
* Returns issue type.
|
||||
* @return {@link IssueType}
|
||||
* Returns threat type.
|
||||
* @return {@link ThreatType}
|
||||
*/
|
||||
IssueType type();
|
||||
ThreatType type();
|
||||
}
|
@ -21,35 +21,35 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents text that is aware of issues that are present in it.
|
||||
* Represents system that is aware of threats that are present in it.
|
||||
*/
|
||||
public interface IssueAwareText {
|
||||
public interface ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* Returns the analyzed text.
|
||||
* Returns the system id.
|
||||
*
|
||||
* @return the analyzed text.
|
||||
* @return system id.
|
||||
*/
|
||||
String text();
|
||||
String systemId();
|
||||
|
||||
/**
|
||||
* Returns list of issues for this text.
|
||||
* @return list of issues for this text.
|
||||
* Returns list of threats for this system.
|
||||
* @return list of threats for this system.
|
||||
*/
|
||||
List<? extends Issue> issues();
|
||||
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 IssueAwareText, ? extends Issue> filtered();
|
||||
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
||||
|
||||
}
|
@ -21,6 +21,6 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
enum IssueType { GRAMMAR, SPELLING }
|
||||
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[]{});
|
||||
}
|
||||
}
|
@ -21,32 +21,31 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SimpleProbabilisticIssueAwareTextTest {
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SimpleProbabilisticThreatAwareSystemTest {
|
||||
@Test
|
||||
void shouldFilterByProbability() {
|
||||
//given
|
||||
ProbableIssue spellingIssue = new SimpleProbableIssue(IssuePosition.of(4, 5), IssueType.SPELLING, 100);
|
||||
ProbableIssue grammarIssue = new SimpleProbableIssue(IssuePosition.of(8, 12), IssueType.GRAMMAR, 99);
|
||||
List<ProbableIssue> issues = List.of(spellingIssue, grammarIssue);
|
||||
ProbableThreat trojan = 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(trojan, rootkit);
|
||||
|
||||
SimpleProbabilisticIssueAwareText simpleIssueWiseText = new SimpleProbabilisticIssueAwareText("I mihgt gone there", issues);
|
||||
ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||
|
||||
//when
|
||||
ProbabilisticIssueAwareText filtered = simpleIssueWiseText.filtered()
|
||||
.by(issue1 -> Double.compare(issue1.probability(), 99) == 0);
|
||||
ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
//then
|
||||
assertThat(filtered.issues()).hasSize(1);
|
||||
assertThat(filtered.issues()).element(0).isEqualTo(grammarIssue);
|
||||
assertEquals(filtered.threats().size(), 1);
|
||||
assertEquals(filtered.threats().get(0), trojan);
|
||||
}
|
||||
|
||||
}
|
@ -21,32 +21,30 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.issue;
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SimpleIssueAwareTextTest {
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SimpleThreatAwareSystemTest {
|
||||
@Test
|
||||
void shouldFilterByStartOffset() {
|
||||
void shouldFilterByThreatType() {
|
||||
//given
|
||||
SimpleIssue spellingIssue = new SimpleIssue(IssuePosition.of(4, 5), IssueType.SPELLING);
|
||||
SimpleIssue grammarIssue = new SimpleIssue(IssuePosition.of(8, 12), IssueType.GRAMMAR);
|
||||
List<Issue> issues = List.of(spellingIssue, grammarIssue);
|
||||
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);
|
||||
|
||||
SimpleIssueAwareText simpleIssueWiseText = new SimpleIssueAwareText("I mihgt gone there", issues);
|
||||
ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
|
||||
|
||||
//when
|
||||
IssueAwareText filtered = simpleIssueWiseText.filtered()
|
||||
.by(issue1 -> issue1.startOffset() == 4);
|
||||
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
//then
|
||||
assertThat(filtered.issues()).hasSize(1);
|
||||
assertThat(filtered.issues()).element(0).isEqualTo(spellingIssue);
|
||||
assertEquals(rootkitThreatAwareSystem.threats().size(), 1);
|
||||
assertEquals(rootkitThreatAwareSystem.threats().get(0), rootkit);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user