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 (at) google.com (Bob Lee) 25 */ 26 public abstract class AbstractMatcher<T> implements Matcher<T> { 27 28 @Override 29 public Matcher<T> and(final Matcher<? super T> other) { 30 return new AndMatcher<T>(this, other); 31 } 32 33 @Override 34 public Matcher<T> or(Matcher<? super T> other) { 35 return new OrMatcher<T>(this, other); 36 } 37 38 private static class AndMatcher<T> extends AbstractMatcher<T> implements Serializable { 39 private final Matcher<? super T> a, b; 40 41 public AndMatcher(Matcher<? super T> a, Matcher<? super T> b) { 42 this.a = a; 43 this.b = b; 44 } 45 46 @Override 47 public boolean matches(T t) { 48 return a.matches(t) && b.matches(t); 49 } 50 51 @Override 52 public boolean equals(Object other) { 53 return other instanceof AndMatcher 54 && ((AndMatcher) other).a.equals(a) 55 && ((AndMatcher) other).b.equals(b); 56 } 57 58 @Override 59 public int hashCode() { 60 return 41 * (a.hashCode() ^ b.hashCode()); 61 } 62 63 @Override 64 public String toString() { 65 return "and(" + a + ", " + b + ")"; 66 } 67 68 private static final long serialVersionUID = 0; 69 } 70 71 private static class OrMatcher<T> extends AbstractMatcher<T> implements Serializable { 72 private final Matcher<? super T> a, b; 73 74 public OrMatcher(Matcher<? super T> a, Matcher<? super T> b) { 75 this.a = a; 76 this.b = b; 77 } 78 79 @Override 80 public boolean matches(T t) { 81 return a.matches(t) || b.matches(t); 82 } 83 84 @Override 85 public boolean equals(Object other) { 86 return other instanceof OrMatcher 87 && ((OrMatcher) other).a.equals(a) 88 && ((OrMatcher) other).b.equals(b); 89 } 90 91 @Override 92 public int hashCode() { 93 return 37 * (a.hashCode() ^ b.hashCode()); 94 } 95 96 @Override 97 public String toString() { 98 return "or(" + a + ", " + b + ")"; 99 } 100 101 private static final long serialVersionUID = 0; 102 } 103 } 104