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
@@ -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 {
@@ -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) {
@@ -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;