Java 11 migrate all remaining s (#1120)

* Moves saga to Java 11

* Moves semaphore to Java 11

* Moves servant to Java 11

* Moves serverless to Java 11

* Moves service-layer to Java 11

* Moves service-locator to Java 11

* Moves sharding to Java 11

* Moves singleton to Java 11

* Moves spatial-partition to Java 11

* Moves specification to Java 11

* Moves state to Java 11

* Moves step-builder to Java 11

* Moves strategy to Java 11

* Moves subclass-sandbox to Java 11

* Fixes checkstyle issues
This commit is contained in:
Anurag Agarwal
2020-01-04 22:06:08 +05:30
committed by Ilkka Seppälä
parent 310ae50248
commit cd2a2e7711
98 changed files with 718 additions and 855 deletions

View File

@ -100,24 +100,24 @@ public class MassGreaterThanSelector extends AbstractSelector<Creature> {
With these building blocks in place, we can perform a search for red creatures as follows:
```java
List<Creature> redCreatures = creatures.stream().filter(new ColorSelector(Color.RED))
var redCreatures = creatures.stream().filter(new ColorSelector(Color.RED))
.collect(Collectors.toList());
```
But we could also use our parameterized selector like this:
```java
List<Creature> heavyCreatures = creatures.stream().filter(new MassGreaterThanSelector(500.0)
var heavyCreatures = creatures.stream().filter(new MassGreaterThanSelector(500.0)
.collect(Collectors.toList());
```
Our third option is to combine multiple selectors together. Performing a search for special creatures (defined as red, flying, and not small) could be done as follows:
```java
AbstractSelector specialCreaturesSelector =
var specialCreaturesSelector =
new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING)).and(new SizeSelector(Size.SMALL).not());
List<Creature> specialCreatures = creatures.stream().filter(specialCreaturesSelector)
var specialCreatures = creatures.stream().filter(specialCreaturesSelector)
.collect(Collectors.toList());
```

View File

@ -32,14 +32,14 @@ import com.iluwatar.specification.creature.Shark;
import com.iluwatar.specification.creature.Troll;
import com.iluwatar.specification.property.Color;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.selector.AbstractSelector;
import com.iluwatar.specification.selector.ColorSelector;
import com.iluwatar.specification.selector.MassEqualSelector;
import com.iluwatar.specification.selector.MassGreaterThanSelector;
import com.iluwatar.specification.selector.MassSmallerThanOrEqSelector;
import com.iluwatar.specification.selector.MovementSelector;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Objects;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -63,57 +63,47 @@ public class App {
*/
public static void main(String[] args) {
// initialize creatures list
List<Creature> 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
LOGGER.info("Find all walking creatures");
List<Creature> walkingCreatures =
creatures.stream().filter(new MovementSelector(Movement.WALKING))
.collect(Collectors.toList());
walkingCreatures.forEach(c -> LOGGER.info(c.toString()));
print(creatures, new MovementSelector(Movement.WALKING));
// find all dark creatures
LOGGER.info("Find all dark creatures");
List<Creature> darkCreatures =
creatures.stream().filter(new ColorSelector(Color.DARK)).collect(Collectors.toList());
darkCreatures.forEach(c -> LOGGER.info(c.toString()));
print(creatures, new ColorSelector(Color.DARK));
LOGGER.info("\n");
// so-called "parameterized" specification
LOGGER.info("Demonstrating parameterized specification :");
// find all creatures heavier than 500kg
LOGGER.info("Find all creatures heavier than 600kg");
List<Creature> heavyCreatures =
creatures.stream().filter(new MassGreaterThanSelector(600.0))
.collect(Collectors.toList());
heavyCreatures.forEach(c -> LOGGER.info(c.toString()));
print(creatures, new MassGreaterThanSelector(600.0));
// find all creatures heavier than 500kg
LOGGER.info("Find all creatures lighter than or weighing exactly 500kg");
List<Creature> lightCreatures =
creatures.stream().filter(new MassSmallerThanOrEqSelector(500.0))
.collect(Collectors.toList());
lightCreatures.forEach(c -> LOGGER.info(c.toString()));
print(creatures, new MassSmallerThanOrEqSelector(500.0));
LOGGER.info("\n");
// so-called "composite" specification
LOGGER.info("Demonstrating composite specification :");
// find all red and flying creatures
LOGGER.info("Find all red and flying creatures");
List<Creature> redAndFlyingCreatures =
creatures.stream()
.filter(new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING)))
.collect(Collectors.toList());
redAndFlyingCreatures.forEach(c -> LOGGER.info(c.toString()));
var redAndFlying = new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING));
print(creatures, redAndFlying);
// find all creatures dark or red, non-swimming, and heavier than or equal to 400kg
LOGGER.info("Find all scary creatures");
AbstractSelector<Creature> scaryCreaturesSelector = new ColorSelector(Color.DARK)
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)));
List<Creature> scaryCreatures =
creatures.stream()
.filter(scaryCreaturesSelector)
.collect(Collectors.toList());
scaryCreatures.forEach(c -> LOGGER.info(c.toString()));
print(creatures, scaryCreaturesSelector);
}
private static void print(List<? extends Creature> creatures, Predicate<Creature> selector) {
creatures.stream().filter(selector).map(Objects::toString).forEach(LOGGER::info);
}
}

View File

@ -32,7 +32,6 @@ public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}

View File

@ -83,27 +83,24 @@ public class CreatureTest {
@ParameterizedTest
@MethodSource("dataProvider")
public void testGetColor(Creature testedCreature, String name, Size size, Movement movement,
Color color) {
Color color) {
assertEquals(color, testedCreature.getColor());
}
@ParameterizedTest
@MethodSource("dataProvider")
public void testGetMass(Creature testedCreature, String name, Size size, Movement movement,
Color color, Mass mass) {
Color color, Mass mass) {
assertEquals(mass, testedCreature.getMass());
}
@ParameterizedTest
@MethodSource("dataProvider")
public void testToString(Creature testedCreature, String name, Size size, Movement movement,
Color color, Mass mass) {
final String toString = testedCreature.toString();
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);
}
}

View File

@ -44,13 +44,13 @@ public class ColorSelectorTest {
*/
@Test
public void testColor() {
final Creature greenCreature = mock(Creature.class);
final var greenCreature = mock(Creature.class);
when(greenCreature.getColor()).thenReturn(Color.GREEN);
final Creature redCreature = mock(Creature.class);
final var redCreature = mock(Creature.class);
when(redCreature.getColor()).thenReturn(Color.RED);
final ColorSelector greenSelector = new ColorSelector(Color.GREEN);
final var greenSelector = new ColorSelector(Color.GREEN);
assertTrue(greenSelector.test(greenCreature));
assertFalse(greenSelector.test(redCreature));

View File

@ -40,16 +40,16 @@ public class CompositeSelectorsTest {
*/
@Test
public void testAndComposition() {
final Creature swimmingHeavyCreature = mock(Creature.class);
final var swimmingHeavyCreature = mock(Creature.class);
when(swimmingHeavyCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingHeavyCreature.getMass()).thenReturn(new Mass(100.0));
final Creature swimmingLightCreature = mock(Creature.class);
final var swimmingLightCreature = mock(Creature.class);
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final AbstractSelector<Creature> 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));
}
@ -59,15 +59,15 @@ public class CompositeSelectorsTest {
*/
@Test
public void testOrComposition() {
final Creature swimmingHeavyCreature = mock(Creature.class);
final var swimmingHeavyCreature = mock(Creature.class);
when(swimmingHeavyCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingHeavyCreature.getMass()).thenReturn(new Mass(100.0));
final Creature swimmingLightCreature = mock(Creature.class);
final var swimmingLightCreature = mock(Creature.class);
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final AbstractSelector<Creature> lightOrSwimmingSelector = new MassSmallerThanOrEqSelector(50.0)
final var lightOrSwimmingSelector = new MassSmallerThanOrEqSelector(50.0)
.or(new MovementSelector(Movement.SWIMMING));
assertTrue(lightOrSwimmingSelector.test(swimmingHeavyCreature));
assertTrue(lightOrSwimmingSelector.test(swimmingLightCreature));
@ -78,15 +78,15 @@ public class CompositeSelectorsTest {
*/
@Test
public void testNotComposition() {
final Creature swimmingHeavyCreature = mock(Creature.class);
final var swimmingHeavyCreature = mock(Creature.class);
when(swimmingHeavyCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingHeavyCreature.getMass()).thenReturn(new Mass(100.0));
final Creature swimmingLightCreature = mock(Creature.class);
final var swimmingLightCreature = mock(Creature.class);
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final AbstractSelector<Creature> heavySelector = new MassSmallerThanOrEqSelector(50.0).not();
final var heavySelector = new MassSmallerThanOrEqSelector(50.0).not();
assertTrue(heavySelector.test(swimmingHeavyCreature));
assertFalse(heavySelector.test(swimmingLightCreature));
}

View File

@ -39,13 +39,13 @@ public class MassSelectorTest {
*/
@Test
public void testMass() {
final Creature lightCreature = mock(Creature.class);
final var lightCreature = mock(Creature.class);
when(lightCreature.getMass()).thenReturn(new Mass(50.0));
final Creature heavyCreature = mock(Creature.class);
final var heavyCreature = mock(Creature.class);
when(heavyCreature.getMass()).thenReturn(new Mass(2500.0));
final MassSmallerThanOrEqSelector lightSelector = new MassSmallerThanOrEqSelector(500.0);
final var lightSelector = new MassSmallerThanOrEqSelector(500.0);
assertTrue(lightSelector.test(lightCreature));
assertFalse(lightSelector.test(heavyCreature));
}

View File

@ -44,13 +44,13 @@ public class MovementSelectorTest {
*/
@Test
public void testMovement() {
final Creature swimmingCreature = mock(Creature.class);
final var swimmingCreature = mock(Creature.class);
when(swimmingCreature.getMovement()).thenReturn(Movement.SWIMMING);
final Creature flyingCreature = mock(Creature.class);
final var flyingCreature = mock(Creature.class);
when(flyingCreature.getMovement()).thenReturn(Movement.FLYING);
final MovementSelector swimmingSelector = new MovementSelector(Movement.SWIMMING);
final var swimmingSelector = new MovementSelector(Movement.SWIMMING);
assertTrue(swimmingSelector.test(swimmingCreature));
assertFalse(swimmingSelector.test(flyingCreature));

View File

@ -44,13 +44,13 @@ public class SizeSelectorTest {
*/
@Test
public void testMovement() {
final Creature normalCreature = mock(Creature.class);
final var normalCreature = mock(Creature.class);
when(normalCreature.getSize()).thenReturn(Size.NORMAL);
final Creature smallCreature = mock(Creature.class);
final var smallCreature = mock(Creature.class);
when(smallCreature.getSize()).thenReturn(Size.SMALL);
final SizeSelector normalSelector = new SizeSelector(Size.NORMAL);
final var normalSelector = new SizeSelector(Size.NORMAL);
assertTrue(normalSelector.test(normalCreature));
assertFalse(normalSelector.test(smallCreature));
}