Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement and and or in Matcher directly #1716

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 7 additions & 71 deletions core/src/com/google/inject/matcher/AbstractMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> implements Matcher<T> {

@Override
public Matcher<T> and(final Matcher<? super T> other) {
return new AndMatcher<T>(this, other);
return Matcher.super.and(other);
}

@Override
public Matcher<T> or(Matcher<? super T> other) {
return new OrMatcher<T>(this, other);
}

private static class AndMatcher<T> extends AbstractMatcher<T> implements Serializable {
private final Matcher<? super T> a, b;

public AndMatcher(Matcher<? super T> a, Matcher<? super T> 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<T> extends AbstractMatcher<T> implements Serializable {
private final Matcher<? super T> a, b;

public OrMatcher(Matcher<? super T> a, Matcher<? super T> 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);
}
}
8 changes: 6 additions & 2 deletions core/src/com/google/inject/matcher/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ public interface Matcher<T> {
* Returns a new matcher which returns {@code true} if both this and the given matcher return
* {@code true}.
*/
Matcher<T> and(Matcher<? super T> other);
default Matcher<T> and(Matcher<? super T> other) {
return new Matchers.AndMatcher<T>(this, other);
}

/**
* Returns a new matcher which returns {@code true} if either this or the given matcher return
* {@code true}.
*/
Matcher<T> or(Matcher<? super T> other);
default Matcher<T> or(Matcher<? super T> other) {
return new Matchers.OrMatcher<T>(this, other);
}
}
66 changes: 66 additions & 0 deletions core/src/com/google/inject/matcher/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,70 @@ public String toString() {

private static final long serialVersionUID = 0;
}

static class AndMatcher<T> extends AbstractMatcher<T> implements Serializable {
private final Matcher<? super T> a, b;

public AndMatcher(Matcher<? super T> a, Matcher<? super T> 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<T> extends AbstractMatcher<T> implements Serializable {
private final Matcher<? super T> a, b;

public OrMatcher(Matcher<? super T> a, Matcher<? super T> 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;
}
}
Loading