Skip to content

Commit

Permalink
Java 11 migration: patterns starting with a (#1084)
Browse files Browse the repository at this point in the history
* Moves abstract-factory pattern to java 11

* Moves abstract-document pattern to java 11

* Moves acyclic-visitor pattern to java 11

* Moves adapter pattern to java 11

* Moves aggregator-microservices pattern to java 11

* Moves api-gateway pattern to java 11
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 13, 2019
1 parent 3c57bf7 commit f04fc3c
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

package com.iluwatar.abstractdocument;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

Expand Down Expand Up @@ -55,9 +55,13 @@ public Object get(String key) {

@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el).findAny();
return any.map(maps -> maps.stream().map(constructor)).orElseGet(Stream::empty);
return Stream.ofNullable(get(key))
.filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el)
.findAny()
.stream()
.flatMap(Collection::stream)
.map(constructor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ public App() {
var car = new Car(carProperties);

LOGGER.info("Here is our car:");
LOGGER.info("-> model: {}", car.getModel().get());
LOGGER.info("-> price: {}", car.getPrice().get());
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
LOGGER.info("-> parts: ");
car.getParts().forEach(p -> LOGGER
.info("\t{}/{}/{}", p.getType().get(), p.getModel().get(), p.getPrice().get()));
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
p.getType().orElse(null),
p.getModel().orElse(null),
p.getPrice().orElse(null))
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

package com.iluwatar.abstractdocument;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

/**
* AbstractDocument test class
Expand Down Expand Up @@ -61,22 +61,22 @@ public void shouldRetrieveChildren() {

document.put(KEY, children);

Stream<DocumentImplementation> childrenStream = document.children(KEY, DocumentImplementation::new);
var childrenStream = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(2, childrenStream.count());
}

@Test
public void shouldRetrieveEmptyStreamForNonExistingChildren() {
Stream<DocumentImplementation> children = document.children(KEY, DocumentImplementation::new);
var children = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(0, children.count());
}

@Test
public void shouldIncludePropsInToString() {
Map<String, Object> props = Map.of(KEY, VALUE);
DocumentImplementation document = new DocumentImplementation(props);
var props = Map.of(KEY, (Object) VALUE);
var document = new DocumentImplementation(props);
assertTrue(document.toString().contains(KEY));
assertTrue(document.toString().contains(VALUE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@

package com.iluwatar.abstractdocument;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.Part;
import com.iluwatar.abstractdocument.domain.enums.Property;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

/**
* Test for Part and Car
Expand All @@ -47,27 +46,27 @@ public class DomainTest {

@Test
public void shouldConstructPart() {
Map<String, Object> partProperties = Map.of(
Property.TYPE.toString(), TEST_PART_TYPE,
Property.MODEL.toString(), TEST_PART_MODEL,
Property.PRICE.toString(), TEST_PART_PRICE);
Part part = new Part(partProperties);

assertEquals(TEST_PART_TYPE, part.getType().get());
assertEquals(TEST_PART_MODEL, part.getModel().get());
assertEquals(TEST_PART_PRICE, part.getPrice().get());
var partProperties = Map.of(
Property.TYPE.toString(), TEST_PART_TYPE,
Property.MODEL.toString(), TEST_PART_MODEL,
Property.PRICE.toString(), (Object) TEST_PART_PRICE
);
var part = new Part(partProperties);
assertEquals(TEST_PART_TYPE, part.getType().orElseThrow());
assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow());
assertEquals(TEST_PART_PRICE, part.getPrice().orElseThrow());
}

@Test
public void shouldConstructCar() {
Map<String, Object> carProperties = Map.of(
Property.MODEL.toString(), TEST_CAR_MODEL,
Property.PRICE.toString(), TEST_CAR_PRICE,
Property.PARTS.toString(), List.of(Map.of(), Map.of()));
Car car = new Car(carProperties);

assertEquals(TEST_CAR_MODEL, car.getModel().get());
assertEquals(TEST_CAR_PRICE, car.getPrice().get());
var carProperties = Map.of(
Property.MODEL.toString(), TEST_CAR_MODEL,
Property.PRICE.toString(), TEST_CAR_PRICE,
Property.PARTS.toString(), List.of(Map.of(), Map.of())
);
var car = new Car(carProperties);
assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow());
assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow());
assertEquals(2, car.getParts().count());
}

Expand Down
10 changes: 5 additions & 5 deletions abstract-factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public class OrcKingdomFactory implements KingdomFactory {
Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.

```java
KingdomFactory factory = new ElfKingdomFactory();
Castle castle = factory.createCastle();
King king = factory.createKing();
Army army = factory.createArmy();
var factory = new ElfKingdomFactory();
var castle = factory.createCastle();
var king = factory.createKing();
var army = factory.createArmy();

castle.getDescription(); // Output: This is the Elven castle!
king.getDescription(); // Output: This is the Elven king!
Expand Down Expand Up @@ -143,7 +143,7 @@ public static class FactoryMaker {
}

public static void main(String[] args) {
App app = new App();
var app = new App();

LOGGER.info("Elf Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static KingdomFactory makeFactory(KingdomType type) {
*/
public static void main(String[] args) {

App app = new App();
var app = new App();

LOGGER.info("Elf Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import com.iluwatar.abstractfactory.App.FactoryMaker;
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -49,40 +48,40 @@ public void setUp() {

@Test
public void king() {
final King elfKing = app.getKing(elfFactory);
final var elfKing = app.getKing(elfFactory);
assertTrue(elfKing instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
final King orcKing = app.getKing(orcFactory);
final var orcKing = app.getKing(orcFactory);
assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
}

@Test
public void castle() {
final Castle elfCastle = app.getCastle(elfFactory);
final var elfCastle = app.getCastle(elfFactory);
assertTrue(elfCastle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
final Castle orcCastle = app.getCastle(orcFactory);
final var orcCastle = app.getCastle(orcFactory);
assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
}

@Test
public void army() {
final Army elfArmy = app.getArmy(elfFactory);
final var elfArmy = app.getArmy(elfFactory);
assertTrue(elfArmy instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
final Army orcArmy = app.getArmy(orcFactory);
final var orcArmy = app.getArmy(orcFactory);
assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
}

@Test
public void createElfKingdom() {
app.createKingdom(elfFactory);
final King king = app.getKing();
final Castle castle = app.getCastle();
final Army army = app.getArmy();
final var king = app.getKing();
final var castle = app.getCastle();
final var army = app.getArmy();
assertTrue(king instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof ElfCastle);
Expand All @@ -94,9 +93,9 @@ public void createElfKingdom() {
@Test
public void createOrcKingdom() {
app.createKingdom(orcFactory);
final King king = app.getKing();
final Castle castle = app.getCastle();
final Army army = app.getArmy();
final var king = app.getKing();
final var castle = app.getCastle();
final var army = app.getArmy();
assertTrue(king instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof OrcCastle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@

import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
* Tests that Abstract Factory example runs without errors.
*/
public class AppTest {
@Test
public void test() throws IOException {
String[] args = {};
App.main(args);
public void test() {
App.main(new String[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@

import org.junit.jupiter.api.Test;

import com.iluwatar.acyclicvisitor.App;

/**
* Tests that the Acyclic Visitor example runs without errors.
*/
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 @@ -37,7 +37,7 @@
*/
public class ConfigureForDosVisitorTest {

TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class);
private TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class);

@Test
public void testVisitForZoom() {
Expand All @@ -46,19 +46,21 @@ public void testVisitForZoom() {

conDos.visit(zoom);

assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
tuple(INFO, zoom + " used with Dos configurator."));
assertThat(logger.getLoggingEvents())
.extracting("level", "message")
.contains(tuple(INFO, zoom + " used with Dos configurator."));
}

@Test
public void testVisitForHayes() {
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor();
Hayes hayes = new Hayes();
var conDos = new ConfigureForDosVisitor();
var hayes = new Hayes();

conDos.visit(hayes);

assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
tuple(INFO, hayes + " used with Dos configurator."));
assertThat(logger.getLoggingEvents())
.extracting("level", "message")
.contains(tuple(INFO, hayes + " used with Dos configurator."));
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void testVisitForZoom() {

conUnix.visit(zoom);

assertThat(LOGGER.getLoggingEvents()).extracting("level", "message").contains(
tuple(INFO, zoom + " used with Unix configurator."));
assertThat(LOGGER.getLoggingEvents())
.extracting("level", "message")
.contains(tuple(INFO, zoom + " used with Unix configurator."));
}
}
2 changes: 1 addition & 1 deletion adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class FishingBoatAdapter implements RowingBoat {
And now the `Captain` can use the `FishingBoat` to escape the pirates.

```java
Captain captain = new Captain(new FishingBoatAdapter());
var captain = new Captain(new FishingBoatAdapter());
captain.row();
```

Expand Down
Loading

0 comments on commit f04fc3c

Please sign in to comment.