Java 11 migration: patterns (t-v) (#1085)

* Moves visitor pattern to java 11

* Moves value-object pattern to java 11

* Moves unit-of-work pattern to java 11

* Moves typeobjectpattern pattern to java 11

* Moves twin pattern to java 11

* Moves trampoline pattern to java 11

* Moves tolerant-reader pattern to java 11

* Moves tls pattern to java 11

* Moves throttling pattern to java 11

* Moves thread-pool pattern to java 11

* Moves template-method pattern to java 11
This commit is contained in:
Anurag Agarwal
2019-11-14 11:12:05 +05:30
committed by Ilkka Seppälä
parent 160b737dcc
commit 50467c9e76
45 changed files with 379 additions and 422 deletions

View File

@ -25,6 +25,7 @@ package com.iluwatar.typeobject;
import com.iluwatar.typeobject.Candy.Type;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -55,21 +56,17 @@ public class CandyGame {
}
static String numOfSpaces(int num) {
String result = "";
for (var i = 0; i < num; i++) {
result += " ";
}
return result;
return " ".repeat(Math.max(0, num));
}
void printGameStatus() {
LOGGER.info("");
for (var i = 0; i < cells.length; i++) {
for (Cell[] cell : cells) {
for (var j = 0; j < cells.length; j++) {
var candyName = cells[i][j].candy.name;
var candyName = cell[j].candy.name;
if (candyName.length() < 20) {
var totalSpaces = 20 - candyName.length();
LOGGER.info(numOfSpaces(totalSpaces / 2) + cells[i][j].candy.name
LOGGER.info(numOfSpaces(totalSpaces / 2) + cell[j].candy.name
+ numOfSpaces(totalSpaces - totalSpaces / 2) + "|");
} else {
LOGGER.info(candyName + "|");
@ -80,8 +77,8 @@ public class CandyGame {
LOGGER.info("");
}
ArrayList<Cell> adjacentCells(int y, int x) {
ArrayList<Cell> adjacent = new ArrayList<Cell>();
List<Cell> adjacentCells(int y, int x) {
var adjacent = new ArrayList<Cell>();
if (y == 0) {
adjacent.add(this.cells[1][x]);
}
@ -115,8 +112,8 @@ public class CandyGame {
for (var j = 0; j < this.cells.length; j++) {
if (!this.cells[i][j].candy.getType().equals(Type.rewardFruit)) {
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)) {
for (Cell cell : adj) {
if (this.cells[i][j].candy.name.equals(cell.candy.name)) {
return true;
}
}
@ -157,11 +154,11 @@ public class CandyGame {
}
}
}
for (var i = 0; i < this.cells.length; i++) {
for (Cell[] cell : this.cells) {
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);
points = cell[j].interact(cell[j + 1], this.pool, this.cells);
if (points != 0) {
handleChange(points);
} else {

View File

@ -27,6 +27,7 @@ import com.iluwatar.typeobject.Candy.Type;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.json.simple.parser.ParseException;
@ -38,12 +39,12 @@ import org.json.simple.parser.ParseException;
public class CellPool {
private static final Random RANDOM = new Random();
ArrayList<Cell> pool;
List<Cell> pool;
int pointer;
Candy[] randomCode;
CellPool(int num) {
this.pool = new ArrayList<Cell>(num);
this.pool = new ArrayList<>(num);
try {
this.randomCode = assignRandomCandytypes();
} catch (Exception e) {

View File

@ -25,11 +25,11 @@ package com.iluwatar.typeobject;
import com.iluwatar.typeobject.Candy.Type;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.stream.Collectors;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@ -43,24 +43,24 @@ public class JsonParser {
Hashtable<String, Candy> candies;
JsonParser() {
this.candies = new Hashtable<String, Candy>();
this.candies = new Hashtable<>();
}
void parse() throws FileNotFoundException, IOException, ParseException {
void parse() throws IOException, ParseException {
var parser = new JSONParser();
var jo = (JSONObject) parser.parse(new FileReader(new File("").getAbsolutePath()
+ "\\src\\main\\java\\com\\iluwatar\\typeobject\\candy.json"));
var workingDirectory = new File("").getAbsolutePath();
var filePath = List.of("src", "main", "java", "com", "iluwatar", "typeobject", "candy.json");
var absolutePath = workingDirectory + File.separator + String.join(File.separator, filePath);
var jo = (JSONObject) parser.parse(new FileReader(absolutePath));
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;
var type = Type.crushableCandy;
if (t.equals("rewardFruit")) {
type = Type.rewardFruit;
} else {
type = Type.crushableCandy;
}
var points = Integer.parseInt((String) candy.get("points"));
var c = new Candy(name, parentName, type, points);
@ -70,7 +70,7 @@ public class JsonParser {
}
void setParentAndPoints() {
for (Enumeration<String> e = this.candies.keys(); e.hasMoreElements(); ) {
for (var e = this.candies.keys(); e.hasMoreElements(); ) {
var c = this.candies.get(e.nextElement());
if (c.parentName == null) {
c.parent = null;

View File

@ -23,10 +23,10 @@
package com.iluwatar.typeobject;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.iluwatar.typeobject.Candy.Type;
import org.junit.jupiter.api.Test;
/**
* The CandyGameTest class tests the methods in the {@link CandyGame} class.
@ -36,7 +36,7 @@ class CandyGameTest {
@Test
void adjacentCellsTest() {
var cg = new CandyGame(3,new CellPool(9));
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);
@ -49,19 +49,19 @@ class CandyGameTest {
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);
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);
var p = new CellPool(4);
var cg = new CandyGame(2,p);
var cg = new CandyGame(2, p);
cg.cells = matrix;
var fruitInLastRow = cg.continueRound();
matrix[1][0].crush(p, matrix);
matrix[0][0] = new Cell(c3,0,0);
var matchingCandy = cg.continueRound();
matrix[0][1].crush(p,matrix);
matrix[0][1] = new Cell(c3,1,0);
matrix[0][0] = new Cell(c3, 0, 0);
var matchingCandy = cg.continueRound();
matrix[0][1].crush(p, matrix);
matrix[0][1] = new Cell(c3, 1, 0);
var noneLeft = cg.continueRound();
assertTrue(fruitInLastRow && matchingCandy && !noneLeft);
}

View File

@ -23,9 +23,10 @@
package com.iluwatar.typeobject;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Hashtable;
import org.junit.jupiter.api.Test;
/**
* The CellPoolTest class tests the methods in the {@link CellPool} class.
@ -39,9 +40,7 @@ class CellPoolTest {
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);
}
ht.putIfAbsent(cp.randomCode[i].name, true);
if (cp.randomCode[i].name.equals("fruit") || cp.randomCode[i].name.equals("candy")) {
parentTypes++;
}

View File

@ -23,14 +23,15 @@
package com.iluwatar.typeobject;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.iluwatar.typeobject.Candy.Type;
import org.junit.jupiter.api.Test;
/**
* The CellTest class tests the methods in the {@link Cell} class.
*/
class CellTest {
@Test
@ -38,10 +39,10 @@ class CellTest {
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);
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);
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);
@ -53,9 +54,9 @@ class CellTest {
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[0][0] = new Cell(c1, 0, 0);
matrix[1][0] = new Cell(c2, 0, 1);
matrix[1][0].crush(new CellPool(5), matrix);
assertTrue(matrix[1][0].candy.name.equals("green jelly"));
assertEquals("green jelly", matrix[1][0].candy.name);
}
}