KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > matcher > AbstractMatcher


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 /**
20  * Implements {@code and()} and {@code or()}.
21  *
22  * @author crazybob@google.com (Bob Lee)
23  */

24 public abstract class AbstractMatcher<T> implements Matcher<T> {
25
26   public Matcher<T> and(final Matcher<? super T> other) {
27     return new AndMatcher<T>(this, other);
28   }
29
30   public Matcher<T> or(Matcher<? super T> other) {
31     return new OrMatcher<T>(this, other);
32   }
33
34   static class AndMatcher<T> extends AbstractMatcher<T> {
35
36     final Matcher<? super T> a, b;
37
38     public AndMatcher(Matcher<? super T> a, Matcher<? super T> b) {
39       this.a = a;
40       this.b = b;
41     }
42
43     public boolean matches(T t) {
44       return a.matches(t) && b.matches(t);
45     }
46   }
47
48   static class OrMatcher<T> extends AbstractMatcher<T> {
49
50     final Matcher<? super T> a, b;
51
52     public OrMatcher(Matcher<? super T> a, Matcher<? super T> b) {
53       this.a = a;
54       this.b = b;
55     }
56
57     public boolean matches(T t) {
58       return a.matches(t) || b.matches(t);
59     }
60   }
61 }
62
Popular Tags