1 /*
2  * Copyright 2001-2009 OFFIS, Henri Tremblay
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.easymock.internal.matchers;
17 
18 import java.io.Serializable;
19 import java.util.Comparator;
20 
21 import org.easymock.IArgumentMatcher;
22 import org.easymock.LogicalOperator;
23 
24 public class Compare<T> implements IArgumentMatcher, Serializable {
25 
26     private static final long serialVersionUID = -4859402739599754147L;
27 
28     private T expected;
29 
30     private Comparator<? super T> comparator;
31 
32     private LogicalOperator operator;
33 
Compare(T expected, Comparator<? super T> comparator, LogicalOperator result)34     public Compare(T expected, Comparator<? super T> comparator, LogicalOperator result) {
35         this.expected = expected;
36         this.comparator = comparator;
37         this.operator = result;
38     }
39 
appendTo(StringBuffer buffer)40     public void appendTo(StringBuffer buffer) {
41         buffer.append(comparator + "(" + expected + ") " + operator.getSymbol()
42                 + " 0");
43     }
44 
45     @SuppressWarnings("unchecked")
matches(Object actual)46     public boolean matches(Object actual) {
47         if(actual == null) {
48             return false;
49         }
50         return operator.matchResult(comparator.compare((T) actual, expected));
51     }
52 
53 }
54