1 /*******************************************************************************
2 * Copyright 2013 André Rouél and Dominik Seichter
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 net.sf.qualitytest.blueprint.strategy.matching;
17
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
20
21 import net.sf.qualitycheck.Check;
22 import net.sf.qualitytest.blueprint.MatchingStrategy;
23
24 /**
25 * This value matching strategy matches string names case insensitively on the method name. This works for setter-based
26 * blueprinting.
27 *
28 * This {@code MatchingStrategy} does never match by type.
29 *
30 * @author Dominik Seichter
31 */
32 public class CaseInsensitiveMethodNameMatchingStrategy implements MatchingStrategy {
33
34 private static final String SETTER_PREFIX = "set";
35
36 private final String name;
37 private final String prefix;
38
39 /**
40 * Create a new {@code CaseInsensitiveMethodNameMatchingStrategy} which matches method names by default such as
41 * 'name' or 'setName' case insensitively.
42 *
43 * @param name
44 * The name to be matched (must not be empty or {@code null}.
45 */
46 public CaseInsensitiveMethodNameMatchingStrategy(final String name) {
47 this.name = Check.notEmpty(name, "name");
48 prefix = SETTER_PREFIX;
49 }
50
51 /**
52 * Create a new {@code CaseInsensitiveMethodNameMatchingStrategy} which matches method names by default such as
53 * 'name' or prefix + 'Name' case insensitively.
54 *
55 * @param name
56 * The name to be matched (must not be empty or {@code null}.
57 * @param prefix
58 * An optional prefix that is matched before the name (default is 'set'). Maybe empty but not
59 * {@code null}.
60 */
61 public CaseInsensitiveMethodNameMatchingStrategy(final String name, final String prefix) {
62 this.name = Check.notEmpty(name);
63 this.prefix = Check.notNull(prefix);
64 }
65
66 @Override
67 public boolean matchesByField(final Field field) {
68 Check.notNull(field, "field");
69 final String fieldName = field.getName();
70 return name.equalsIgnoreCase(fieldName);
71 }
72
73 @Override
74 public boolean matchesByMethod(final Method method) {
75 Check.notNull(method, "method");
76 final String methodName = method.getName();
77 final String setterName = prefix + name;
78 return name.equalsIgnoreCase(methodName) || setterName.equalsIgnoreCase(methodName);
79 }
80
81 /**
82 * This strategy does never match a type!
83 *
84 * @return false
85 */
86 @Override
87 public boolean matchesByType(final Class<?> clazz) {
88 return false;
89 }
90
91 }