📍Use lombok, reformat, and optimize the code (#1560)

* Use lombok, reformat, and optimize the code

* Fix merge conflicts and some sonar issues

Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
va1m
2021-03-13 13:19:21 +01:00
committed by GitHub
parent 0e26a6adb5
commit 5cf2fe009b
681 changed files with 2472 additions and 4966 deletions

View File

@ -31,12 +31,9 @@ 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;
import lombok.extern.slf4j.Slf4j;
/**
* This demo class represent how {@link com.iluwatar.filterer.domain.Filterer} pattern is used to
@ -46,10 +43,9 @@ import org.slf4j.LoggerFactory;
* 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.
*/
@Slf4j
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
filteringSimpleThreats();
filteringSimpleProbableThreats();
@ -62,7 +58,7 @@ public class App {
* as predicate argument.
*/
private static void filteringSimpleProbableThreats() {
LOGGER.info(" ### Filtering ProbabilisticThreatAwareSystem by probability ###");
LOGGER.info("### Filtering ProbabilisticThreatAwareSystem by probability ###");
var trojanArcBomb = new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
var rootkit = new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8);
@ -70,14 +66,14 @@ public class App {
List<ProbableThreat> probableThreats = List.of(trojanArcBomb, rootkit);
var probabilisticThreatAwareSystem =
new SimpleProbabilisticThreatAwareSystem("Sys-1", probableThreats);
new SimpleProbabilisticThreatAwareSystem("Sys-1", probableThreats);
LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : "
+ probabilisticThreatAwareSystem);
+ probabilisticThreatAwareSystem);
//Filtering using filterer
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 : " + filteredThreatAwareSystem);
}
@ -100,7 +96,7 @@ public class App {
//Filtering using Filterer
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
.by(threat -> threat.type() == ThreatType.ROOTKIT);
.by(threat -> threat.type() == ThreatType.ROOTKIT);
LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem);
}

View File

@ -23,29 +23,24 @@
package com.iluwatar.filterer.threat;
import com.google.common.collect.ImmutableList;
import com.iluwatar.filterer.domain.Filterer;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
/**
* {@inheritDoc}
*/
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
private final String systemId;
private final ImmutableList<ProbableThreat> threats;
public SimpleProbabilisticThreatAwareSystem(
final String systemId,
final List<ProbableThreat> threats
) {
this.systemId = systemId;
this.threats = ImmutableList.copyOf(threats);
}
private final List<ProbableThreat> threats;
/**
* {@inheritDoc}
@ -72,42 +67,15 @@ public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreat
}
private ProbabilisticThreatAwareSystem filteredGroup(
final Predicate<? super ProbableThreat> predicate
) {
final Predicate<? super ProbableThreat> predicate) {
return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
}
private List<ProbableThreat> filteredItems(
final Predicate<? super ProbableThreat> predicate
) {
final Predicate<? super ProbableThreat> predicate) {
return this.threats.stream()
.filter(predicate)
.collect(Collectors.toList());
.filter(predicate)
.collect(Collectors.toUnmodifiableList());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (SimpleProbabilisticThreatAwareSystem) o;
return systemId.equals(that.systemId)
&& threats.equals(that.threats);
}
@Override
public int hashCode() {
return Objects.hash(systemId, threats);
}
@Override
public String toString() {
return "SimpleProbabilisticThreatAwareSystem{"
+ "systemId='" + systemId + '\''
+ ", threats=" + threats
+ '}';
}
}

View File

@ -23,20 +23,18 @@
package com.iluwatar.filterer.threat;
import java.util.Objects;
import lombok.EqualsAndHashCode;
/**
* {@inheritDoc}
*/
@EqualsAndHashCode(callSuper = false)
public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat {
private final double probability;
public SimpleProbableThreat(final String name,
final int id,
final ThreatType threatType,
final double probability
) {
public SimpleProbableThreat(final String name, final int id, final ThreatType threatType,
final double probability) {
super(threatType, id, name);
this.probability = probability;
}
@ -49,31 +47,11 @@ public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat
return probability;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
var that = (SimpleProbableThreat) o;
return Double.compare(that.probability, probability) == 0;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), probability);
}
@Override
public String toString() {
return "SimpleProbableThreat{"
+ "probability=" + probability
+ "} "
+ super.toString();
+ "probability=" + probability
+ "} "
+ super.toString();
}
}

View File

@ -23,30 +23,22 @@
package com.iluwatar.filterer.threat;
import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
/**
* Represents a simple threat.
*/
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public class SimpleThreat implements Threat {
private final ThreatType threatType;
private final int id;
private final String name;
/**
* 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}
*/
@ -71,31 +63,4 @@ public class SimpleThreat implements Threat {
return threatType;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (SimpleThreat) o;
return id == that.id
&& threatType == that.threatType
&& Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(threatType, id, name);
}
@Override
public String toString() {
return "SimpleThreat{"
+ "threatType=" + threatType
+ ", id=" + id
+ ", name='" + name + '\''
+ '}';
}
}

View File

@ -23,27 +23,25 @@
package com.iluwatar.filterer.threat;
import com.google.common.collect.ImmutableList;
import com.iluwatar.filterer.domain.Filterer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
/**
* {@inheritDoc}
*/
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
private final String systemId;
private final ImmutableList<Threat> issues;
public SimpleThreatAwareSystem(final String systemId, final List<Threat> issues) {
this.systemId = systemId;
this.issues = ImmutableList.copyOf(issues);
}
private final List<Threat> issues;
/**
* {@inheritDoc}
@ -75,33 +73,8 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem {
private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
return this.issues.stream()
.filter(predicate)
.collect(Collectors.toList());
.filter(predicate)
.collect(Collectors.toUnmodifiableList());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (SimpleThreatAwareSystem) o;
return systemId.equals(that.systemId)
&& issues.equals(that.issues);
}
@Override
public int hashCode() {
return Objects.hash(systemId, issues);
}
@Override
public String toString() {
return "SimpleThreatAwareSystem{"
+ "systemId='" + systemId
+ '\'' + ", issues=" + issues
+ '}';
}
}

View File

@ -23,4 +23,8 @@
package com.iluwatar.filterer.threat;
public enum ThreatType { TROJAN, WORM, ROOTKIT }
public enum ThreatType {
TROJAN,
WORM,
ROOTKIT
}

View File

@ -23,13 +23,13 @@
package com.iluwatar.filterer.threat;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
class SimpleProbabilisticThreatAwareSystemTest {
@Test
void shouldFilterByProbability() {
//given
@ -38,11 +38,11 @@ class SimpleProbabilisticThreatAwareSystemTest {
List<ProbableThreat> probableThreats = List.of(trojan, rootkit);
var simpleProbabilisticThreatAwareSystem =
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
//when
var filtered = simpleProbabilisticThreatAwareSystem.filtered()
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
//then
assertEquals(filtered.threats().size(), 1);

View File

@ -41,7 +41,7 @@ class SimpleThreatAwareSystemTest {
//when
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
.by(threat -> threat.type() == ThreatType.ROOTKIT);
.by(threat -> threat.type() == ThreatType.ROOTKIT);
//then
assertEquals(rootkitThreatAwareSystem.threats().size(), 1);