diff --git a/chain/src/main/java/com/iluwatar/chain/OrcCommander.java b/chain/src/main/java/com/iluwatar/chain/OrcCommander.java
index 73de1b8b2..6cc495b99 100644
--- a/chain/src/main/java/com/iluwatar/chain/OrcCommander.java
+++ b/chain/src/main/java/com/iluwatar/chain/OrcCommander.java
@@ -15,6 +15,7 @@ public class OrcCommander extends RequestHandler {
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
printHandling(req);
+ req.markHandled();
} else {
super.handleRequest(req);
}
diff --git a/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java b/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java
index 68df3ec6f..e6d68c19c 100644
--- a/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java
+++ b/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java
@@ -15,6 +15,7 @@ public class OrcOfficer extends RequestHandler {
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
printHandling(req);
+ req.markHandled();
} else {
super.handleRequest(req);
}
diff --git a/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java b/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java
index d96f51702..bd2a8d11d 100644
--- a/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java
+++ b/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java
@@ -15,6 +15,7 @@ public class OrcSoldier extends RequestHandler {
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
printHandling(req);
+ req.markHandled();
} else {
super.handleRequest(req);
}
diff --git a/chain/src/main/java/com/iluwatar/chain/Request.java b/chain/src/main/java/com/iluwatar/chain/Request.java
index 0c62cfd43..5c3256a55 100644
--- a/chain/src/main/java/com/iluwatar/chain/Request.java
+++ b/chain/src/main/java/com/iluwatar/chain/Request.java
@@ -1,38 +1,78 @@
package com.iluwatar.chain;
+import java.util.Objects;
+
/**
- *
* Request
- *
*/
public class Request {
- private String requestDescription;
- private RequestType requestType;
+ /**
+ * The type of this request, used by each item in the chain to see if they should or can handle
+ * this particular request
+ */
+ private final RequestType requestType;
- public Request(RequestType requestType, String requestDescription) {
- this.setRequestType(requestType);
- this.setRequestDescription(requestDescription);
+ /**
+ * A description of the request
+ */
+ private final String requestDescription;
+
+ /**
+ * Indicates if the request is handled or not. A request can only switch state from unhandled to
+ * handled, there's no way to 'unhandle' a request
+ */
+ private boolean handled = false;
+
+ /**
+ * Create a new request of the given type and accompanied description.
+ *
+ * @param requestType The type of request
+ * @param requestDescription The description of the request
+ */
+ public Request(final RequestType requestType, final String requestDescription) {
+ this.requestType = Objects.requireNonNull(requestType);
+ this.requestDescription = Objects.requireNonNull(requestDescription);
}
+ /**
+ * Get a description of the request
+ *
+ * @return A human readable description of the request
+ */
public String getRequestDescription() {
return requestDescription;
}
- public void setRequestDescription(String requestDescription) {
- this.requestDescription = requestDescription;
- }
-
+ /**
+ * Get the type of this request, used by each person in the chain of command to see if they should
+ * or can handle this particular request
+ *
+ * @return The request type
+ */
public RequestType getRequestType() {
return requestType;
}
- public void setRequestType(RequestType requestType) {
- this.requestType = requestType;
+ /**
+ * Mark the request as handled
+ */
+ public void markHandled() {
+ this.handled = true;
+ }
+
+ /**
+ * Indicates if this request is handled or not
+ *
+ * @return true when the request is handled, false if not
+ */
+ public boolean isHandled() {
+ return this.handled;
}
@Override
public String toString() {
return getRequestDescription();
}
+
}
diff --git a/chain/src/test/java/com/iluwatar/chain/OrcKingTest.java b/chain/src/test/java/com/iluwatar/chain/OrcKingTest.java
new file mode 100644
index 000000000..fd3d573b6
--- /dev/null
+++ b/chain/src/test/java/com/iluwatar/chain/OrcKingTest.java
@@ -0,0 +1,37 @@
+package com.iluwatar.chain;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Date: 12/6/15 - 9:29 PM
+ *
+ * @author Jeroen Meulemeester
+ */
+public class OrcKingTest {
+
+ /**
+ * All possible requests
+ */
+ private static final Request[] REQUESTS = new Request[]{
+ new Request(RequestType.DEFEND_CASTLE, "Don't let the barbarians enter my castle!!"),
+ new Request(RequestType.TORTURE_PRISONER, "Don't just stand there, tickle him!"),
+ new Request(RequestType.COLLECT_TAX, "Don't steal, the King hates competition ..."),
+ };
+
+ @Test
+ public void testMakeRequest() throws Exception {
+ final OrcKing king = new OrcKing();
+
+ for (final Request request : REQUESTS) {
+ king.makeRequest(request);
+ assertTrue(
+ "Expected all requests from King to be handled, but [" + request + "] was not!",
+ request.isHandled()
+ );
+ }
+
+ }
+
+}
\ No newline at end of file