Skip to content

Commit 888af23

Browse files
authored
Merge pull request iluwatar#1220 from anuragagarwal561994/java-11
Java 11 (patterns with m)
2 parents cd20e7a + a709560 commit 888af23

File tree

74 files changed

+537
-544
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+537
-544
lines changed

marker/src/main/java/App.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@ public class App {
4646
* @param args command line args
4747
*/
4848
public static void main(String[] args) {
49+
final var logger = LoggerFactory.getLogger(App.class);
50+
var guard = new Guard();
51+
var thief = new Thief();
4952

50-
final Logger logger = LoggerFactory.getLogger(App.class);
51-
Guard guard = new Guard();
52-
Thief thief = new Thief();
53-
53+
//noinspection ConstantConditions
5454
if (guard instanceof Permission) {
5555
guard.enter();
5656
} else {
5757
logger.info("You have no permission to enter, please leave this area");
5858
}
5959

60+
//noinspection ConstantConditions
6061
if (thief instanceof Permission) {
6162
thief.steal();
6263
} else {

marker/src/main/java/Guard.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
* Class defining Guard.
2929
*/
3030
public class Guard implements Permission {
31-
3231
private static final Logger LOGGER = LoggerFactory.getLogger(Guard.class);
3332

34-
protected static void enter() {
35-
33+
protected void enter() {
3634
LOGGER.info("You can enter");
3735
}
3836
}

marker/src/main/java/Thief.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@
2828
* Class defining Thief.
2929
*/
3030
public class Thief {
31-
3231
private static final Logger LOGGER = LoggerFactory.getLogger(Thief.class);
3332

34-
protected static void steal() {
33+
protected void steal() {
3534
LOGGER.info("Steal valuable items");
3635
}
3736

38-
protected static void doNothing() {
37+
protected void doNothing() {
3938
LOGGER.info("Pretend nothing happened and just leave");
4039
}
4140
}

marker/src/test/java/AppTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public class AppTest {
3030

3131
@Test
3232
public void test() {
33-
String[] args = {};
34-
App.main(args);
33+
App.main(new String[]{});
3534
}
3635
}

marker/src/test/java/GuardTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class GuardTest {
3333

3434
@Test
3535
public void testGuard() {
36-
Guard guard = new Guard();
36+
var guard = new Guard();
3737
assertThat(guard, instanceOf(Permission.class));
3838
}
3939
}

marker/src/test/java/ThiefTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
import org.junit.jupiter.api.Test;
24+
import static org.hamcrest.CoreMatchers.instanceOf;
25+
import static org.hamcrest.CoreMatchers.not;
26+
import static org.hamcrest.MatcherAssert.assertThat;
2527

26-
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
import org.junit.jupiter.api.Test;
2729

2830
/**
2931
* Thief test
3032
*/
3133
public class ThiefTest {
3234
@Test
3335
public void testThief() {
34-
Thief thief = new Thief();
35-
assertFalse(thief instanceof Permission);
36+
var thief = new Thief();
37+
assertThat(thief, not(instanceOf(Permission.class)));
3638
}
3739
}

master-worker-pattern/pom.xml

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,39 @@
2222
THE SOFTWARE.
2323
2424
-->
25-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
26-
<modelVersion>4.0.0</modelVersion>
27-
<parent>
28-
<groupId>com.iluwatar</groupId>
29-
<artifactId>java-design-patterns</artifactId>
30-
<version>1.23.0-SNAPSHOT</version>
31-
</parent>
32-
<artifactId>master-worker-pattern</artifactId>
33-
<dependencies>
34-
<dependency>
35-
<groupId>org.junit.jupiter</groupId>
36-
<artifactId>junit-jupiter-engine</artifactId>
37-
<scope>test</scope>
38-
</dependency>
25+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
27+
<modelVersion>4.0.0</modelVersion>
28+
<parent>
29+
<groupId>com.iluwatar</groupId>
30+
<artifactId>java-design-patterns</artifactId>
31+
<version>1.23.0-SNAPSHOT</version>
32+
</parent>
33+
<artifactId>master-worker-pattern</artifactId>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<scope>test</scope>
39+
</dependency>
3940
</dependencies>
40-
<build>
41-
<plugins>
42-
<plugin>
43-
<groupId>org.apache.maven.plugins</groupId>
44-
<artifactId>maven-assembly-plugin</artifactId>
45-
<executions>
46-
<execution>
47-
<configuration>
48-
<archive>
49-
<manifest>
50-
<mainClass>com.iluwatar.masterworker.App</mainClass>
51-
</manifest>
52-
</archive>
53-
</configuration>
54-
</execution>
55-
</executions>
56-
</plugin>
57-
</plugins>
58-
</build>
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-assembly-plugin</artifactId>
46+
<executions>
47+
<execution>
48+
<configuration>
49+
<archive>
50+
<manifest>
51+
<mainClass>com.iluwatar.masterworker.App</mainClass>
52+
</manifest>
53+
</archive>
54+
</configuration>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
</plugins>
59+
</build>
5960
</project>

master-worker-pattern/src/main/java/com/iluwatar/masterworker/App.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,25 @@
3434

3535
/**
3636
* <p>The <b><em>Master-Worker</em></b> pattern is used when the problem at hand can be solved by
37-
* dividing into
38-
* multiple parts which need to go through the same computation and may need to be aggregated to get
39-
* final result. Parallel processing is performed using a system consisting of a master and some
40-
* number of workers, where a master divides the work among the workers, gets the result back from
41-
* them and assimilates all the results to give final result. The only communication is between the
42-
* master and the worker - none of the workers communicate among one another and the user only
43-
* communicates with the master to get required job done.</p>
37+
* dividing into multiple parts which need to go through the same computation and may need to be
38+
* aggregated to get final result. Parallel processing is performed using a system consisting of a
39+
* master and some number of workers, where a master divides the work among the workers, gets the
40+
* result back from them and assimilates all the results to give final result. The only
41+
* communication is between the master and the worker - none of the workers communicate among one
42+
* another and the user only communicates with the master to get required job done.</p>
4443
* <p>In our example, we have generic abstract classes {@link MasterWorker}, {@link Master} and
45-
* {@link Worker} which
46-
* have to be extended by the classes which will perform the specific job at hand (in this case
47-
* finding transpose of matrix, done by {@link ArrayTransposeMasterWorker}, {@link
48-
* ArrayTransposeMaster} and {@link ArrayTransposeWorker}). The Master class divides the work into
49-
* parts to be given to the workers, collects the results from the workers and aggregates it when
50-
* all workers have responded before returning the solution. The Worker class extends the Thread
51-
* class to enable parallel processing, and does the work once the data has been received from the
52-
* Master. The MasterWorker contains a reference to the Master class, gets the input from the App
53-
* and passes it on to the Master. These 3 classes define the system which computes the result. We
54-
* also have 2 abstract classes {@link Input} and {@link Result}, which contain the input data and
55-
* result data respectively. The Input class also has an abstract method divideData which defines
56-
* how the data is to be divided into segments. These classes are extended by {@link ArrayInput} and
57-
* {@link ArrayResult}.</p>
44+
* {@link Worker} which have to be extended by the classes which will perform the specific job at
45+
* hand (in this case finding transpose of matrix, done by {@link ArrayTransposeMasterWorker},
46+
* {@link ArrayTransposeMaster} and {@link ArrayTransposeWorker}). The Master class divides the work
47+
* into parts to be given to the workers, collects the results from the workers and aggregates it
48+
* when all workers have responded before returning the solution. The Worker class extends the
49+
* Thread class to enable parallel processing, and does the work once the data has been received
50+
* from the Master. The MasterWorker contains a reference to the Master class, gets the input from
51+
* the App and passes it on to the Master. These 3 classes define the system which computes the
52+
* result. We also have 2 abstract classes {@link Input} and {@link Result}, which contain the input
53+
* data and result data respectively. The Input class also has an abstract method divideData which
54+
* defines how the data is to be divided into segments. These classes are extended by {@link
55+
* ArrayInput} and {@link ArrayResult}.</p>
5856
*/
5957

6058
public class App {
@@ -68,12 +66,12 @@ public class App {
6866
*/
6967

7068
public static void main(String[] args) {
71-
ArrayTransposeMasterWorker mw = new ArrayTransposeMasterWorker();
72-
int rows = 10;
73-
int columns = 20;
74-
int[][] inputMatrix = ArrayUtilityMethods.createRandomIntMatrix(rows, columns);
75-
ArrayInput input = new ArrayInput(inputMatrix);
76-
ArrayResult result = (ArrayResult) mw.getResult(input);
69+
var mw = new ArrayTransposeMasterWorker();
70+
var rows = 10;
71+
var columns = 20;
72+
var inputMatrix = ArrayUtilityMethods.createRandomIntMatrix(rows, columns);
73+
var input = new ArrayInput(inputMatrix);
74+
var result = (ArrayResult) mw.getResult(input);
7775
if (result != null) {
7876
ArrayUtilityMethods.printMatrix(inputMatrix);
7977
ArrayUtilityMethods.printMatrix(result.data);

master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayInput.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.ArrayList;
2727
import java.util.Arrays;
28+
import java.util.List;
2829

2930
/**
3031
* Class ArrayInput extends abstract class {@link Input} and contains data of type int[][].
@@ -37,12 +38,12 @@ public ArrayInput(int[][] data) {
3738
}
3839

3940
static int[] makeDivisions(int[][] data, int num) {
40-
int initialDivision = data.length / num; //equally dividing
41-
int[] divisions = new int[num];
41+
var initialDivision = data.length / num; //equally dividing
42+
var divisions = new int[num];
4243
Arrays.fill(divisions, initialDivision);
4344
if (initialDivision * num != data.length) {
44-
int extra = data.length - initialDivision * num;
45-
int l = 0;
45+
var extra = data.length - initialDivision * num;
46+
var l = 0;
4647
//equally dividing extra among all parts
4748
while (extra > 0) {
4849
divisions[l] = divisions[l] + 1;
@@ -58,22 +59,20 @@ static int[] makeDivisions(int[][] data, int num) {
5859
}
5960

6061
@Override
61-
public ArrayList<Input> divideData(int num) {
62+
public List<Input<int[][]>> divideData(int num) {
6263
if (this.data == null) {
6364
return null;
6465
} else {
65-
int[] divisions = makeDivisions(this.data, num);
66-
ArrayList<Input> result = new ArrayList<Input>(num);
67-
int rowsDone = 0; //number of rows divided so far
68-
for (int i = 0; i < num; i++) {
69-
int rows = divisions[i];
66+
var divisions = makeDivisions(this.data, num);
67+
var result = new ArrayList<Input<int[][]>>(num);
68+
var rowsDone = 0; //number of rows divided so far
69+
for (var i = 0; i < num; i++) {
70+
var rows = divisions[i];
7071
if (rows != 0) {
71-
int[][] divided = new int[rows][this.data[0].length];
72-
for (int j = 0; j < rows; j++) {
73-
divided[j] = this.data[rowsDone + j];
74-
}
72+
var divided = new int[rows][this.data[0].length];
73+
System.arraycopy(this.data, rowsDone, divided, 0, rows);
7574
rowsDone += rows;
76-
ArrayInput dividedInput = new ArrayInput(divided);
75+
var dividedInput = new ArrayInput(divided);
7776
result.add(dividedInput);
7877
} else {
7978
break; //rest of divisions will also be 0

master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayUtilityMethods.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public static boolean arraysSame(int[] a1, int[] a2) {
4747
if (a1.length != a2.length) {
4848
return false;
4949
} else {
50-
boolean answer = false;
51-
for (int i = 0; i < a1.length; i++) {
50+
var answer = false;
51+
for (var i = 0; i < a1.length; i++) {
5252
if (a1[i] == a2[i]) {
5353
answer = true;
5454
} else {
@@ -69,8 +69,8 @@ public static boolean matricesSame(int[][] m1, int[][] m2) {
6969
if (m1.length != m2.length) {
7070
return false;
7171
} else {
72-
boolean answer = false;
73-
for (int i = 0; i < m1.length; i++) {
72+
var answer = false;
73+
for (var i = 0; i < m1.length; i++) {
7474
if (arraysSame(m1[i], m2[i])) {
7575
answer = true;
7676
} else {
@@ -88,9 +88,9 @@ public static boolean matricesSame(int[][] m1, int[][] m2) {
8888
* @return it (int[][]).
8989
*/
9090
public static int[][] createRandomIntMatrix(int rows, int columns) {
91-
int[][] matrix = new int[rows][columns];
92-
for (int i = 0; i < rows; i++) {
93-
for (int j = 0; j < columns; j++) {
91+
var matrix = new int[rows][columns];
92+
for (var i = 0; i < rows; i++) {
93+
for (var j = 0; j < columns; j++) {
9494
//filling cells in matrix
9595
matrix[i][j] = RANDOM.nextInt(10);
9696
}
@@ -104,9 +104,9 @@ public static int[][] createRandomIntMatrix(int rows, int columns) {
104104

105105
public static void printMatrix(int[][] matrix) {
106106
//prints out int[][]
107-
for (int i = 0; i < matrix.length; i++) {
108-
for (int j = 0; j < matrix[0].length; j++) {
109-
LOGGER.info(matrix[i][j] + " ");
107+
for (var ints : matrix) {
108+
for (var j = 0; j < matrix[0].length; j++) {
109+
LOGGER.info(ints[j] + " ");
110110
}
111111
LOGGER.info("");
112112
}

master-worker-pattern/src/main/java/com/iluwatar/masterworker/Input.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
package com.iluwatar.masterworker;
2525

26-
import java.util.ArrayList;
26+
import java.util.List;
2727

2828
/**
2929
* The abstract Input class, having 1 public field which contains input data, and abstract method
@@ -40,5 +40,5 @@ public Input(T data) {
4040
this.data = data;
4141
}
4242

43-
public abstract ArrayList<Input> divideData(int num);
43+
public abstract List<Input<T>> divideData(int num);
4444
}

master-worker-pattern/src/main/java/com/iluwatar/masterworker/system/MasterWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public MasterWorker(int numOfWorkers) {
4040

4141
abstract Master setMaster(int numOfWorkers);
4242

43-
public Result getResult(Input input) {
43+
public Result<?> getResult(Input<?> input) {
4444
this.master.doWork(input);
4545
return this.master.getFinalResult();
4646
}

0 commit comments

Comments
 (0)