From d1d63229b689f3e6894c3b9549d55155cf978554 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Fri, 28 Apr 2023 17:50:23 -0700 Subject: [PATCH] Implement `and` and `or` in `Matcher` directly --- .../inject/matcher/AbstractMatcher.java | 78 ++----------------- .../com/google/inject/matcher/Matcher.java | 8 +- .../com/google/inject/matcher/Matchers.java | 66 ++++++++++++++++ 3 files changed, 79 insertions(+), 73 deletions(-) diff --git a/core/src/com/google/inject/matcher/AbstractMatcher.java b/core/src/com/google/inject/matcher/AbstractMatcher.java index 08209fc33a..0d026372bc 100644 --- a/core/src/com/google/inject/matcher/AbstractMatcher.java +++ b/core/src/com/google/inject/matcher/AbstractMatcher.java @@ -16,88 +16,24 @@ package com.google.inject.matcher; -import java.io.Serializable; - /** * Implements {@code and()} and {@code or()}. * * @author crazybob@google.com (Bob Lee) + * + * @deprecated This class used to be useful to avoid implementing {@code and()} and + * {@code or()} yourself, but is no longer necessary now that {@link Matcher} implements + * these methods. */ +@Deprecated public abstract class AbstractMatcher implements Matcher { - @Override public Matcher and(final Matcher other) { - return new AndMatcher(this, other); + return Matcher.super.and(other); } @Override public Matcher or(Matcher other) { - return new OrMatcher(this, other); - } - - private static class AndMatcher extends AbstractMatcher implements Serializable { - private final Matcher a, b; - - public AndMatcher(Matcher a, Matcher b) { - this.a = a; - this.b = b; - } - - @Override - public boolean matches(T t) { - return a.matches(t) && b.matches(t); - } - - @Override - public boolean equals(Object other) { - return other instanceof AndMatcher - && ((AndMatcher) other).a.equals(a) - && ((AndMatcher) other).b.equals(b); - } - - @Override - public int hashCode() { - return 41 * (a.hashCode() ^ b.hashCode()); - } - - @Override - public String toString() { - return "and(" + a + ", " + b + ")"; - } - - private static final long serialVersionUID = 0; - } - - private static class OrMatcher extends AbstractMatcher implements Serializable { - private final Matcher a, b; - - public OrMatcher(Matcher a, Matcher b) { - this.a = a; - this.b = b; - } - - @Override - public boolean matches(T t) { - return a.matches(t) || b.matches(t); - } - - @Override - public boolean equals(Object other) { - return other instanceof OrMatcher - && ((OrMatcher) other).a.equals(a) - && ((OrMatcher) other).b.equals(b); - } - - @Override - public int hashCode() { - return 37 * (a.hashCode() ^ b.hashCode()); - } - - @Override - public String toString() { - return "or(" + a + ", " + b + ")"; - } - - private static final long serialVersionUID = 0; + return Matcher.super.or(other); } } diff --git a/core/src/com/google/inject/matcher/Matcher.java b/core/src/com/google/inject/matcher/Matcher.java index 6eb81e881a..9cc86c9385 100644 --- a/core/src/com/google/inject/matcher/Matcher.java +++ b/core/src/com/google/inject/matcher/Matcher.java @@ -30,11 +30,15 @@ public interface Matcher { * Returns a new matcher which returns {@code true} if both this and the given matcher return * {@code true}. */ - Matcher and(Matcher other); + default Matcher and(Matcher other) { + return new Matchers.AndMatcher(this, other); + } /** * Returns a new matcher which returns {@code true} if either this or the given matcher return * {@code true}. */ - Matcher or(Matcher other); + default Matcher or(Matcher other) { + return new Matchers.OrMatcher(this, other); + } } diff --git a/core/src/com/google/inject/matcher/Matchers.java b/core/src/com/google/inject/matcher/Matchers.java index 7aaf750e57..2a94cf98e3 100644 --- a/core/src/com/google/inject/matcher/Matchers.java +++ b/core/src/com/google/inject/matcher/Matchers.java @@ -406,4 +406,70 @@ public String toString() { private static final long serialVersionUID = 0; } + + static class AndMatcher extends AbstractMatcher implements Serializable { + private final Matcher a, b; + + public AndMatcher(Matcher a, Matcher b) { + this.a = a; + this.b = b; + } + + @Override + public boolean matches(T t) { + return a.matches(t) && b.matches(t); + } + + @Override + public boolean equals(Object other) { + return other instanceof AndMatcher + && ((AndMatcher) other).a.equals(a) + && ((AndMatcher) other).b.equals(b); + } + + @Override + public int hashCode() { + return 41 * (a.hashCode() ^ b.hashCode()); + } + + @Override + public String toString() { + return "and(" + a + ", " + b + ")"; + } + + private static final long serialVersionUID = 0; + } + + static class OrMatcher extends AbstractMatcher implements Serializable { + private final Matcher a, b; + + public OrMatcher(Matcher a, Matcher b) { + this.a = a; + this.b = b; + } + + @Override + public boolean matches(T t) { + return a.matches(t) || b.matches(t); + } + + @Override + public boolean equals(Object other) { + return other instanceof OrMatcher + && ((OrMatcher) other).a.equals(a) + && ((OrMatcher) other).b.equals(b); + } + + @Override + public int hashCode() { + return 37 * (a.hashCode() ^ b.hashCode()); + } + + @Override + public String toString() { + return "or(" + a + ", " + b + ")"; + } + + private static final long serialVersionUID = 0; + } }