Skip to content

Commit

Permalink
Resolves checkstyle errors for remaining m (#1090)
Browse files Browse the repository at this point in the history
* Reduces checkstyle errors in marker

* Reduces checkstyle errors in master-worker-pattern

* Reduces checkstyle errors in mediator

* Reduces checkstyle errors in memento

* Reduces checkstyle errors in model-view-controller

* Reduces checkstyle errors in model-view-presenter

* Reduces checkstyle errors in module

* Reduces checkstyle errors in monad

* Reduces checkstyle errors in monostate

* Reduces checkstyle errors in multiton

* Reduces checkstyle errors in mute-idiom

* Reduces checkstyle errors in mutex
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 16, 2019
1 parent 3ccc9ba commit 1fdc650
Show file tree
Hide file tree
Showing 66 changed files with 374 additions and 423 deletions.
30 changes: 13 additions & 17 deletions marker/src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,23 @@
import org.slf4j.LoggerFactory;

/**
* Created by Alexis on 28-Apr-17.
* With Marker interface idea is to make empty interface and extend it.
* Basically it is just to identify the special objects from normal objects.
* Like in case of serialization , objects that need to be serialized must implement serializable interface
* (it is empty interface) and down the line writeObject() method must be checking
* if it is a instance of serializable or not.
* <p>
* Marker interface vs annotation
* Marker interfaces and marker annotations both have their uses,
* neither of them is obsolete or always better then the other one.
* If you want to define a type that does not have any new methods associated with it,
* a marker interface is the way to go.
* If you want to mark program elements other than classes and interfaces,
* to allow for the possibility of adding more information to the marker in the future,
* or to fit the marker into a framework that already makes heavy use of annotation types,
* then a marker annotation is the correct choice
* Created by Alexis on 28-Apr-17. With Marker interface idea is to make empty interface and extend
* it. Basically it is just to identify the special objects from normal objects. Like in case of
* serialization , objects that need to be serialized must implement serializable interface (it is
* empty interface) and down the line writeObject() method must be checking if it is a instance of
* serializable or not.
*
* <p>Marker interface vs annotation Marker interfaces and marker annotations both have their uses,
* neither of them is obsolete or always better then the other one. If you want to define a type
* that does not have any new methods associated with it, a marker interface is the way to go. If
* you want to mark program elements other than classes and interfaces, to allow for the possibility
* of adding more information to the marker in the future, or to fit the marker into a framework
* that already makes heavy use of annotation types, then a marker annotation is the correct choice
*/
public class App {

/**
* Program entry point
* Program entry point.
*
* @param args command line args
*/
Expand Down
2 changes: 1 addition & 1 deletion marker/src/main/java/Guard.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.slf4j.LoggerFactory;

/**
* Class defining Guard
* Class defining Guard.
*/
public class Guard implements Permission {

Expand Down
3 changes: 1 addition & 2 deletions marker/src/main/java/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
*/

/**
* Interface without any methods
* Marker interface is based on that assumption
* Interface without any methods Marker interface is based on that assumption.
*/
public interface Permission {
}
2 changes: 1 addition & 1 deletion marker/src/main/java/Thief.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.slf4j.LoggerFactory;

/**
* Class defining Thief
* Class defining Thief.
*/
public class Thief {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,47 @@
import org.slf4j.LoggerFactory;

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

public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* Program entry point.
*
* @param args command line args
*/

public static void main(String[] args) {
ArrayTransposeMasterWorker mw = new ArrayTransposeMasterWorker();
int rows = 10;
int columns = 20;
int[][] inputMatrix = ArrayUtilityMethods.createRandomIntMatrix(rows,columns);
int[][] inputMatrix = ArrayUtilityMethods.createRandomIntMatrix(rows, columns);
ArrayInput input = new ArrayInput(inputMatrix);
ArrayResult result = (ArrayResult) mw.getResult(input);
ArrayResult result = (ArrayResult) mw.getResult(input);
if (result != null) {
ArrayUtilityMethods.printMatrix(inputMatrix);
ArrayUtilityMethods.printMatrix(result.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@
import java.util.Arrays;

/**
*Class ArrayInput extends abstract class {@link Input} and contains data
*of type int[][].
* Class ArrayInput extends abstract class {@link Input} and contains data of type int[][].
*/

public class ArrayInput extends Input<int[][]> {

public ArrayInput(int[][] data) {
super(data);
}

static int[] makeDivisions(int[][] data, int num) {
int initialDivision = data.length / num; //equally dividing
int[] divisions = new int[num];
Expand Down Expand Up @@ -81,6 +80,6 @@ public ArrayList<Input> divideData(int num) {
}
}
return result;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
package com.iluwatar.masterworker;

/**
*Class ArrayResult extends abstract class {@link Result} and contains data
*of type int[][].
* Class ArrayResult extends abstract class {@link Result} and contains data of type int[][].
*/

public class ArrayResult extends Result<int[][]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@

package com.iluwatar.masterworker;

import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

/**
*Class ArrayUtilityMethods has some utility methods for matrices and arrays.
* Class ArrayUtilityMethods has some utility methods for matrices and arrays.
*/

public class ArrayUtilityMethods {

private static final Logger LOGGER = LoggerFactory.getLogger(ArrayUtilityMethods.class);

private static final Random RANDOM = new Random();

/**
* Method arraysSame compares 2 arrays @param a1 and @param a2
* and @return whether their values are equal (boolean).
* Method arraysSame compares 2 arrays @param a1 and @param a2 and @return whether their values
* are equal (boolean).
*/

public static boolean arraysSame(int[] a1, int[] a2) {
Expand All @@ -61,10 +61,10 @@ public static boolean arraysSame(int[] a1, int[] a2) {
}

/**
* Method matricesSame compares 2 matrices @param m1 and @param m2
* and @return whether their values are equal (boolean).
* Method matricesSame compares 2 matrices @param m1 and @param m2 and @return whether their
* values are equal (boolean).
*/

public static boolean matricesSame(int[][] m1, int[][] m2) {
if (m1.length != m2.length) {
return false;
Expand All @@ -81,12 +81,12 @@ public static boolean matricesSame(int[][] m1, int[][] m2) {
return answer;
}
}

/**
* Method createRandomIntMatrix creates a random matrix of size @param rows
* and @param columns @return it (int[][]).
* Method createRandomIntMatrix creates a random matrix of size @param rows and @param columns.
*
* @return it (int[][]).
*/

public static int[][] createRandomIntMatrix(int rows, int columns) {
int[][] matrix = new int[rows][columns];
for (int i = 0; i < rows; i++) {
Expand All @@ -97,11 +97,11 @@ public static int[][] createRandomIntMatrix(int rows, int columns) {
}
return matrix;
}

/**
* Method printMatrix prints input matrix @param matrix.
*/

public static void printMatrix(int[][] matrix) {
//prints out int[][]
for (int i = 0; i < matrix.length; i++) {
Expand All @@ -111,5 +111,5 @@ public static void printMatrix(int[][] matrix) {
LOGGER.info("");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
import java.util.ArrayList;

/**
*The abstract Input class, having 1 public field which contains input data,
*and abstract method divideData.
* The abstract Input class, having 1 public field which contains input data, and abstract method
* divideData.
*
* @param <T> T will be type of data.
*/

public abstract class Input<T> {

public final T data;

public Input(T data) {
this.data = data;
}

public abstract ArrayList<Input> divideData(int num);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
package com.iluwatar.masterworker;

/**
*The abstract Result class, which contains 1 public field containing result
*data.
* The abstract Result class, which contains 1 public field containing result data.
*
* @param <T> T will be type of data.
*/

public abstract class Result<T> {

public final T data;

public Result(T data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import com.iluwatar.masterworker.system.systemmaster.Master;

/**
*Class ArrayTransposeMasterWorker extends abstract class {@link MasterWorker} and
*specifically solves the problem of finding transpose of input array.
* Class ArrayTransposeMasterWorker extends abstract class {@link MasterWorker} and specifically
* solves the problem of finding transpose of input array.
*/

public class ArrayTransposeMasterWorker extends MasterWorker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.iluwatar.masterworker.system.systemmaster.Master;

/**
*The abstract MasterWorker class which contains reference to master.
* The abstract MasterWorker class which contains reference to master.
*/

public abstract class MasterWorker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@

package com.iluwatar.masterworker.system.systemmaster;

import java.util.ArrayList;
import java.util.Enumeration;
import com.iluwatar.masterworker.ArrayResult;
import com.iluwatar.masterworker.system.systemworkers.ArrayTransposeWorker;
import com.iluwatar.masterworker.system.systemworkers.Worker;
import java.util.ArrayList;
import java.util.Enumeration;

/**
*Class ArrayTransposeMaster extends abstract class {@link Master} and contains
*definition of aggregateData, which will obtain final result from all
*data obtained and for setWorkers.
* Class ArrayTransposeMaster extends abstract class {@link Master} and contains definition of
* aggregateData, which will obtain final result from all data obtained and for setWorkers.
*/

public class ArrayTransposeMaster extends Master {
Expand All @@ -43,26 +42,29 @@ public ArrayTransposeMaster(int numOfWorkers) {
@Override
ArrayList<Worker> setWorkers(int num) {
ArrayList<Worker> ws = new ArrayList<Worker>(num);
for (int i = 0; i < num ; i++) {
for (int i = 0; i < num; i++) {
ws.add(new ArrayTransposeWorker(this, i + 1));
//i+1 will be id
}
return ws;
}

@Override
ArrayResult aggregateData() {
//number of rows in final result is number of rows in any of obtained results obtained from workers
int rows = ((ArrayResult) this.getAllResultData().get(this.getAllResultData().keys().nextElement())).data.length;
int columns = 0; //number of columns is sum of number of columns in all results obtained from workers
for (Enumeration<Integer> e = this.getAllResultData().keys(); e.hasMoreElements();) {
// number of rows in final result is number of rows in any of obtained results from workers
int rows = ((ArrayResult) this.getAllResultData()
.get(this.getAllResultData().keys().nextElement())).data.length;
int columns =
0; //number of columns is sum of number of columns in all results obtained from workers
for (Enumeration<Integer> e = this.getAllResultData().keys(); e.hasMoreElements(); ) {
columns += ((ArrayResult) this.getAllResultData().get(e.nextElement())).data[0].length;
}
int[][] resultData = new int[rows][columns];
int columnsDone = 0; //columns aggregated so far
for (int i = 0; i < this.getExpectedNumResults(); i++) {
//result obtained from ith worker
int[][] work = ((ArrayResult) this.getAllResultData().get(this.getWorkers().get(i).getWorkerId())).data;
int[][] work =
((ArrayResult) this.getAllResultData().get(this.getWorkers().get(i).getWorkerId())).data;
for (int m = 0; m < work.length; m++) {
//m = row number, n = columns number
for (int n = 0; n < work[0].length; n++) {
Expand All @@ -73,5 +75,5 @@ ArrayResult aggregateData() {
}
return new ArrayResult(resultData);
}

}

0 comments on commit 1fdc650

Please sign in to comment.