All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ruediste.salta.matchers.Matchers Maven / Gradle / Ivy

Go to download

Core of the Salta Framework. Provides most of the infrastructure, but no API

There is a newer version: 1.1
Show newest version
package com.github.ruediste.salta.matchers;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

/**
 * Matcher implementations. Supports matching classes and methods.
 *
 * @author [email protected] (Bob Lee)
 */
@SuppressWarnings("rawtypes")
public class Matchers {
    private Matchers() {
    }

    static class AndMatcher implements Matcher, 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 implements Matcher, 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;
    }

    /**
     * Returns a matcher which matches any input.
     */
    public static Matcher any() {
        return ANY;
    }

    private static final Matcher ANY = new Any();

    private static class Any implements Matcher, Serializable {
        @Override
        public boolean matches(Object o) {
            return true;
        }

        @Override
        public String toString() {
            return "any()";
        }

        public Object readResolve() {
            return any();
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Inverts the given matcher.
     */
    public static  Matcher not(final Matcher p) {
        return new Not(p);
    }

    private static class Not implements Matcher, Serializable {
        final Matcher delegate;

        private Not(Matcher delegate) {
            this.delegate = checkNotNull(delegate, "delegate");
        }

        @Override
        public boolean matches(T t) {
            return !delegate.matches(t);
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof Not && ((Not) other).delegate.equals(delegate);
        }

        @Override
        public int hashCode() {
            return -delegate.hashCode();
        }

        @Override
        public String toString() {
            return "not(" + delegate + ")";
        }

        private static final long serialVersionUID = 0;
    }

    private static void checkForRuntimeRetention(Class annotationType) {
        Retention retention = annotationType.getAnnotation(Retention.class);
        checkArgument(retention != null && retention.value() == RetentionPolicy.RUNTIME,
                "Annotation %s is missing RUNTIME retention", annotationType.getSimpleName());
    }

    /**
     * Returns a matcher which matches elements (methods, classes, etc.) with a
     * given annotation.
     */
    public static Matcher annotatedWith(final Class annotationType) {
        return new AnnotatedWithType(annotationType);
    }

    private static class AnnotatedWithType implements Matcher, Serializable {
        private final Class annotationType;

        public AnnotatedWithType(Class annotationType) {
            this.annotationType = checkNotNull(annotationType, "annotation type");
            checkForRuntimeRetention(annotationType);
        }

        @Override
        public boolean matches(AnnotatedElement element) {
            return element.isAnnotationPresent(annotationType);
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof AnnotatedWithType
                    && ((AnnotatedWithType) other).annotationType.equals(annotationType);
        }

        @Override
        public int hashCode() {
            return 37 * annotationType.hashCode();
        }

        @Override
        public String toString() {
            return "annotatedWith(" + annotationType.getSimpleName() + ".class)";
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Returns a matcher which matches elements (methods, classes, etc.) with a
     * given annotation.
     */
    public static Matcher annotatedWith(final Annotation annotation) {
        return new AnnotatedWith(annotation);
    }

    private static class AnnotatedWith implements Matcher, Serializable {
        private final Annotation annotation;

        public AnnotatedWith(Annotation annotation) {
            this.annotation = checkNotNull(annotation, "annotation");
            checkForRuntimeRetention(annotation.annotationType());
        }

        @Override
        public boolean matches(AnnotatedElement element) {
            Annotation fromElement = element.getAnnotation(annotation.annotationType());
            return fromElement != null && annotation.equals(fromElement);
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof AnnotatedWith && ((AnnotatedWith) other).annotation.equals(annotation);
        }

        @Override
        public int hashCode() {
            return 37 * annotation.hashCode();
        }

        @Override
        public String toString() {
            return "annotatedWith(" + annotation + ")";
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Returns a matcher which matches subclasses of the given type (as well as
     * the given type).
     */
    public static Matcher subclassesOf(final Class superclass) {
        return new SubclassesOf(superclass);
    }

    private static class SubclassesOf implements Matcher, Serializable {
        private final Class superclass;

        public SubclassesOf(Class superclass) {
            this.superclass = checkNotNull(superclass, "superclass");
        }

        @Override
        public boolean matches(Class subclass) {
            return superclass.isAssignableFrom(subclass);
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof SubclassesOf && ((SubclassesOf) other).superclass.equals(superclass);
        }

        @Override
        public int hashCode() {
            return 37 * superclass.hashCode();
        }

        @Override
        public String toString() {
            return "subclassesOf(" + superclass.getSimpleName() + ".class)";
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Returns a matcher which matches objects equal to the given object.
     */
    public static Matcher only(Object value) {
        return new Only(value);
    }

    private static class Only implements Matcher, Serializable {
        private final Object value;

        public Only(Object value) {
            this.value = checkNotNull(value, "value");
        }

        @Override
        public boolean matches(Object other) {
            return value.equals(other);
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof Only && ((Only) other).value.equals(value);
        }

        @Override
        public int hashCode() {
            return 37 * value.hashCode();
        }

        @Override
        public String toString() {
            return "only(" + value + ")";
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Returns a matcher which matches only the given object.
     */
    public static Matcher identicalTo(final Object value) {
        return new IdenticalTo(value);
    }

    private static class IdenticalTo implements Matcher, Serializable {
        private final Object value;

        public IdenticalTo(Object value) {
            this.value = checkNotNull(value, "value");
        }

        @Override
        public boolean matches(Object other) {
            return value == other;
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof IdenticalTo && ((IdenticalTo) other).value == value;
        }

        @Override
        public int hashCode() {
            return 37 * System.identityHashCode(value);
        }

        @Override
        public String toString() {
            return "identicalTo(" + value + ")";
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Returns a matcher which matches classes in the given package. Packages
     * are specific to their classloader, so classes with the same package name
     * may not have the same package at runtime.
     */
    public static Matcher inPackage(final Package targetPackage) {
        return new InPackage(targetPackage);
    }

    private static class InPackage implements Matcher, Serializable {
        private final transient Package targetPackage;
        private final String packageName;

        public InPackage(Package targetPackage) {
            this.targetPackage = checkNotNull(targetPackage, "package");
            this.packageName = targetPackage.getName();
        }

        @Override
        public boolean matches(Class c) {
            return c.getPackage().equals(targetPackage);
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof InPackage && ((InPackage) other).targetPackage.equals(targetPackage);
        }

        @Override
        public int hashCode() {
            return 37 * targetPackage.hashCode();
        }

        @Override
        public String toString() {
            return "inPackage(" + targetPackage.getName() + ")";
        }

        public Object readResolve() {
            return inPackage(Package.getPackage(packageName));
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Returns a matcher which matches classes in the given package and its
     * subpackages. Unlike {@link #inPackage(Package) inPackage()}, this matches
     * classes from any classloader.
     * 
     * @since 2.0
     */
    public static Matcher inSubpackage(final String targetPackageName) {
        return new InSubpackage(targetPackageName);
    }

    private static class InSubpackage implements Matcher, Serializable {
        private final String targetPackageName;

        public InSubpackage(String targetPackageName) {
            this.targetPackageName = targetPackageName;
        }

        @Override
        public boolean matches(Class c) {
            String classPackageName = c.getPackage().getName();
            return classPackageName.equals(targetPackageName) || classPackageName.startsWith(targetPackageName + ".");
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof InSubpackage && ((InSubpackage) other).targetPackageName.equals(targetPackageName);
        }

        @Override
        public int hashCode() {
            return 37 * targetPackageName.hashCode();
        }

        @Override
        public String toString() {
            return "inSubpackage(" + targetPackageName + ")";
        }

        private static final long serialVersionUID = 0;
    }

    /**
     * Returns a matcher which matches methods with matching return types.
     */
    public static Matcher returns(final Matcher> returnType) {
        return new Returns(returnType);
    }

    private static class Returns implements Matcher, Serializable {
        private final Matcher> returnType;

        public Returns(Matcher> returnType) {
            this.returnType = checkNotNull(returnType, "return type matcher");
        }

        @Override
        public boolean matches(Method m) {
            return returnType.matches(m.getReturnType());
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof Returns && ((Returns) other).returnType.equals(returnType);
        }

        @Override
        public int hashCode() {
            return 37 * returnType.hashCode();
        }

        @Override
        public String toString() {
            return "returns(" + returnType + ")";
        }

        private static final long serialVersionUID = 0;
    }
}