1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.springframework.batch.classify; |
17 | |
18 | import java.util.Map; |
19 | |
20 | /** |
21 | * A special purpose {@link Classifier} with easy configuration options for |
22 | * mapping from one arbitrary type of object to another via a pattern matcher. |
23 | * |
24 | * @author Dave Syer |
25 | * |
26 | */ |
27 | public class BackToBackPatternClassifier<C, T> implements Classifier<C, T> { |
28 | |
29 | private Classifier<C, String> router; |
30 | |
31 | private Classifier<String, T> matcher; |
32 | |
33 | /** |
34 | * Default constructor, provided as a convenience for people using setter |
35 | * injection. |
36 | */ |
37 | public BackToBackPatternClassifier() { |
38 | } |
39 | |
40 | /** |
41 | * Set up a classifier with input to the router and output from the matcher. |
42 | * |
43 | * @param router see {@link #setRouterDelegate(Object)} |
44 | * @param matcher see {@link #setMatcherMap(Map)} |
45 | */ |
46 | public BackToBackPatternClassifier(Classifier<C, String> router, Classifier<String, T> matcher) { |
47 | super(); |
48 | this.router = router; |
49 | this.matcher = matcher; |
50 | } |
51 | |
52 | /** |
53 | * A convenience method for creating a pattern matching classifier for the |
54 | * matcher component. |
55 | * |
56 | * @param map maps pattern keys with wildcards to output values |
57 | */ |
58 | public void setMatcherMap(Map<String, T> map) { |
59 | this.matcher = new PatternMatchingClassifier<T>(map); |
60 | } |
61 | |
62 | /** |
63 | * A convenience method of creating a router classifier based on a plain old |
64 | * Java Object. The object provided must have precisely one public method |
65 | * that either has the @Classifier annotation or accepts a single argument |
66 | * and outputs a String. This will be used to create an input classifier for |
67 | * the router component. <br/> |
68 | * |
69 | * @param delegate the delegate object used to create a router classifier |
70 | */ |
71 | public void setRouterDelegate(Object delegate) { |
72 | this.router = new ClassifierAdapter<C,String>(delegate); |
73 | } |
74 | |
75 | /** |
76 | * Classify the input and map to a String, then take that and put it into a |
77 | * pattern matcher to match to an output value. |
78 | */ |
79 | public T classify(C classifiable) { |
80 | return matcher.classify(router.classify(classifiable)); |
81 | } |
82 | |
83 | } |