Use local variable type inference (#995)

* "visitor" pattern: Use local variable type inference

Update "visitor" pattern with local variable type inference.

* "value-object" pattern: Use local variable type inference

Update "value-object" pattern with local variable type inference.

* "unit-of-work" pattern: Use local variable type inference

Update "value-object" pattern with local variable type inference.

* "typeobjectpattern" pattern: Use local variable type inference

Update "value-object" pattern with local variable type inference.
This commit is contained in:
Zack Beach 2019-10-21 01:09:29 -04:00 committed by Ilkka Seppälä
parent 5fc03ee9f8
commit c81c3ff1c7
17 changed files with 110 additions and 110 deletions

View File

@ -53,17 +53,17 @@ public class App {
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException { public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
int givenTime = 50; //50ms var givenTime = 50; //50ms
int toWin = 500; //points var toWin = 500; //points
int pointsWon = 0; var pointsWon = 0;
int numOfRows = 3; var numOfRows = 3;
long start = System.currentTimeMillis(); var start = System.currentTimeMillis();
long end = System.currentTimeMillis(); var end = System.currentTimeMillis();
int round = 0; var round = 0;
while (pointsWon < toWin && end - start < givenTime) { while (pointsWon < toWin && end - start < givenTime) {
round++; round++;
CellPool pool = new CellPool(numOfRows * numOfRows + 5); var pool = new CellPool(numOfRows * numOfRows + 5);
CandyGame cg = new CandyGame(numOfRows, pool); var cg = new CandyGame(numOfRows, pool);
if (round > 1) { if (round > 1) {
LOGGER.info("Refreshing.."); LOGGER.info("Refreshing..");
} else { } else {

View File

@ -44,8 +44,8 @@ public class CandyGame {
this.cells = new Cell[num][num]; this.cells = new Cell[num][num];
this.pool = pool; this.pool = pool;
this.totalPoints = 0; this.totalPoints = 0;
for (int i = 0; i < num; i++) { for (var i = 0; i < num; i++) {
for (int j = 0; j < num; j++) { for (var j = 0; j < num; j++) {
this.cells[i][j] = this.pool.getNewCell(); this.cells[i][j] = this.pool.getNewCell();
this.cells[i][j].xIndex = j; this.cells[i][j].xIndex = j;
this.cells[i][j].yIndex = i; this.cells[i][j].yIndex = i;
@ -55,7 +55,7 @@ public class CandyGame {
static String numOfSpaces(int num) { static String numOfSpaces(int num) {
String result = ""; String result = "";
for (int i = 0; i < num; i++) { for (var i = 0; i < num; i++) {
result += " "; result += " ";
} }
return result; return result;
@ -63,11 +63,11 @@ public class CandyGame {
void printGameStatus() { void printGameStatus() {
LOGGER.info(""); LOGGER.info("");
for (int i = 0; i < cells.length; i++) { for (var i = 0; i < cells.length; i++) {
for (int j = 0; j < cells.length; j++) { for (var j = 0; j < cells.length; j++) {
String candyName = cells[i][j].candy.name; var candyName = cells[i][j].candy.name;
if (candyName.length() < 20) { if (candyName.length() < 20) {
int totalSpaces = 20 - candyName.length(); var totalSpaces = 20 - candyName.length();
LOGGER.info(numOfSpaces(totalSpaces / 2) + cells[i][j].candy.name LOGGER.info(numOfSpaces(totalSpaces / 2) + cells[i][j].candy.name
+ numOfSpaces(totalSpaces - totalSpaces / 2) + "|"); + numOfSpaces(totalSpaces - totalSpaces / 2) + "|");
} else { } else {
@ -105,16 +105,16 @@ public class CandyGame {
} }
boolean continueRound() { boolean continueRound() {
for (int i = 0; i < this.cells.length; i++) { for (var i = 0; i < this.cells.length; i++) {
if (this.cells[cells.length - 1][i].candy.getType().equals(Type.rewardFruit)) { if (this.cells[cells.length - 1][i].candy.getType().equals(Type.rewardFruit)) {
return true; return true;
} }
} }
for (int i = 0; i < this.cells.length; i++) { for (var i = 0; i < this.cells.length; i++) {
for (int j = 0; j < this.cells.length; j++) { for (var j = 0; j < this.cells.length; j++) {
if (!this.cells[i][j].candy.getType().equals(Type.rewardFruit)) { if (!this.cells[i][j].candy.getType().equals(Type.rewardFruit)) {
ArrayList<Cell> adj = adjacentCells(i,j); var adj = adjacentCells(i,j);
for (int a = 0; a < adj.size(); a++) { for (var a = 0; a < adj.size(); a++) {
if (this.cells[i][j].candy.name.equals(adj.get(a).candy.name)) { if (this.cells[i][j].candy.name.equals(adj.get(a).candy.name)) {
return true; return true;
} }
@ -132,21 +132,21 @@ public class CandyGame {
} }
void round(int timeSoFar, int totalTime) { void round(int timeSoFar, int totalTime) {
long start = System.currentTimeMillis(); var start = System.currentTimeMillis();
long end = System.currentTimeMillis(); var end = System.currentTimeMillis();
while (end - start + timeSoFar < totalTime && continueRound()) { while (end - start + timeSoFar < totalTime && continueRound()) {
for (int i = 0; i < this.cells.length; i++) { for (var i = 0; i < this.cells.length; i++) {
int points = 0; var points = 0;
int j = this.cells.length - 1; var j = this.cells.length - 1;
while (this.cells[j][i].candy.getType().equals(Type.rewardFruit)) { while (this.cells[j][i].candy.getType().equals(Type.rewardFruit)) {
points = this.cells[j][i].candy.getPoints(); points = this.cells[j][i].candy.getPoints();
this.cells[j][i].crush(pool, this.cells); this.cells[j][i].crush(pool, this.cells);
handleChange(points); handleChange(points);
} }
} }
for (int i = 0; i < this.cells.length; i++) { for (var i = 0; i < this.cells.length; i++) {
int j = cells.length - 1; var j = cells.length - 1;
int points = 0; var points = 0;
while (j > 0) { while (j > 0) {
points = this.cells[j][i].interact(this.cells[j - 1][i], this.pool, this.cells); points = this.cells[j][i].interact(this.cells[j - 1][i], this.pool, this.cells);
if (points != 0) { if (points != 0) {
@ -156,9 +156,9 @@ public class CandyGame {
} }
} }
} }
for (int i = 0; i < this.cells.length; i++) { for (var i = 0; i < this.cells.length; i++) {
int j = 0; var j = 0;
int points = 0; var points = 0;
while (j < cells.length - 1) { while (j < cells.length - 1) {
points = this.cells[i][j].interact(this.cells[i][j + 1], this.pool, this.cells); points = this.cells[i][j].interact(this.cells[i][j + 1], this.pool, this.cells);
if (points != 0) { if (points != 0) {

View File

@ -53,11 +53,11 @@ public class Cell {
} }
void fillThisSpace(CellPool pool, Cell[][] cellMatrix) { void fillThisSpace(CellPool pool, Cell[][] cellMatrix) {
for (int y = this.yIndex; y > 0; y--) { for (var y = this.yIndex; y > 0; y--) {
cellMatrix[y][this.xIndex] = cellMatrix[y - 1][this.xIndex]; cellMatrix[y][this.xIndex] = cellMatrix[y - 1][this.xIndex];
cellMatrix[y][this.xIndex].yIndex = y; cellMatrix[y][this.xIndex].yIndex = y;
} }
Cell newC = pool.getNewCell(); var newC = pool.getNewCell();
cellMatrix[0][this.xIndex] = newC; cellMatrix[0][this.xIndex] = newC;
cellMatrix[0][this.xIndex].xIndex = this.xIndex; cellMatrix[0][this.xIndex].xIndex = this.xIndex;
cellMatrix[0][this.xIndex].yIndex = 0; cellMatrix[0][this.xIndex].yIndex = 0;
@ -78,7 +78,7 @@ public class Cell {
return 0; return 0;
} else { } else {
if (this.candy.name.equals(c.candy.name)) { if (this.candy.name.equals(c.candy.name)) {
int pointsWon = this.candy.getPoints() + c.candy.getPoints(); var pointsWon = this.candy.getPoints() + c.candy.getPoints();
handleCrush(c,pool,cellMatrix); handleCrush(c,pool,cellMatrix);
return pointsWon; return pointsWon;
} else { } else {

View File

@ -57,7 +57,7 @@ public class CellPool {
randomCode[4] = new Candy("orange gum", "candy", Type.crushableCandy, 10); randomCode[4] = new Candy("orange gum", "candy", Type.crushableCandy, 10);
} }
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
Cell c = new Cell(); var c = new Cell();
c.candy = randomCode[RANDOM.nextInt(randomCode.length)]; c.candy = randomCode[RANDOM.nextInt(randomCode.length)];
this.pool.add(c); this.pool.add(c);
} }
@ -65,7 +65,7 @@ public class CellPool {
} }
Cell getNewCell() { Cell getNewCell() {
Cell newCell = this.pool.remove(pointer); var newCell = this.pool.remove(pointer);
pointer--; pointer--;
return newCell; return newCell;
} }
@ -77,12 +77,12 @@ public class CellPool {
} }
Candy[] assignRandomCandytypes() throws FileNotFoundException, IOException, ParseException { Candy[] assignRandomCandytypes() throws FileNotFoundException, IOException, ParseException {
JsonParser jp = new JsonParser(); var jp = new JsonParser();
jp.parse(); jp.parse();
Candy[] randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy' var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
int i = 0; var i = 0;
for (Enumeration<String> e = jp.candies.keys(); e.hasMoreElements();) { for (var e = jp.candies.keys(); e.hasMoreElements();) {
String s = e.nextElement(); var s = e.nextElement();
if (!s.equals("fruit") && !s.equals("candy")) { if (!s.equals("fruit") && !s.equals("candy")) {
//not generic //not generic
randomCode[i] = jp.candies.get(s); randomCode[i] = jp.candies.get(s);

View File

@ -47,23 +47,23 @@ public class JsonParser {
} }
void parse() throws FileNotFoundException, IOException, ParseException { void parse() throws FileNotFoundException, IOException, ParseException {
JSONParser parser = new JSONParser(); var parser = new JSONParser();
JSONObject jo = (JSONObject) parser.parse(new FileReader(new File("").getAbsolutePath() var jo = (JSONObject) parser.parse(new FileReader(new File("").getAbsolutePath()
+ "\\src\\main\\java\\com\\iluwatar\\typeobject\\candy.json")); + "\\src\\main\\java\\com\\iluwatar\\typeobject\\candy.json"));
JSONArray a = (JSONArray) jo.get("candies"); var a = (JSONArray) jo.get("candies");
for (Object o : a) { for (var o : a) {
JSONObject candy = (JSONObject) o; var candy = (JSONObject) o;
String name = (String) candy.get("name"); var name = (String) candy.get("name");
String parentName = (String) candy.get("parent"); var parentName = (String) candy.get("parent");
String t = (String) candy.get("type"); var t = (String) candy.get("type");
Type type = null; Type type = null;
if (t.equals("rewardFruit")) { if (t.equals("rewardFruit")) {
type = Type.rewardFruit; type = Type.rewardFruit;
} else { } else {
type = Type.crushableCandy; type = Type.crushableCandy;
} }
int points = Integer.parseInt((String) candy.get("points")); var points = Integer.parseInt((String) candy.get("points"));
Candy c = new Candy(name, parentName, type, points); var c = new Candy(name, parentName, type, points);
this.candies.put(name, c); this.candies.put(name, c);
} }
setParentAndPoints(); setParentAndPoints();
@ -71,7 +71,7 @@ public class JsonParser {
void setParentAndPoints() { void setParentAndPoints() {
for (Enumeration<String> e = this.candies.keys(); e.hasMoreElements();) { for (Enumeration<String> e = this.candies.keys(); e.hasMoreElements();) {
Candy c = this.candies.get(e.nextElement()); var c = this.candies.get(e.nextElement());
if (c.parentName == null) { if (c.parentName == null) {
c.parent = null; c.parent = null;
} else { } else {

View File

@ -35,33 +35,33 @@ class CandyGameTest {
@Test @Test
void adjacentCellsTest() { void adjacentCellsTest() {
CandyGame cg = new CandyGame(3,new CellPool(9)); var cg = new CandyGame(3,new CellPool(9));
ArrayList<Cell> arr1 = cg.adjacentCells(0, 0); var arr1 = cg.adjacentCells(0, 0);
ArrayList<Cell> arr2 = cg.adjacentCells(1, 2); var arr2 = cg.adjacentCells(1, 2);
ArrayList<Cell> arr3 = cg.adjacentCells(1, 1); var arr3 = cg.adjacentCells(1, 1);
assertTrue(arr1.size() == 2 && arr2.size() == 3 && arr3.size() == 4); assertTrue(arr1.size() == 2 && arr2.size() == 3 && arr3.size() == 4);
} }
@Test @Test
void continueRoundTest() { void continueRoundTest() {
Cell[][] matrix = new Cell[2][2]; var matrix = new Cell[2][2];
Candy c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5); var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
Candy c2 = new Candy("purple jelly", "jelly", Type.crushableCandy, 5); var c2 = new Candy("purple jelly", "jelly", Type.crushableCandy, 5);
Candy c3 = new Candy("green apple", "apple", Type.rewardFruit, 10); var c3 = new Candy("green apple", "apple", Type.rewardFruit, 10);
matrix[0][0] = new Cell(c1,0,0);; matrix[0][0] = new Cell(c1,0,0);;
matrix[0][1] = new Cell(c2,1,0); matrix[0][1] = new Cell(c2,1,0);
matrix[1][0] = new Cell(c3,0,1); matrix[1][0] = new Cell(c3,0,1);
matrix[1][1] = new Cell(c2,1,1); matrix[1][1] = new Cell(c2,1,1);
CellPool p = new CellPool(4); var p = new CellPool(4);
CandyGame cg = new CandyGame(2,p); var cg = new CandyGame(2,p);
cg.cells = matrix; cg.cells = matrix;
boolean fruitInLastRow = cg.continueRound(); var fruitInLastRow = cg.continueRound();
matrix[1][0].crush(p, matrix); matrix[1][0].crush(p, matrix);
matrix[0][0] = new Cell(c3,0,0); matrix[0][0] = new Cell(c3,0,0);
boolean matchingCandy = cg.continueRound(); var matchingCandy = cg.continueRound();
matrix[0][1].crush(p,matrix); matrix[0][1].crush(p,matrix);
matrix[0][1] = new Cell(c3,1,0); matrix[0][1] = new Cell(c3,1,0);
boolean noneLeft = cg.continueRound(); var noneLeft = cg.continueRound();
assertTrue(fruitInLastRow && matchingCandy && !noneLeft); assertTrue(fruitInLastRow && matchingCandy && !noneLeft);
} }

View File

@ -34,10 +34,10 @@ class CellPoolTest {
@Test @Test
void assignRandomCandyTypesTest() { void assignRandomCandyTypesTest() {
CellPool cp = new CellPool(10); var cp = new CellPool(10);
Hashtable<String, Boolean> ht = new Hashtable<String, Boolean>(); var ht = new Hashtable<String, Boolean>();
int parentTypes = 0; var parentTypes = 0;
for (int i = 0; i < cp.randomCode.length; i++) { for (var i = 0; i < cp.randomCode.length; i++) {
if (ht.get(cp.randomCode[i].name) == null) { if (ht.get(cp.randomCode[i].name) == null) {
ht.put(cp.randomCode[i].name, true); ht.put(cp.randomCode[i].name, true);
} }

View File

@ -34,24 +34,24 @@ class CellTest {
@Test @Test
void interactTest() { void interactTest() {
Candy c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5); var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
Candy c2 = new Candy("green apple", "apple", Type.rewardFruit, 10); var c2 = new Candy("green apple", "apple", Type.rewardFruit, 10);
Cell[][] matrix = new Cell[4][4]; var matrix = new Cell[4][4];
matrix[0][0] = new Cell(c1,0,0); matrix[0][0] = new Cell(c1,0,0);
matrix[0][1] = new Cell(c1,1,0); matrix[0][1] = new Cell(c1,1,0);
matrix[0][2] = new Cell(c2,2,0); matrix[0][2] = new Cell(c2,2,0);
matrix[0][3] = new Cell(c1,3,0); matrix[0][3] = new Cell(c1,3,0);
CellPool cp = new CellPool(5); var cp = new CellPool(5);
int points1 = matrix[0][0].interact(matrix[0][1], cp, matrix); var points1 = matrix[0][0].interact(matrix[0][1], cp, matrix);
int points2 = matrix[0][2].interact(matrix[0][3], cp, matrix); var points2 = matrix[0][2].interact(matrix[0][3], cp, matrix);
assertTrue(points1 > 0 && points2 == 0); assertTrue(points1 > 0 && points2 == 0);
} }
@Test @Test
void crushTest() { void crushTest() {
Candy c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5); var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
Candy c2 = new Candy("purple candy", "candy", Type.crushableCandy, 5); var c2 = new Candy("purple candy", "candy", Type.crushableCandy, 5);
Cell[][] matrix = new Cell[4][4]; var matrix = new Cell[4][4];
matrix[0][0] = new Cell(c1,0,0); matrix[0][0] = new Cell(c1,0,0);
matrix[1][0] = new Cell(c2,0,1); matrix[1][0] = new Cell(c2,0,1);
matrix[1][0].crush(new CellPool(5), matrix); matrix[1][0].crush(new CellPool(5), matrix);

View File

@ -34,13 +34,13 @@ public class App {
* @param args no argument sent * @param args no argument sent
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Student ram = new Student(1, "Ram", "Street 9, Cupertino"); var ram = new Student(1, "Ram", "Street 9, Cupertino");
Student shyam = new Student(2, "Shyam", "Z bridge, Pune"); var shyam = new Student(2, "Shyam", "Z bridge, Pune");
Student gopi = new Student(3, "Gopi", "Street 10, Mumbai"); var gopi = new Student(3, "Gopi", "Street 10, Mumbai");
HashMap<String, List<Student>> context = new HashMap<>(); HashMap<String, List<Student>> context = new HashMap<>();
StudentDatabase studentDatabase = new StudentDatabase(); var studentDatabase = new StudentDatabase();
StudentRepository studentRepository = new StudentRepository(context, studentDatabase); var studentRepository = new StudentRepository(context, studentDatabase);
studentRepository.registerNew(ram); studentRepository.registerNew(ram);
studentRepository.registerModified(shyam); studentRepository.registerModified(shyam);

View File

@ -68,7 +68,7 @@ public class StudentRepository implements IUnitOfWork<Student> {
} }
private void register(Student student, String operation) { private void register(Student student, String operation) {
List<Student> studentsToOperate = context.get(operation); var studentsToOperate = context.get(operation);
if (studentsToOperate == null) { if (studentsToOperate == null) {
studentsToOperate = new ArrayList<>(); studentsToOperate = new ArrayList<>();
} }
@ -99,24 +99,24 @@ public class StudentRepository implements IUnitOfWork<Student> {
} }
private void commitInsert() { private void commitInsert() {
List<Student> studentsToBeInserted = context.get(IUnitOfWork.INSERT); var studentsToBeInserted = context.get(IUnitOfWork.INSERT);
for (Student student : studentsToBeInserted) { for (var student : studentsToBeInserted) {
LOGGER.info("Saving {} to database.", student.getName()); LOGGER.info("Saving {} to database.", student.getName());
studentDatabase.insert(student); studentDatabase.insert(student);
} }
} }
private void commitModify() { private void commitModify() {
List<Student> modifiedStudents = context.get(IUnitOfWork.MODIFY); var modifiedStudents = context.get(IUnitOfWork.MODIFY);
for (Student student : modifiedStudents) { for (var student : modifiedStudents) {
LOGGER.info("Modifying {} to database.", student.getName()); LOGGER.info("Modifying {} to database.", student.getName());
studentDatabase.modify(student); studentDatabase.modify(student);
} }
} }
private void commitDelete() { private void commitDelete() {
List<Student> deletedStudents = context.get(IUnitOfWork.DELETE); var deletedStudents = context.get(IUnitOfWork.DELETE);
for (Student student : deletedStudents) { for (var student : deletedStudents) {
LOGGER.info("Deleting {} to database.", student.getName()); LOGGER.info("Deleting {} to database.", student.getName());
studentDatabase.delete(student); studentDatabase.delete(student);
} }

View File

@ -97,7 +97,7 @@ public class StudentRepositoryTest {
@Test @Test
public void shouldNotWriteToDbIfContextIsNull() { public void shouldNotWriteToDbIfContextIsNull() {
StudentRepository studentRepository = new StudentRepository(null, studentDatabase); var studentRepository = new StudentRepository(null, studentDatabase);
studentRepository.commit(); studentRepository.commit();
@ -106,7 +106,7 @@ public class StudentRepositoryTest {
@Test @Test
public void shouldNotWriteToDbIfNothingToCommit() { public void shouldNotWriteToDbIfNothingToCommit() {
StudentRepository studentRepository = new StudentRepository(new HashMap<>(), studentDatabase); var studentRepository = new StudentRepository(new HashMap<>(), studentDatabase);
studentRepository.commit(); studentRepository.commit();

View File

@ -48,9 +48,9 @@ public class App {
* This practice creates three HeroStats(Value object) and checks equality between those. * This practice creates three HeroStats(Value object) and checks equality between those.
*/ */
public static void main(String[] args) { public static void main(String[] args) {
HeroStat statA = HeroStat.valueOf(10, 5, 0); var statA = HeroStat.valueOf(10, 5, 0);
HeroStat statB = HeroStat.valueOf(10, 5, 0); var statB = HeroStat.valueOf(10, 5, 0);
HeroStat statC = HeroStat.valueOf(5, 1, 8); var statC = HeroStat.valueOf(5, 1, 8);
LOGGER.info(statA.toString()); LOGGER.info(statA.toString());

View File

@ -76,8 +76,8 @@ public class HeroStat {
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final var prime = 31;
int result = 1; var result = 1;
result = prime * result + intelligence; result = prime * result + intelligence;
result = prime * result + luck; result = prime * result + luck;
result = prime * result + strength; result = prime * result + strength;
@ -95,7 +95,7 @@ public class HeroStat {
if (getClass() != obj.getClass()) { if (getClass() != obj.getClass()) {
return false; return false;
} }
HeroStat other = (HeroStat) obj; var other = (HeroStat) obj;
if (intelligence != other.intelligence) { if (intelligence != other.intelligence) {
return false; return false;
} }

View File

@ -45,8 +45,8 @@ public class HeroStatTest {
*/ */
@Test @Test
public void testEquals() { public void testEquals() {
HeroStat heroStatA = HeroStat.valueOf(3, 9, 2); var heroStatA = HeroStat.valueOf(3, 9, 2);
HeroStat heroStatB = HeroStat.valueOf(3, 9, 2); var heroStatB = HeroStat.valueOf(3, 9, 2);
new EqualsTester().addEqualityGroup(heroStatA, heroStatB).testEquals(); new EqualsTester().addEqualityGroup(heroStatA, heroStatB).testEquals();
} }
@ -56,9 +56,9 @@ public class HeroStatTest {
*/ */
@Test @Test
public void testToString() { public void testToString() {
HeroStat heroStatA = HeroStat.valueOf(3, 9, 2); var heroStatA = HeroStat.valueOf(3, 9, 2);
HeroStat heroStatB = HeroStat.valueOf(3, 9, 2); var heroStatB = HeroStat.valueOf(3, 9, 2);
HeroStat heroStatC = HeroStat.valueOf(3, 9, 8); var heroStatC = HeroStat.valueOf(3, 9, 8);
assertThat(heroStatA.toString(), is(heroStatB.toString())); assertThat(heroStatA.toString(), is(heroStatB.toString()));
assertThat(heroStatA.toString(), is(not(heroStatC.toString()))); assertThat(heroStatA.toString(), is(not(heroStatC.toString())));

View File

@ -41,7 +41,7 @@ public class App {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Commander commander = var commander =
new Commander(new Sergeant(new Soldier(), new Soldier(), new Soldier()), new Sergeant( new Commander(new Sergeant(new Soldier(), new Soldier(), new Soldier()), new Sergeant(
new Soldier(), new Soldier(), new Soldier())); new Soldier(), new Soldier(), new Soldier()));
commander.accept(new SoldierVisitor()); commander.accept(new SoldierVisitor());

View File

@ -39,7 +39,7 @@ public abstract class Unit {
* Accept visitor * Accept visitor
*/ */
public void accept(UnitVisitor visitor) { public void accept(UnitVisitor visitor) {
for (Unit child : children) { for (var child : children) {
child.accept(visitor); child.accept(visitor);
} }
} }

View File

@ -56,15 +56,15 @@ public abstract class UnitTest<U extends Unit> {
@Test @Test
public void testAccept() { public void testAccept() {
final Unit[] children = new Unit[5]; final var children = new Unit[5];
Arrays.setAll(children, (i) -> mock(Unit.class)); Arrays.setAll(children, (i) -> mock(Unit.class));
final U unit = this.factory.apply(children); final var unit = this.factory.apply(children);
final UnitVisitor visitor = mock(UnitVisitor.class); final var visitor = mock(UnitVisitor.class);
unit.accept(visitor); unit.accept(visitor);
verifyVisit(unit, visitor); verifyVisit(unit, visitor);
for (final Unit child : children) { for (final var child : children) {
verify(child).accept(eq(visitor)); verify(child).accept(eq(visitor));
} }