|
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 |
| - * https://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.classify; |
17 |
| - |
18 |
| -import java.util.Map; |
19 |
| - |
20 |
| -/** |
21 |
| - * A special purpose {@link Classifier} with easy configuration options for mapping from |
22 |
| - * one arbitrary type of object to another via a pattern matcher. |
23 |
| - * |
24 |
| - * @author Dave Syer |
25 |
| - * @param <C> the type of thing to classify |
26 |
| - * @param <T> the output of the classifier |
27 |
| - * |
28 |
| - */ |
29 |
| -@SuppressWarnings("serial") |
30 |
| -public class BackToBackPatternClassifier<C, T> implements Classifier<C, T> { |
31 |
| - |
32 |
| - private Classifier<C, String> router; |
33 |
| - |
34 |
| - private Classifier<String, T> matcher; |
35 |
| - |
36 |
| - /** |
37 |
| - * Default constructor, provided as a convenience for people using setter injection. |
38 |
| - */ |
39 |
| - public BackToBackPatternClassifier() { |
40 |
| - } |
41 |
| - |
42 |
| - /** |
43 |
| - * Set up a classifier with input to the router and output from the matcher. |
44 |
| - * @param router see {@link #setRouterDelegate(Object)} |
45 |
| - * @param matcher see {@link #setMatcherMap(Map)} |
46 |
| - */ |
47 |
| - public BackToBackPatternClassifier(Classifier<C, String> router, Classifier<String, T> matcher) { |
48 |
| - super(); |
49 |
| - this.router = router; |
50 |
| - this.matcher = matcher; |
51 |
| - } |
52 |
| - |
53 |
| - /** |
54 |
| - * A convenience method for creating a pattern matching classifier for the matcher |
55 |
| - * component. |
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 Java |
64 |
| - * Object. The object provided must have precisely one public method that either has |
65 |
| - * the <code>@Classifier</code> annotation or accepts a single argument and outputs a |
66 |
| - * String. This will be used to create an input classifier for the router component. |
67 |
| - * @param delegate the delegate object used to create a router classifier |
68 |
| - */ |
69 |
| - public void setRouterDelegate(Object delegate) { |
70 |
| - this.router = new ClassifierAdapter<C, String>(delegate); |
71 |
| - } |
72 |
| - |
73 |
| - /** |
74 |
| - * Classify the input and map to a String, then take that and put it into a pattern |
75 |
| - * matcher to match to an output value. |
76 |
| - */ |
77 |
| - @Override |
78 |
| - public T classify(C classifiable) { |
79 |
| - return this.matcher.classify(this.router.classify(classifiable)); |
80 |
| - } |
81 |
| - |
82 |
| -} |
| 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 | + * https://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.classify; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * A special purpose {@link Classifier} with easy configuration options for mapping from |
| 22 | + * one arbitrary type of object to another via a pattern matcher. |
| 23 | + * |
| 24 | + * @author Dave Syer |
| 25 | + * @param <C> the type of thing to classify |
| 26 | + * @param <T> the output of the classifier |
| 27 | + * |
| 28 | + */ |
| 29 | +@SuppressWarnings("serial") |
| 30 | +public class BackToBackPatternClassifier<C, T> implements Classifier<C, T> { |
| 31 | + |
| 32 | + private Classifier<C, String> router; |
| 33 | + |
| 34 | + private Classifier<String, T> matcher; |
| 35 | + |
| 36 | + /** |
| 37 | + * Default constructor, provided as a convenience for people using setter injection. |
| 38 | + */ |
| 39 | + public BackToBackPatternClassifier() { |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Set up a classifier with input to the router and output from the matcher. |
| 44 | + * @param router see {@link #setRouterDelegate(Object)} |
| 45 | + * @param matcher see {@link #setMatcherMap(Map)} |
| 46 | + */ |
| 47 | + public BackToBackPatternClassifier(Classifier<C, String> router, Classifier<String, T> matcher) { |
| 48 | + super(); |
| 49 | + this.router = router; |
| 50 | + this.matcher = matcher; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * A convenience method for creating a pattern matching classifier for the matcher |
| 55 | + * component. |
| 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 Java |
| 64 | + * Object. The object provided must have precisely one public method that either has |
| 65 | + * the <code>@Classifier</code> annotation or accepts a single argument and outputs a |
| 66 | + * String. This will be used to create an input classifier for the router component. |
| 67 | + * @param delegate the delegate object used to create a router classifier |
| 68 | + */ |
| 69 | + public void setRouterDelegate(Object delegate) { |
| 70 | + this.router = new ClassifierAdapter<C, String>(delegate); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Classify the input and map to a String, then take that and put it into a pattern |
| 75 | + * matcher to match to an output value. |
| 76 | + */ |
| 77 | + @Override |
| 78 | + public T classify(C classifiable) { |
| 79 | + return this.matcher.classify(this.router.classify(classifiable)); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments