• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2006 Google Inc.
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 
17 package com.google.inject.matcher;
18 
19 import java.io.Serializable;
20 
21 /**
22  * Implements {@code and()} and {@code or()}.
23  *
24  * @author crazybob@google.com (Bob Lee)
25  */
26 public abstract class AbstractMatcher<T> implements Matcher<T> {
27 
and(final Matcher<? super T> other)28   public Matcher<T> and(final Matcher<? super T> other) {
29     return new AndMatcher<T>(this, other);
30   }
31 
or(Matcher<? super T> other)32   public Matcher<T> or(Matcher<? super T> other) {
33     return new OrMatcher<T>(this, other);
34   }
35 
36   private static class AndMatcher<T> extends AbstractMatcher<T> implements Serializable {
37     private final Matcher<? super T> a, b;
38 
AndMatcher(Matcher<? super T> a, Matcher<? super T> b)39     public AndMatcher(Matcher<? super T> a, Matcher<? super T> b) {
40       this.a = a;
41       this.b = b;
42     }
43 
matches(T t)44     public boolean matches(T t) {
45       return a.matches(t) && b.matches(t);
46     }
47 
equals(Object other)48     @Override public boolean equals(Object other) {
49       return other instanceof AndMatcher
50           && ((AndMatcher) other).a.equals(a)
51           && ((AndMatcher) other).b.equals(b);
52     }
53 
hashCode()54     @Override public int hashCode() {
55       return 41 * (a.hashCode() ^ b.hashCode());
56     }
57 
toString()58     @Override public String toString() {
59       return "and(" + a + ", " + b + ")";
60     }
61 
62     private static final long serialVersionUID = 0;
63   }
64 
65   private static class OrMatcher<T> extends AbstractMatcher<T> implements Serializable {
66     private final Matcher<? super T> a, b;
67 
OrMatcher(Matcher<? super T> a, Matcher<? super T> b)68     public OrMatcher(Matcher<? super T> a, Matcher<? super T> b) {
69       this.a = a;
70       this.b = b;
71     }
72 
matches(T t)73     public boolean matches(T t) {
74       return a.matches(t) || b.matches(t);
75     }
76 
equals(Object other)77     @Override public boolean equals(Object other) {
78       return other instanceof OrMatcher
79           && ((OrMatcher) other).a.equals(a)
80           && ((OrMatcher) other).b.equals(b);
81     }
82 
hashCode()83     @Override public int hashCode() {
84       return 37 * (a.hashCode() ^ b.hashCode());
85     }
86 
toString()87     @Override public String toString() {
88       return "or(" + a + ", " + b + ")";
89     }
90 
91     private static final long serialVersionUID = 0;
92   }
93 }
94