Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 11 migrate all remaining s #1120

Merged
merged 16 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Moves specification to Java 11
  • Loading branch information
anuragagarwal561994 committed Dec 28, 2019
commit 5a2c2fed85fb14ab5627ab0c45c0378ac3c0872b
8 changes: 4 additions & 4 deletions specification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
```

Expand Down
54 changes: 22 additions & 32 deletions specification/src/main/java/com/iluwatar/specification/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class AppTest {

@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,24 @@ public void testGetMovement(Creature testedCreature, String name, Size size, Mov
@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -59,15 +59,15 @@ public void testAndComposition() {
*/
@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));
Expand All @@ -78,15 +78,15 @@ public void testOrComposition() {
*/
@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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down