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

View File

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

View File

@ -53,11 +53,11 @@ public class Cell {
}
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].yIndex = y;
}
Cell newC = pool.getNewCell();
var newC = pool.getNewCell();
cellMatrix[0][this.xIndex] = newC;
cellMatrix[0][this.xIndex].xIndex = this.xIndex;
cellMatrix[0][this.xIndex].yIndex = 0;
@ -78,7 +78,7 @@ public class Cell {
return 0;
} else {
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);
return pointsWon;
} else {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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