|
1 | 1 | package com.iluwatar.chain; |
2 | 2 |
|
| 3 | +import java.util.Objects; |
| 4 | + |
3 | 5 | /** |
4 | | - * |
5 | 6 | * Request |
6 | | - * |
7 | 7 | */ |
8 | 8 | public class Request { |
9 | 9 |
|
10 | | - private String requestDescription; |
11 | | - private RequestType requestType; |
| 10 | + /** |
| 11 | + * The type of this request, used by each item in the chain to see if they should or can handle |
| 12 | + * this particular request |
| 13 | + */ |
| 14 | + private final RequestType requestType; |
| 15 | + |
| 16 | + /** |
| 17 | + * A description of the request |
| 18 | + */ |
| 19 | + private final String requestDescription; |
12 | 20 |
|
13 | | - public Request(RequestType requestType, String requestDescription) { |
14 | | - this.setRequestType(requestType); |
15 | | - this.setRequestDescription(requestDescription); |
| 21 | + /** |
| 22 | + * Indicates if the request is handled or not. A request can only switch state from unhandled to |
| 23 | + * handled, there's no way to 'unhandle' a request |
| 24 | + */ |
| 25 | + private boolean handled = false; |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a new request of the given type and accompanied description. |
| 29 | + * |
| 30 | + * @param requestType The type of request |
| 31 | + * @param requestDescription The description of the request |
| 32 | + */ |
| 33 | + public Request(final RequestType requestType, final String requestDescription) { |
| 34 | + this.requestType = Objects.requireNonNull(requestType); |
| 35 | + this.requestDescription = Objects.requireNonNull(requestDescription); |
16 | 36 | } |
17 | 37 |
|
| 38 | + /** |
| 39 | + * Get a description of the request |
| 40 | + * |
| 41 | + * @return A human readable description of the request |
| 42 | + */ |
18 | 43 | public String getRequestDescription() { |
19 | 44 | return requestDescription; |
20 | 45 | } |
21 | 46 |
|
22 | | - public void setRequestDescription(String requestDescription) { |
23 | | - this.requestDescription = requestDescription; |
24 | | - } |
25 | | - |
| 47 | + /** |
| 48 | + * Get the type of this request, used by each person in the chain of command to see if they should |
| 49 | + * or can handle this particular request |
| 50 | + * |
| 51 | + * @return The request type |
| 52 | + */ |
26 | 53 | public RequestType getRequestType() { |
27 | 54 | return requestType; |
28 | 55 | } |
29 | 56 |
|
30 | | - public void setRequestType(RequestType requestType) { |
31 | | - this.requestType = requestType; |
| 57 | + /** |
| 58 | + * Mark the request as handled |
| 59 | + */ |
| 60 | + public void markHandled() { |
| 61 | + this.handled = true; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Indicates if this request is handled or not |
| 66 | + * |
| 67 | + * @return <tt>true</tt> when the request is handled, <tt>false</tt> if not |
| 68 | + */ |
| 69 | + public boolean isHandled() { |
| 70 | + return this.handled; |
32 | 71 | } |
33 | 72 |
|
34 | 73 | @Override |
35 | 74 | public String toString() { |
36 | 75 | return getRequestDescription(); |
37 | 76 | } |
| 77 | + |
38 | 78 | } |
0 commit comments