Java 11 migration: patterns (remaining b-c) (#1081)
* Moves business-delegate pattern to java 11 * Moves bytecode pattern to java 11 * Moves caching pattern to java 11 * Moves callback pattern to java 11 * Moves chain pattern to java 11 * Moves circuit-breaker pattern to java 11 * Moves collection-pipeline pattern to java 11 * Moves command pattern to java 11 * Moves commander pattern to java 11 * Moves composite pattern to java 11 * Corrects test cases
This commit is contained in:
committed by
Ilkka Seppälä
parent
6ef840f3cf
commit
33ea7335b1
@ -49,12 +49,12 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
LOGGER.info("Message from the orcs: ");
|
||||
|
||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||
var orcMessage = new Messenger().messageFromOrcs();
|
||||
orcMessage.print();
|
||||
|
||||
LOGGER.info("\nMessage from the elves: ");
|
||||
|
||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||
var elfMessage = new Messenger().messageFromElves();
|
||||
elfMessage.print();
|
||||
}
|
||||
}
|
||||
|
@ -52,9 +52,7 @@ public abstract class LetterComposite {
|
||||
*/
|
||||
public void print() {
|
||||
printThisBefore();
|
||||
for (LetterComposite letter : children) {
|
||||
letter.print();
|
||||
}
|
||||
children.forEach(LetterComposite::print);
|
||||
printThisAfter();
|
||||
}
|
||||
}
|
||||
|
@ -32,20 +32,17 @@ public class Messenger {
|
||||
|
||||
LetterComposite messageFromOrcs() {
|
||||
|
||||
List<Word> words = List.of(
|
||||
new Word(List.of(new Letter('W'), new Letter('h'), new Letter('e'), new Letter(
|
||||
'r'), new Letter('e'))),
|
||||
new Word(List.of(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
|
||||
'r'), new Letter('e'))),
|
||||
new Word(List.of(new Letter('i'), new Letter('s'))),
|
||||
new Word(List.of(new Letter('a'))),
|
||||
new Word(List.of(new Letter('w'), new Letter('h'), new Letter('i'), new Letter(
|
||||
'p'))),
|
||||
new Word(List.of(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
|
||||
'r'), new Letter('e'))),
|
||||
new Word(List.of(new Letter('i'), new Letter('s'))),
|
||||
new Word(List.of(new Letter('a'))),
|
||||
new Word(List.of(new Letter('w'), new Letter('a'), new Letter('y'))));
|
||||
var words = List.of(
|
||||
new Word('W', 'h', 'e', 'r', 'e'),
|
||||
new Word('t', 'h', 'e', 'r', 'e'),
|
||||
new Word('i', 's'),
|
||||
new Word('a'),
|
||||
new Word('w', 'h', 'i', 'p'),
|
||||
new Word('t', 'h', 'e', 'r', 'e'),
|
||||
new Word('i', 's'),
|
||||
new Word('a'),
|
||||
new Word('w', 'a', 'y')
|
||||
);
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
@ -53,15 +50,14 @@ public class Messenger {
|
||||
|
||||
LetterComposite messageFromElves() {
|
||||
|
||||
List<Word> words = List.of(
|
||||
new Word(List.of(new Letter('M'), new Letter('u'), new Letter('c'), new Letter('h'))),
|
||||
new Word(List.of(new Letter('w'), new Letter('i'), new Letter('n'), new Letter('d'))),
|
||||
new Word(List.of(new Letter('p'), new Letter('o'), new Letter('u'), new Letter('r'),
|
||||
new Letter('s'))),
|
||||
new Word(List.of(new Letter('f'), new Letter('r'), new Letter('o'), new Letter('m'))),
|
||||
new Word(List.of(new Letter('y'), new Letter('o'), new Letter('u'), new Letter('r'))),
|
||||
new Word(List.of(new Letter('m'), new Letter('o'), new Letter('u'), new Letter('t'),
|
||||
new Letter('h'))));
|
||||
var words = List.of(
|
||||
new Word('M', 'u', 'c', 'h'),
|
||||
new Word('w', 'i', 'n', 'd'),
|
||||
new Word('p', 'o', 'u', 'r', 's'),
|
||||
new Word('f', 'r', 'o', 'm'),
|
||||
new Word('y', 'o', 'u', 'r'),
|
||||
new Word('m', 'o', 'u', 't', 'h')
|
||||
);
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
|
@ -34,9 +34,7 @@ public class Sentence extends LetterComposite {
|
||||
* Constructor.
|
||||
*/
|
||||
public Sentence(List<Word> words) {
|
||||
for (Word w : words) {
|
||||
this.add(w);
|
||||
}
|
||||
words.forEach(this::add);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -34,8 +34,12 @@ public class Word extends LetterComposite {
|
||||
* Constructor.
|
||||
*/
|
||||
public Word(List<Letter> letters) {
|
||||
for (Letter l : letters) {
|
||||
this.add(l);
|
||||
letters.forEach(this::add);
|
||||
}
|
||||
|
||||
public Word(char... letters) {
|
||||
for (char letter : letters) {
|
||||
this.add(new Letter(letter));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,15 +26,12 @@ package com.iluwatar.composite;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public class MessengerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testMessageFromOrcs() {
|
||||
final Messenger messenger = new Messenger();
|
||||
final var messenger = new Messenger();
|
||||
testMessage(
|
||||
messenger.messageFromOrcs(),
|
||||
"Where there is a whip there is a way."
|
||||
@ -84,7 +84,7 @@ public class MessengerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testMessageFromElves() {
|
||||
final Messenger messenger = new Messenger();
|
||||
final var messenger = new Messenger();
|
||||
testMessage(
|
||||
messenger.messageFromElves(),
|
||||
"Much wind pours from your mouth."
|
||||
@ -99,7 +99,7 @@ public class MessengerTest {
|
||||
*/
|
||||
private void testMessage(final LetterComposite composedMessage, final String message) {
|
||||
// Test is the composed message has the correct number of words
|
||||
final String[] words = message.split(" ");
|
||||
final var words = message.split(" ");
|
||||
assertNotNull(composedMessage);
|
||||
assertEquals(words.length, composedMessage.count());
|
||||
|
||||
|
Reference in New Issue
Block a user