Skip to content

Commit

Permalink
Implement and and or in Matcher directly
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Apr 29, 2023
1 parent 99d0e11 commit 6965a27
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 79 deletions.
77 changes: 0 additions & 77 deletions core/src/com/google/inject/matcher/AbstractMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,11 @@

package com.google.inject.matcher;

import java.io.Serializable;

/**
* Implements {@code and()} and {@code or()}.
*
* @author [email protected] (Bob Lee)
*/
public abstract class AbstractMatcher<T> implements Matcher<T> {

@Override
public Matcher<T> and(final Matcher<? super T> other) {
return new AndMatcher<T>(this, 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;
}
}
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;
}
}

0 comments on commit 6965a27

Please sign in to comment.