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:
Ilkka Seppälä
2025-03-29 19:34:27 +02:00
committed by GitHub
parent 371439aeaa
commit 0ca162a55c
1863 changed files with 14408 additions and 17637 deletions
@@ -44,32 +44,25 @@ import java.util.function.Predicate;
import lombok.extern.slf4j.Slf4j;
/**
* <p>The central idea of the Specification pattern is to separate the statement of how to match a
* The central idea of the Specification pattern is to separate the statement of how to match a
* candidate, from the candidate object that it is matched against. As well as its usefulness in
* selection, it is also valuable for validation and for building to order.</p>
* selection, it is also valuable for validation and for building to order.
*
* <p>In this example we have a pool of creatures with different properties. We then have defined
* separate selection rules (Specifications) that we apply to the collection and as output receive
* only the creatures that match the selection criteria.</p>
* only the creatures that match the selection criteria.
*
* <p>http://martinfowler.com/apsupp/spec.pdf</p>
* <p>http://martinfowler.com/apsupp/spec.pdf
*/
@Slf4j
public class App {
/**
* Program entry point.
*/
/** Program entry point. */
public static void main(String[] args) {
// initialize creatures list
var creatures = List.of(
new Goblin(),
new Octopus(),
new Dragon(),
new Shark(),
new Troll(),
new KillerBee()
);
var creatures =
List.of(
new Goblin(), new Octopus(), new Dragon(), new Shark(), new Troll(), new KillerBee());
// so-called "hard-coded" specification
LOGGER.info("Demonstrating hard-coded specification :");
// find all walking creatures
@@ -96,9 +89,11 @@ public class App {
print(creatures, redAndFlying);
// find all creatures dark or red, non-swimming, and heavier than or equal to 400kg
LOGGER.info("Find all scary creatures");
var scaryCreaturesSelector = new ColorSelector(Color.DARK)
.or(new ColorSelector(Color.RED)).and(new MovementSelector(Movement.SWIMMING).not())
.and(new MassGreaterThanSelector(400.0).or(new MassEqualSelector(400.0)));
var scaryCreaturesSelector =
new ColorSelector(Color.DARK)
.or(new ColorSelector(Color.RED))
.and(new MovementSelector(Movement.SWIMMING).not())
.and(new MassGreaterThanSelector(400.0).or(new MassEqualSelector(400.0)));
print(creatures, scaryCreaturesSelector);
}
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* Base class for concrete creatures.
*/
/** Base class for concrete creatures. */
public abstract class AbstractCreature implements Creature {
private final String name;
@@ -40,9 +38,7 @@ public abstract class AbstractCreature implements Creature {
private final Color color;
private final Mass mass;
/**
* Constructor.
*/
/** Constructor. */
public AbstractCreature(String name, Size size, Movement movement, Color color, Mass mass) {
this.name = name;
this.size = size;
@@ -53,8 +49,8 @@ public abstract class AbstractCreature implements Creature {
@Override
public String toString() {
return String.format("%s [size=%s, movement=%s, color=%s, mass=%s]",
name, size, movement, color, mass);
return String.format(
"%s [size=%s, movement=%s, color=%s, mass=%s]", name, size, movement, color, mass);
}
@Override
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* Creature interface.
*/
/** Creature interface. */
public interface Creature {
String getName();
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* Dragon creature.
*/
/** Dragon creature. */
public class Dragon extends AbstractCreature {
public Dragon() {
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* Goblin creature.
*/
/** Goblin creature. */
public class Goblin extends AbstractCreature {
public Goblin() {
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* KillerBee creature.
*/
/** KillerBee creature. */
public class KillerBee extends AbstractCreature {
public KillerBee() {
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* Octopus creature.
*/
/** Octopus creature. */
public class Octopus extends AbstractCreature {
public Octopus() {
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* Shark creature.
*/
/** Shark creature. */
public class Shark extends AbstractCreature {
public Shark() {
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
/**
* Troll creature.
*/
/** Troll creature. */
public class Troll extends AbstractCreature {
public Troll() {
@@ -24,12 +24,12 @@
*/
package com.iluwatar.specification.property;
/**
* Color property.
*/
/** Color property. */
public enum Color {
DARK("dark"), LIGHT("light"), GREEN("green"), RED("red");
DARK("dark"),
LIGHT("light"),
GREEN("green"),
RED("red");
private final String title;
@@ -26,9 +26,7 @@ package com.iluwatar.specification.property;
import lombok.EqualsAndHashCode;
/**
* Mass property.
*/
/** Mass property. */
@EqualsAndHashCode
public class Mass {
@@ -60,5 +58,4 @@ public class Mass {
public String toString() {
return title;
}
}
@@ -24,12 +24,11 @@
*/
package com.iluwatar.specification.property;
/**
* Movement property.
*/
/** Movement property. */
public enum Movement {
WALKING("walking"), SWIMMING("swimming"), FLYING("flying");
WALKING("walking"),
SWIMMING("swimming"),
FLYING("flying");
private final String title;
@@ -24,12 +24,11 @@
*/
package com.iluwatar.specification.property;
/**
* Size property.
*/
/** Size property. */
public enum Size {
SMALL("small"), NORMAL("normal"), LARGE("large");
SMALL("small"),
NORMAL("normal"),
LARGE("large");
private final String title;
@@ -26,9 +26,7 @@ package com.iluwatar.specification.selector;
import java.util.function.Predicate;
/**
* Base class for selectors.
*/
/** Base class for selectors. */
public abstract class AbstractSelector<T> implements Predicate<T> {
public AbstractSelector<T> and(AbstractSelector<T> other) {
@@ -27,9 +27,7 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Color;
/**
* Color selector.
*/
/** Color selector. */
public class ColorSelector extends AbstractSelector<Creature> {
private final Color color;
@@ -26,9 +26,7 @@ package com.iluwatar.specification.selector;
import java.util.List;
/**
* A Selector defined as the conjunction (AND) of other (leaf) selectors.
*/
/** A Selector defined as the conjunction (AND) of other (leaf) selectors. */
public class ConjunctionSelector<T> extends AbstractSelector<T> {
private final List<AbstractSelector<T>> leafComponents;
@@ -38,9 +36,7 @@ public class ConjunctionSelector<T> extends AbstractSelector<T> {
this.leafComponents = List.of(selectors);
}
/**
* Tests if *all* selectors pass the test.
*/
/** Tests if *all* selectors pass the test. */
@Override
public boolean test(T t) {
return leafComponents.stream().allMatch(comp -> (comp.test(t)));
@@ -26,9 +26,7 @@ package com.iluwatar.specification.selector;
import java.util.List;
/**
* A Selector defined as the disjunction (OR) of other (leaf) selectors.
*/
/** A Selector defined as the disjunction (OR) of other (leaf) selectors. */
public class DisjunctionSelector<T> extends AbstractSelector<T> {
private final List<AbstractSelector<T>> leafComponents;
@@ -38,9 +36,7 @@ public class DisjunctionSelector<T> extends AbstractSelector<T> {
this.leafComponents = List.of(selectors);
}
/**
* Tests if *at least one* selector passes the test.
*/
/** Tests if *at least one* selector passes the test. */
@Override
public boolean test(T t) {
return leafComponents.stream().anyMatch(comp -> comp.test(t));
@@ -27,16 +27,12 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Mass;
/**
* Mass selector for values exactly equal than the parameter.
*/
/** Mass selector for values exactly equal than the parameter. */
public class MassEqualSelector extends AbstractSelector<Creature> {
private final Mass mass;
/**
* The use of a double as a parameter will spare some typing when instantiating this class.
*/
/** The use of a double as a parameter will spare some typing when instantiating this class. */
public MassEqualSelector(double mass) {
this.mass = new Mass(mass);
}
@@ -27,16 +27,12 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Mass;
/**
* Mass selector for values greater than the parameter.
*/
/** Mass selector for values greater than the parameter. */
public class MassGreaterThanSelector extends AbstractSelector<Creature> {
private final Mass mass;
/**
* The use of a double as a parameter will spare some typing when instantiating this class.
*/
/** The use of a double as a parameter will spare some typing when instantiating this class. */
public MassGreaterThanSelector(double mass) {
this.mass = new Mass(mass);
}
@@ -27,16 +27,12 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Mass;
/**
* Mass selector for values smaller or equal to the parameter.
*/
/** Mass selector for values smaller or equal to the parameter. */
public class MassSmallerThanOrEqSelector extends AbstractSelector<Creature> {
private final Mass mass;
/**
* The use of a double as a parameter will spare some typing when instantiating this class.
*/
/** The use of a double as a parameter will spare some typing when instantiating this class. */
public MassSmallerThanOrEqSelector(double mass) {
this.mass = new Mass(mass);
}
@@ -27,9 +27,7 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Movement;
/**
* Movement selector.
*/
/** Movement selector. */
public class MovementSelector extends AbstractSelector<Creature> {
private final Movement movement;
@@ -24,7 +24,6 @@
*/
package com.iluwatar.specification.selector;
/**
* A Selector defined as the negation (NOT) of a (leaf) selectors. This is of course only useful
* when used in combination with other composite selectors.
@@ -37,9 +36,7 @@ public class NegationSelector<T> extends AbstractSelector<T> {
this.component = selector;
}
/**
* Tests if the selector fails the test (yes).
*/
/** Tests if the selector fails the test (yes). */
@Override
public boolean test(T t) {
return !(component.test(t));
@@ -27,9 +27,7 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Size;
/**
* Size selector.
*/
/** Size selector. */
public class SizeSelector extends AbstractSelector<Creature> {
private final Size size;
@@ -24,17 +24,15 @@
*/
package com.iluwatar.specification.app;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
import org.junit.jupiter.api.Test;
/** Application test */
class AppTest {
@Test
void shouldExecuteWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
assertDoesNotThrow(() -> App.main(new String[] {}));
}
}
@@ -36,10 +36,7 @@ import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
/**
* CreatureTest
*
*/
/** CreatureTest */
class CreatureTest {
/**
@@ -47,19 +44,24 @@ class CreatureTest {
*/
public static Collection<Object[]> dataProvider() {
return List.of(
new Object[]{new Dragon(), "Dragon", Size.LARGE, Movement.FLYING, Color.RED,
new Mass(39300.0)},
new Object[]{new Goblin(), "Goblin", Size.SMALL, Movement.WALKING, Color.GREEN,
new Mass(30.0)},
new Object[]{new KillerBee(), "KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT,
new Mass(6.7)},
new Object[]{new Octopus(), "Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK,
new Mass(12.0)},
new Object[]{new Shark(), "Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT,
new Mass(500.0)},
new Object[]{new Troll(), "Troll", Size.LARGE, Movement.WALKING, Color.DARK,
new Mass(4000.0)}
);
new Object[] {
new Dragon(), "Dragon", Size.LARGE, Movement.FLYING, Color.RED, new Mass(39300.0)
},
new Object[] {
new Goblin(), "Goblin", Size.SMALL, Movement.WALKING, Color.GREEN, new Mass(30.0)
},
new Object[] {
new KillerBee(), "KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT, new Mass(6.7)
},
new Object[] {
new Octopus(), "Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK, new Mass(12.0)
},
new Object[] {
new Shark(), "Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT, new Mass(500.0)
},
new Object[] {
new Troll(), "Troll", Size.LARGE, Movement.WALKING, Color.DARK, new Mass(4000.0)
});
}
@ParameterizedTest
@@ -82,25 +84,27 @@ class CreatureTest {
@ParameterizedTest
@MethodSource("dataProvider")
void testGetColor(Creature testedCreature, String name, Size size, Movement movement,
Color color) {
void testGetColor(
Creature testedCreature, String name, Size size, Movement movement, Color color) {
assertEquals(color, testedCreature.getColor());
}
@ParameterizedTest
@MethodSource("dataProvider")
void testGetMass(Creature testedCreature, String name, Size size, Movement movement,
Color color, Mass mass) {
void testGetMass(
Creature testedCreature, String name, Size size, Movement movement, Color color, Mass mass) {
assertEquals(mass, testedCreature.getMass());
}
@ParameterizedTest
@MethodSource("dataProvider")
void testToString(Creature testedCreature, String name, Size size, Movement movement,
Color color, Mass mass) {
void testToString(
Creature testedCreature, String name, Size size, Movement movement, Color color, Mass mass) {
final var toString = testedCreature.toString();
assertNotNull(toString);
assertEquals(String
.format("%s [size=%s, movement=%s, color=%s, mass=%s]", name, size, movement, color, mass), toString);
assertEquals(
String.format(
"%s [size=%s, movement=%s, color=%s, mass=%s]", name, size, movement, color, mass),
toString);
}
}
}
@@ -33,15 +33,10 @@ import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Color;
import org.junit.jupiter.api.Test;
/**
* ColorSelectorTest
*
*/
/** ColorSelectorTest */
class ColorSelectorTest {
/**
* Verify if the color selector gives the correct results
*/
/** Verify if the color selector gives the correct results */
@Test
void testColor() {
final var greenCreature = mock(Creature.class);
@@ -53,7 +48,5 @@ class ColorSelectorTest {
final var greenSelector = new ColorSelector(Color.GREEN);
assertTrue(greenSelector.test(greenCreature));
assertFalse(greenSelector.test(redCreature));
}
}
}
@@ -36,9 +36,7 @@ import org.junit.jupiter.api.Test;
class CompositeSelectorsTest {
/**
* Verify if the conjunction selector gives the correct results.
*/
/** Verify if the conjunction selector gives the correct results. */
@Test
void testAndComposition() {
final var swimmingHeavyCreature = mock(Creature.class);
@@ -49,15 +47,13 @@ class CompositeSelectorsTest {
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final var lightAndSwimmingSelector = new MassSmallerThanOrEqSelector(50.0)
.and(new MovementSelector(Movement.SWIMMING));
final var lightAndSwimmingSelector =
new MassSmallerThanOrEqSelector(50.0).and(new MovementSelector(Movement.SWIMMING));
assertFalse(lightAndSwimmingSelector.test(swimmingHeavyCreature));
assertTrue(lightAndSwimmingSelector.test(swimmingLightCreature));
}
/**
* Verify if the disjunction selector gives the correct results.
*/
/** Verify if the disjunction selector gives the correct results. */
@Test
void testOrComposition() {
final var swimmingHeavyCreature = mock(Creature.class);
@@ -68,15 +64,13 @@ class CompositeSelectorsTest {
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final var lightOrSwimmingSelector = new MassSmallerThanOrEqSelector(50.0)
.or(new MovementSelector(Movement.SWIMMING));
final var lightOrSwimmingSelector =
new MassSmallerThanOrEqSelector(50.0).or(new MovementSelector(Movement.SWIMMING));
assertTrue(lightOrSwimmingSelector.test(swimmingHeavyCreature));
assertTrue(lightOrSwimmingSelector.test(swimmingLightCreature));
}
/**
* Verify if the negation selector gives the correct results.
*/
/** Verify if the negation selector gives the correct results. */
@Test
void testNotComposition() {
final var swimmingHeavyCreature = mock(Creature.class);
@@ -35,9 +35,7 @@ import org.junit.jupiter.api.Test;
class MassSelectorTest {
/**
* Verify if the mass selector gives the correct results.
*/
/** Verify if the mass selector gives the correct results. */
@Test
void testMass() {
final var lightCreature = mock(Creature.class);
@@ -33,15 +33,10 @@ import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Movement;
import org.junit.jupiter.api.Test;
/**
* MovementSelectorTest
*
*/
/** MovementSelectorTest */
class MovementSelectorTest {
/**
* Verify if the movement selector gives the correct results.
*/
/** Verify if the movement selector gives the correct results. */
@Test
void testMovement() {
final var swimmingCreature = mock(Creature.class);
@@ -53,7 +48,5 @@ class MovementSelectorTest {
final var swimmingSelector = new MovementSelector(Movement.SWIMMING);
assertTrue(swimmingSelector.test(swimmingCreature));
assertFalse(swimmingSelector.test(flyingCreature));
}
}
}
@@ -33,15 +33,10 @@ import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Size;
import org.junit.jupiter.api.Test;
/**
* SizeSelectorTest
*
*/
/** SizeSelectorTest */
class SizeSelectorTest {
/**
* Verify if the size selector gives the correct results
*/
/** Verify if the size selector gives the correct results */
@Test
void testMovement() {
final var normalCreature = mock(Creature.class);
@@ -54,5 +49,4 @@ class SizeSelectorTest {
assertTrue(normalSelector.test(normalCreature));
assertFalse(normalSelector.test(smallCreature));
}
}