1
- package com .gazbert .patterns .behavioural .chainofresponsibility ;
2
-
3
- /*The MIT License (MIT)
4
-
5
- Copyright (c) 2014 Gazbert
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy of
8
- this software and associated documentation files (the "Software"), to deal in
9
- the Software without restriction, including without limitation the rights to
10
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
- the Software, and to permit persons to whom the Software is furnished to do so,
12
- subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in all
15
- copies or substantial portions of the Software.
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2016 Gareth Jon Lynch
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ * this software and associated documentation files (the "Software"), to deal in
8
+ * the Software without restriction, including without limitation the rights to
9
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
+ * the Software, and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
16
23
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
24
+ package com .gazbert .patterns .behavioural .chainofresponsibility ;
23
25
24
26
/**
25
- *
26
- * Provides the default functionality and 2 operations for subclasses to provide implementations for:
27
+ * Provides the default functionality and 2 operations for subclasses to provide implementations for:
27
28
* <br>
28
29
* (1) deciding if they want to review the document.
29
30
* <br>
30
31
* (2) method to call to review the document.
31
32
* <p>
32
33
* Consumers of the pattern call the reviewDocument() method.
33
- *
34
- * @author gazbert
35
34
*
35
+ * @author gazbert
36
36
*/
37
37
public abstract class AbstractDocumentReviewHandler implements DocumentReviewHandler {
38
38
39
- /** Here for our test assertions to track who's reviewed the document */
39
+ /**
40
+ * Here for our test assertions to track who's reviewed the document
41
+ */
40
42
private static String handledBy = "" ;
41
43
42
44
/**
43
45
* Holds reference to the next Handler/Receiver.
44
46
*/
45
- private DocumentReviewHandler nextHandler ;
47
+ private DocumentReviewHandler nextHandler ;
46
48
47
49
/**
48
50
* Consumers of the pattern call this method to do stuff.
49
51
* <p>
50
52
* This is the business method.
51
53
* <p>
52
54
* In this case, JIRA/Bugzilla would call this with the document to review...
53
- *
54
- * @param document
55
+ *
56
+ * @param document the doc to review
55
57
*/
56
- public static void reviewDocumentRequest (final String document )
57
- {
58
+ public static void reviewDocumentRequest (String document ) {
58
59
// Create the handlers/receivers
59
60
final DocumentReviewHandler supportReviewHandler = new SupportReviewHandler ();
60
61
final DocumentReviewHandler salesReviewHandler = new SalesReviewHandler ();
@@ -64,16 +65,16 @@ public static void reviewDocumentRequest(final String document)
64
65
// Chain em together - totally random order of chaining here ;-)
65
66
supportReviewHandler .setNextHandler (salesReviewHandler );
66
67
salesReviewHandler .setNextHandler (engineeringReviewHandler );
67
- engineeringReviewHandler .setNextHandler (testingReviewHandler );
68
+ engineeringReviewHandler .setNextHandler (testingReviewHandler );
68
69
testingReviewHandler .setNextHandler (null ); // see NullObjectPattern for better way of 'ending' stuff
69
70
70
71
// New review request comes in and gets routed to support team first...
71
72
supportReviewHandler .processHandler (document );
72
73
}
73
74
74
75
@ Override
75
- public void setNextHandler (final DocumentReviewHandler handler ) {
76
- this .nextHandler = handler ;
76
+ public void setNextHandler (DocumentReviewHandler handler ) {
77
+ this .nextHandler = handler ;
77
78
}
78
79
79
80
@@ -87,51 +88,47 @@ public void processHandler(String document) {
87
88
boolean wordFound = false ;
88
89
89
90
// check for matching words for this Handler
90
- for (String word : getSelectionCriteria ())
91
- {
92
- if (document .indexOf (word ) >= 0 )
93
- {
91
+ for (String word : getSelectionCriteria ()) {
92
+ if (document .contains (word )) {
94
93
wordFound = true ;
95
94
break ;
96
95
}
97
- }
96
+ }
98
97
99
98
// Do the handling if we need to...
100
- if (wordFound )
101
- {
99
+ if (wordFound ) {
102
100
handledBy = reviewDocument (document );
103
- }
104
- else
105
- {
101
+ } else {
106
102
// Check if next Receiver 'wants it'... ;-o
107
- if (null != nextHandler )
108
- {
103
+ if (null != nextHandler ) {
109
104
nextHandler .processHandler (document );
110
105
}
111
106
}
112
107
}
113
108
114
109
/**
115
110
* Only here for unit test code to assert stuff with sake of this demo.
116
- * @return
111
+ *
112
+ * @return handledBy
117
113
*/
118
- public static String getHandledBy ()
119
- {
120
- return handledBy ;
114
+ public static String getHandledBy () {
115
+ return handledBy ;
121
116
}
122
117
123
118
///////////////////// Subclass contract for the concrete Handlers ////////////////////////
124
119
125
120
/**
126
121
* This is where we ask each Handler for its document review selection criteria.
127
- * @return
122
+ *
123
+ * @return selection criteria
128
124
*/
129
125
protected abstract String [] getSelectionCriteria ();
130
126
131
127
/**
132
128
* This is where we send the document to interested Handlers.
133
- * @param document
129
+ *
130
+ * @param document the doc
134
131
* @return department that reviewed the document
135
- */
132
+ */
136
133
protected abstract String reviewDocument (String document );
137
134
}
0 commit comments