mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-05 18:24:34 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -39,10 +39,10 @@ import lombok.extern.slf4j.Slf4j;
|
||||
/**
|
||||
* 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 its properties.
|
||||
* 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 its properties.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -55,8 +55,8 @@ public class App {
|
||||
/**
|
||||
* 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.
|
||||
* method is able to use {@link com.iluwatar.filterer.threat.ProbableThreat} as predicate
|
||||
* argument.
|
||||
*/
|
||||
private static void filteringSimpleProbableThreats() {
|
||||
LOGGER.info("### Filtering ProbabilisticThreatAwareSystem by probability ###");
|
||||
@@ -69,20 +69,22 @@ public class App {
|
||||
var probabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("Sys-1", probableThreats);
|
||||
|
||||
LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : "
|
||||
+ probabilisticThreatAwareSystem);
|
||||
LOGGER.info(
|
||||
"Filtering ProbabilisticThreatAwareSystem. Initial : " + probabilisticThreatAwareSystem);
|
||||
|
||||
//Filtering using filterer
|
||||
var filteredThreatAwareSystem = probabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
// Filtering using filterer
|
||||
var filteredThreatAwareSystem =
|
||||
probabilisticThreatAwareSystem
|
||||
.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
LOGGER.info("Filtered by probability = 0.99 : " + filteredThreatAwareSystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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 ###");
|
||||
@@ -95,11 +97,10 @@ public class App {
|
||||
|
||||
LOGGER.info("Filtering ThreatAwareSystem. Initial : " + threatAwareSystem);
|
||||
|
||||
//Filtering using Filterer
|
||||
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
// Filtering using Filterer
|
||||
var rootkitThreatAwareSystem =
|
||||
threatAwareSystem.filtered().by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,10 +28,11 @@ import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Filterer helper interface.
|
||||
*
|
||||
* @param <G> type of the container-like object.
|
||||
* @param <E> type of the elements contained within this container-like object.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Filterer<G, E> {
|
||||
G by(Predicate<? super E> predicate);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-4
@@ -27,13 +27,12 @@ package com.iluwatar.filterer.threat;
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents system that is aware of its threats with given probability of their occurrence.
|
||||
*/
|
||||
/** Represents system that is aware of its threats with given probability of their occurrence. */
|
||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return {@link ProbableThreat}
|
||||
*/
|
||||
@Override
|
||||
@@ -41,9 +40,9 @@ public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return {@link Filterer}
|
||||
*/
|
||||
@Override
|
||||
Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,13 +24,12 @@
|
||||
*/
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
/**
|
||||
* Represents threat that might be a threat with given probability.
|
||||
*/
|
||||
/** Represents threat that might be a threat with given probability. */
|
||||
public interface ProbableThreat extends Threat {
|
||||
/**
|
||||
* Returns probability of occurrence of given threat.
|
||||
*
|
||||
* @return probability of occurrence of given threat.
|
||||
*/
|
||||
double probability();
|
||||
}
|
||||
}
|
||||
|
||||
+6
-18
@@ -31,9 +31,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
@@ -42,25 +40,19 @@ public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreat
|
||||
private final String systemId;
|
||||
private final List<ProbableThreat> threats;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public List<? extends ProbableThreat> threats() {
|
||||
return threats;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered() {
|
||||
return this::filteredGroup;
|
||||
@@ -71,11 +63,7 @@ public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreat
|
||||
return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<ProbableThreat> filteredItems(
|
||||
final Predicate<? super ProbableThreat> predicate) {
|
||||
return this.threats.stream()
|
||||
.filter(predicate)
|
||||
.toList();
|
||||
private List<ProbableThreat> filteredItems(final Predicate<? super ProbableThreat> predicate) {
|
||||
return this.threats.stream().filter(predicate).toList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,23 +26,19 @@ package com.iluwatar.filterer.threat;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double probability() {
|
||||
return probability;
|
||||
@@ -50,9 +46,6 @@ public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleProbableThreat{"
|
||||
+ "probability=" + probability
|
||||
+ "} "
|
||||
+ super.toString();
|
||||
return "SimpleProbableThreat{" + "probability=" + probability + "} " + super.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Represents a simple threat.
|
||||
*/
|
||||
/** Represents a simple threat. */
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
@@ -40,28 +38,21 @@ public class SimpleThreat implements Threat {
|
||||
private final int id;
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public ThreatType type() {
|
||||
return threatType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,9 +32,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
@@ -43,25 +41,19 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||
private final String systemId;
|
||||
private final List<Threat> issues;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public List<? extends Threat> threats() {
|
||||
return new ArrayList<>(issues);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered() {
|
||||
return this::filteredGroup;
|
||||
@@ -72,8 +64,6 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||
}
|
||||
|
||||
private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
|
||||
return this.issues.stream()
|
||||
.filter(predicate).toList();
|
||||
return this.issues.stream().filter(predicate).toList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
/**
|
||||
* Represents a threat that can be detected in given system.
|
||||
*/
|
||||
/** Represents a threat that can be detected in given system. */
|
||||
public interface Threat {
|
||||
/**
|
||||
* Returns name of the threat.
|
||||
@@ -44,6 +42,7 @@ public interface Threat {
|
||||
|
||||
/**
|
||||
* Returns threat type.
|
||||
*
|
||||
* @return {@link ThreatType}
|
||||
*/
|
||||
ThreatType type();
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.filterer.threat;
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents system that is aware of threats that are present in it.
|
||||
*/
|
||||
/** Represents system that is aware of threats that are present in it. */
|
||||
public interface ThreatAwareSystem<T extends Threat> {
|
||||
|
||||
/**
|
||||
@@ -41,15 +39,16 @@ public interface ThreatAwareSystem<T extends Threat> {
|
||||
|
||||
/**
|
||||
* Returns list of threats for this system.
|
||||
*
|
||||
* @return list of threats for this system.
|
||||
*/
|
||||
List<T> threats();
|
||||
|
||||
/**
|
||||
* Returns the instance of {@link Filterer} helper interface that allows to covariantly
|
||||
* specify lower bound for predicate that we want to filter by.
|
||||
* 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<ThreatAwareSystem<T>, T> filtered();
|
||||
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
/**
|
||||
* Enum class representing Threat types.
|
||||
*/
|
||||
/** Enum class representing Threat types. */
|
||||
public enum ThreatType {
|
||||
TROJAN,
|
||||
WORM,
|
||||
|
||||
@@ -32,6 +32,6 @@ class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldLaunchApp() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-6
@@ -33,7 +33,7 @@ class SimpleProbabilisticThreatAwareSystemTest {
|
||||
|
||||
@Test
|
||||
void shouldFilterByProbability() {
|
||||
//given
|
||||
// given
|
||||
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);
|
||||
@@ -41,12 +41,14 @@ class SimpleProbabilisticThreatAwareSystemTest {
|
||||
var simpleProbabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||
|
||||
//when
|
||||
var filtered = simpleProbabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
// when
|
||||
var filtered =
|
||||
simpleProbabilisticThreatAwareSystem
|
||||
.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(filtered.threats().size(), 1);
|
||||
assertEquals(filtered.threats().get(0), trojan);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -32,18 +32,18 @@ import org.junit.jupiter.api.Test;
|
||||
class SimpleThreatAwareSystemTest {
|
||||
@Test
|
||||
void shouldFilterByThreatType() {
|
||||
//given
|
||||
// given
|
||||
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);
|
||||
|
||||
var threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
|
||||
|
||||
//when
|
||||
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
// when
|
||||
var rootkitThreatAwareSystem =
|
||||
threatAwareSystem.filtered().by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(rootkitThreatAwareSystem.threats().size(), 1);
|
||||
assertEquals(rootkitThreatAwareSystem.threats().get(0), rootkit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user