View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy2.interceptor.matcher.argument;
19  
20  import org.apache.commons.lang3.ObjectUtils;
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.commons.lang3.Validate;
23  import org.apache.commons.proxy2.interceptor.matcher.ArgumentMatcher;
24  
25  public final class ArgumentMatcherUtils
26  {
27      //******************************************************************************************************************
28      // Static Methods
29      //******************************************************************************************************************
30  
31      public static <T> ArgumentMatcher<T> any()
32      {
33          return new AnyMatcher<T>();
34      }
35  
36      public static ArgumentMatcher<String> endsWith(String suffix)
37      {
38          return new EndsWithMatcher(Validate.notNull(suffix));
39      }
40  
41      public static <T> ArgumentMatcher<T> eq(final T value)
42      {
43          return new EqualsMatcher<T>(value);
44      }
45  
46      public static <C extends Comparable<C>> ArgumentMatcher<C> gt(C comparable)
47      {
48          return new GreaterThanMatcher<C>(comparable);
49      }
50  
51      public static <C extends Comparable<C>> ArgumentMatcher<C> gte(C comparable)
52      {
53          return new GreaterThanOrEqualMatcher<C>(comparable);
54      }
55  
56      public static <T> ArgumentMatcher<T> isA(final Class<?> type)
57      {
58          return new InstanceOfMatcher<T>(type);
59      }
60  
61      public static <T> ArgumentMatcher<T> isNull()
62      {
63          return new IsNullMatcher<T>();
64      }
65  
66      public static <C extends Comparable<C>> ArgumentMatcher<C> lt(C comparable)
67      {
68          return new LessThanMatcher<C>(comparable);
69      }
70  
71      public static <C extends Comparable<C>> ArgumentMatcher<C> lte(C comparable)
72      {
73          return new LessThanOrEqualMatcher<C>(comparable);
74      }
75  
76      public static ArgumentMatcher<String> matches(String regex)
77      {
78          return new RegexMatcher(Validate.notNull(regex));
79      }
80  
81      public static <T> ArgumentMatcher<T> notNull()
82      {
83          return new NotNullMatcher<T>();
84      }
85  
86      public static ArgumentMatcher<String> startsWith(String prefix)
87      {
88          return new StartsWithMatcher(Validate.notNull(prefix));
89      }
90  
91      //******************************************************************************************************************
92      // Constructors
93      //******************************************************************************************************************
94  
95      private ArgumentMatcherUtils()
96      {
97  
98      }
99  
100     //******************************************************************************************************************
101     // Inner Classes
102     //******************************************************************************************************************
103 
104     private static final class AnyMatcher<T> implements ArgumentMatcher<T>
105     {
106         @Override
107         public boolean matches(T argument)
108         {
109             return true;
110         }
111     }
112 
113     private abstract static class ComparatorMatcher<C extends Comparable<C>> implements ArgumentMatcher<C>
114     {
115         private final C comparable;
116 
117         protected ComparatorMatcher(C comparable)
118         {
119             this.comparable = Validate.notNull(comparable);
120         }
121 
122         protected abstract boolean evaluate(int comparison);
123 
124         @Override
125         public boolean matches(C argument)
126         {
127             if (argument == null)
128             {
129                 return false;
130             }
131             final int comparison = (comparable).compareTo(argument);
132             return evaluate(comparison);
133         }
134     }
135 
136     public static class EndsWithMatcher implements ArgumentMatcher<String>
137     {
138         private final String suffix;
139 
140         public EndsWithMatcher(String suffix)
141         {
142             this.suffix = suffix;
143         }
144 
145         @Override
146         public boolean matches(String argument)
147         {
148             return StringUtils.endsWith(argument, suffix);
149         }
150     }
151 
152     private static final class EqualsMatcher<T> implements ArgumentMatcher<T>
153     {
154         private final T value;
155 
156         public EqualsMatcher(T value)
157         {
158             this.value = value;
159         }
160 
161         @SuppressWarnings("deprecation")
162         @Override
163         public boolean matches(T argument)
164         {
165             return ObjectUtils.equals(argument, value);
166         }
167     }
168 
169     private static final class GreaterThanMatcher<C extends Comparable<C>> extends ComparatorMatcher<C>
170     {
171         private GreaterThanMatcher(C comparable)
172         {
173             super(comparable);
174         }
175 
176         @Override
177         protected boolean evaluate(int comparison)
178         {
179             return comparison < 0;
180         }
181     }
182 
183     private static final class GreaterThanOrEqualMatcher<C extends Comparable<C>> extends ComparatorMatcher<C>
184     {
185         private GreaterThanOrEqualMatcher(C comparable)
186         {
187             super(comparable);
188         }
189 
190         @Override
191         protected boolean evaluate(int comparison)
192         {
193             return comparison <= 0;
194         }
195     }
196 
197     private static final class InstanceOfMatcher<T> implements ArgumentMatcher<T>
198     {
199         private final Class<?> type;
200 
201         public InstanceOfMatcher(Class<?> type)
202         {
203             this.type = Validate.notNull(type, "type");
204         }
205 
206         @Override
207         public boolean matches(T argument)
208         {
209             return type.isInstance(argument);
210         }
211     }
212 
213     private static final class IsNullMatcher<T> implements ArgumentMatcher<T>
214     {
215         @Override
216         public boolean matches(T argument)
217         {
218             return argument == null;
219         }
220     }
221 
222     private static final class LessThanMatcher<C extends Comparable<C>> extends ComparatorMatcher<C>
223     {
224         private LessThanMatcher(C comparable)
225         {
226             super(comparable);
227         }
228 
229         @Override
230         protected boolean evaluate(int comparison)
231         {
232             return comparison > 0;
233         }
234     }
235 
236     private static final class LessThanOrEqualMatcher<C extends Comparable<C>> extends ComparatorMatcher<C>
237     {
238         private LessThanOrEqualMatcher(C comparable)
239         {
240             super(comparable);
241         }
242 
243         @Override
244         protected boolean evaluate(int comparison)
245         {
246             return comparison >= 0;
247         }
248     }
249 
250     private static final class NotNullMatcher<T> implements ArgumentMatcher<T>
251     {
252         @Override
253         public boolean matches(T argument)
254         {
255             return argument != null;
256         }
257     }
258 
259     public static class RegexMatcher implements ArgumentMatcher<String>
260     {
261         private final String regex;
262 
263         public RegexMatcher(String regex)
264         {
265             this.regex = regex;
266         }
267 
268         @Override
269         public boolean matches(String argument)
270         {
271             return argument != null && argument.matches(regex);
272         }
273     }
274 
275     private static final class StartsWithMatcher implements ArgumentMatcher<String>
276     {
277         private final String prefix;
278 
279         private StartsWithMatcher(String prefix)
280         {
281             this.prefix = prefix;
282         }
283 
284         @Override
285         public boolean matches(String argument)
286         {
287             return StringUtils.startsWith(argument, prefix);
288         }
289     }
290 }