From e6c74a5fb97f3c1faacda5653894eb654be72435 Mon Sep 17 00:00:00 2001
From: Anurag Agarwal The Master-Worker 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.
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}.
+ * {@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}. */ public class App { @@ -68,12 +66,12 @@ public class App { */ public static void main(String[] args) { - ArrayTransposeMasterWorker mw = new ArrayTransposeMasterWorker(); - int rows = 10; - int columns = 20; - int[][] inputMatrix = ArrayUtilityMethods.createRandomIntMatrix(rows, columns); - ArrayInput input = new ArrayInput(inputMatrix); - ArrayResult result = (ArrayResult) mw.getResult(input); + var mw = new ArrayTransposeMasterWorker(); + var rows = 10; + var columns = 20; + var inputMatrix = ArrayUtilityMethods.createRandomIntMatrix(rows, columns); + var input = new ArrayInput(inputMatrix); + var result = (ArrayResult) mw.getResult(input); if (result != null) { ArrayUtilityMethods.printMatrix(inputMatrix); ArrayUtilityMethods.printMatrix(result.data); diff --git a/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayInput.java b/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayInput.java index cd03a0a21..c8e68f958 100644 --- a/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayInput.java +++ b/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayInput.java @@ -25,6 +25,7 @@ package com.iluwatar.masterworker; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; /** * Class ArrayInput extends abstract class {@link Input} and contains data of type int[][]. @@ -37,12 +38,12 @@ public class ArrayInput extends InputAs a validation result {@link Validator#get()} it either returns valid object {@link - * Validator#t} or throws a list of exceptions {@link Validator#exceptions} collected during - * validation. + *
As a validation result {@link Validator#get()} either returns valid object
+ * or throws {@link IllegalStateException} with list of exceptions collected during validation.
*/
public class App {
@@ -55,10 +54,10 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
- User user = new User("user", 24, Sex.FEMALE, "foobar.com");
+ var user = new User("user", 24, Sex.FEMALE, "foobar.com");
LOGGER.info(Validator.of(user).validate(User::getName, Objects::nonNull, "name is null")
.validate(User::getName, name -> !name.isEmpty(), "name is empty")
- .validate(User::getEmail, email -> !email.contains("@"), "email doesn't containt '@'")
+ .validate(User::getEmail, email -> !email.contains("@"), "email doesn't contains '@'")
.validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get()
.toString());
}
diff --git a/monad/src/main/java/com/iluwatar/monad/User.java b/monad/src/main/java/com/iluwatar/monad/User.java
index 77766d1aa..f67009bc3 100644
--- a/monad/src/main/java/com/iluwatar/monad/User.java
+++ b/monad/src/main/java/com/iluwatar/monad/User.java
@@ -28,10 +28,10 @@ package com.iluwatar.monad;
*/
public class User {
- private String name;
- private int age;
- private Sex sex;
- private String email;
+ private final String name;
+ private final int age;
+ private final Sex sex;
+ private final String email;
/**
* Constructor.
diff --git a/monad/src/main/java/com/iluwatar/monad/Validator.java b/monad/src/main/java/com/iluwatar/monad/Validator.java
index 2d1f1bdab..47acc8a42 100644
--- a/monad/src/main/java/com/iluwatar/monad/Validator.java
+++ b/monad/src/main/java/com/iluwatar/monad/Validator.java
@@ -85,18 +85,21 @@ public class Validator In the following example, The {@link LoadBalancer} class represents the app's logic. It
* contains a series of Servers, which can handle requests of type {@link Request}. Two instances of
- * LoadBalacer are created. When a request is made to a server via the first LoadBalancer the state
+ * LoadBalancer are created. When a request is made to a server via the first LoadBalancer the state
* change in the first load balancer affects the second. So if the first LoadBalancer selects the
* Server 1, the second LoadBalancer on a new request will select the Second server. If a third
* LoadBalancer is created and a new request is made to it, then it will select the third server as
@@ -43,8 +43,8 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
- LoadBalancer loadBalancer1 = new LoadBalancer();
- LoadBalancer loadBalancer2 = new LoadBalancer();
+ var loadBalancer1 = new LoadBalancer();
+ var loadBalancer2 = new LoadBalancer();
loadBalancer1.serverRequest(new Request("Hello"));
loadBalancer2.serverRequest(new Request("Hello World"));
}
diff --git a/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java b/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java
index 8546ae177..7a784f514 100644
--- a/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java
+++ b/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java
@@ -38,8 +38,8 @@ public class LoadBalancer {
private static int lastServedId;
static {
- int id = 0;
- for (int port : new int[]{8080, 8081, 8082, 8083, 8084}) {
+ var id = 0;
+ for (var port : new int[]{8080, 8081, 8082, 8083, 8084}) {
SERVERS.add(new Server("localhost", port, ++id));
}
}
@@ -69,7 +69,7 @@ public class LoadBalancer {
if (lastServedId >= SERVERS.size()) {
lastServedId = 0;
}
- Server server = SERVERS.get(lastServedId++);
+ var server = SERVERS.get(lastServedId++);
server.serve(request);
}
diff --git a/monostate/src/test/java/com/iluwatar/monostate/AppTest.java b/monostate/src/test/java/com/iluwatar/monostate/AppTest.java
index c914f136e..d17a56bb9 100644
--- a/monostate/src/test/java/com/iluwatar/monostate/AppTest.java
+++ b/monostate/src/test/java/com/iluwatar/monostate/AppTest.java
@@ -32,8 +32,7 @@ public class AppTest {
@Test
public void testMain() {
- String[] args = {};
- App.main(args);
+ App.main(new String[]{});
}
}
diff --git a/monostate/src/test/java/com/iluwatar/monostate/LoadBalancerTest.java b/monostate/src/test/java/com/iluwatar/monostate/LoadBalancerTest.java
index 736bf6ea6..d62c029e2 100644
--- a/monostate/src/test/java/com/iluwatar/monostate/LoadBalancerTest.java
+++ b/monostate/src/test/java/com/iluwatar/monostate/LoadBalancerTest.java
@@ -44,8 +44,8 @@ public class LoadBalancerTest {
@Test
public void testSameStateAmongstAllInstances() {
- final LoadBalancer firstBalancer = new LoadBalancer();
- final LoadBalancer secondBalancer = new LoadBalancer();
+ final var firstBalancer = new LoadBalancer();
+ final var secondBalancer = new LoadBalancer();
firstBalancer.addServer(new Server("localhost", 8085, 6));
// Both should have the same number of servers.
assertEquals(firstBalancer.getNoOfServers(), secondBalancer.getNoOfServers());
@@ -55,18 +55,18 @@ public class LoadBalancerTest {
@Test
public void testServe() {
- final Server server = mock(Server.class);
+ final var server = mock(Server.class);
when(server.getHost()).thenReturn("testhost");
when(server.getPort()).thenReturn(1234);
doNothing().when(server).serve(any(Request.class));
- final LoadBalancer loadBalancer = new LoadBalancer();
+ final var loadBalancer = new LoadBalancer();
loadBalancer.addServer(server);
verifyZeroInteractions(server);
- final Request request = new Request("test");
- for (int i = 0; i < loadBalancer.getNoOfServers() * 2; i++) {
+ final var request = new Request("test");
+ for (var i = 0; i < loadBalancer.getNoOfServers() * 2; i++) {
loadBalancer.serverRequest(request);
}
From 9b105d770df2ca56ffd725e88fdbf17666e7cd1b Mon Sep 17 00:00:00 2001
From: Anurag Agarwal
Ilkka Seppälä 💻 |
+
Ilkka Seppälä 💻 |
+ Ilkka Seppälä 💻 📆 |
Ilkka Seppälä 💻 📆 |
+ amit1307 💻 |
Ilkka Seppälä 💻 📆 |
+ Ilkka Seppälä 💻 📆 🚧 📝 🖋 📖 🤔 🚇 👀 |
amit1307 💻 |
Ilkka Seppälä 💻 📆 🚧 📝 🖋 📖 🤔 🚇 👀 |
+ Ilkka Seppälä 💻 📆 🚧 📝 🖋 📖 🤔 🚇 👀 📆 |
amit1307 💻 |
+ Narendra Pathai 💻 🤔 🚧 💬 👀 |
Ilkka Seppälä 💻 📆 🚧 📝 🖋 📖 🤔 🚇 👀 📆 |
+ Ilkka Seppälä 📆 🚧 🖋 |
amit1307 💻 |
- Narendra Pathai 💻 🤔 🚧 💬 👀 |
+ Narendra Pathai 💻 🤔 👀 |
Jeroen Meulemeester 💻 |
Ilkka Seppälä 📆 🚧 🖋 |
+ Ilkka Seppälä 📆 🚧 🖋 |
amit1307 💻 |
Narendra Pathai 💻 🤔 👀 |
Jeroen Meulemeester 💻 |
+ Joseph McCarthy 💻 |
The below example demonstrates basic CRUD operations: Create, Read, Update, and Delete. - */ -public final class App { - - private static Logger log = LoggerFactory.getLogger(App.class); - private static final String STUDENT_STRING = "App.main(), student : "; - - - /** - * Program entry point. - * - * @param args command line args. - */ - public static void main(final String... args) { - - /* Create new data mapper for type 'first' */ - final var mapper = new StudentDataMapperImpl(); - - /* Create new student */ - var student = new Student(1, "Adam", 'A'); - - /* Add student in respectibe store */ - mapper.insert(student); - - log.debug(STUDENT_STRING + student + ", is inserted"); - - /* Find this student */ - final var studentToBeFound = mapper.find(student.getStudentId()); - - log.debug(STUDENT_STRING + studentToBeFound + ", is searched"); - - /* Update existing student object */ - student = new Student(student.getStudentId(), "AdamUpdated", 'A'); - - /* Update student in respectibe db */ - mapper.update(student); - - log.debug(STUDENT_STRING + student + ", is updated"); - log.debug(STUDENT_STRING + student + ", is going to be deleted"); - - /* Delete student in db */ - mapper.delete(student); - } - - private App() { - } -} +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.datamapper; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the + * database. Its responsibility is to transfer data between the two and also to isolate them from + * each other. With Data Mapper the in-memory objects needn't know even that there's a database + * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The + * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , + * Data Mapper itself is even unknown to the domain layer. + * + *
The below example demonstrates basic CRUD operations: Create, Read, Update, and Delete.
+ */
+public final class App {
+
+ private static final Logger log = LoggerFactory.getLogger(App.class);
+ private static final String STUDENT_STRING = "App.main(), student : ";
+
+
+ /**
+ * Program entry point.
+ *
+ * @param args command line args.
+ */
+ public static void main(final String... args) {
+
+ /* Create new data mapper for type 'first' */
+ final var mapper = new StudentDataMapperImpl();
+
+ /* Create new student */
+ var student = new Student(1, "Adam", 'A');
+
+ /* Add student in respectibe store */
+ mapper.insert(student);
+
+ log.debug(STUDENT_STRING + student + ", is inserted");
+
+ /* Find this student */
+ final var studentToBeFound = mapper.find(student.getStudentId());
+
+ log.debug(STUDENT_STRING + studentToBeFound + ", is searched");
+
+ /* Update existing student object */
+ student = new Student(student.getStudentId(), "AdamUpdated", 'A');
+
+ /* Update student in respectibe db */
+ mapper.update(student);
+
+ log.debug(STUDENT_STRING + student + ", is updated");
+ log.debug(STUDENT_STRING + student + ", is going to be deleted");
+
+ /* Delete student in db */
+ mapper.delete(student);
+ }
+
+ private App() {
+ }
+}
diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapperImpl.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapperImpl.java
index 85ad4aa8d..7abe04e3f 100644
--- a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapperImpl.java
+++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapperImpl.java
@@ -33,7 +33,7 @@ import java.util.Optional;
public final class StudentDataMapperImpl implements StudentDataMapper {
/* Note: Normally this would be in the form of an actual database */
- private List