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

com.tngtech.archunit.base.DescribedPredicate Maven / Gradle / Ivy

Go to download

A Java architecture test library, to specify and assert architecture rules in plain Java - Module 'archunit'

There is a newer version: 1.3.0
Show newest version
/*
 * Copyright 2017 TNG Technology Consulting GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tngtech.archunit.base;

import com.tngtech.archunit.PublicAPI;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.tngtech.archunit.PublicAPI.Usage.INHERITANCE;

/**
 * A predicate holding a description.
 *
 * @param  The type of objects the predicate applies to
 */
@PublicAPI(usage = INHERITANCE)
public abstract class DescribedPredicate {
    private final String description;

    public abstract boolean apply(T input);

    public DescribedPredicate(String description, Object... params) {
        checkArgument(description != null, "Description must be set");
        this.description = String.format(description, params);
    }

    public String getDescription() {
        return description;
    }

    public DescribedPredicate as(String description, Object... params) {
        return new DescribedPredicate(description, params) {
            @Override
            public boolean apply(T input) {
                return DescribedPredicate.this.apply(input);
            }
        };
    }

    public DescribedPredicate and(final DescribedPredicate other) {
        return new DescribedPredicate(description + " and " + other.getDescription()) {
            @Override
            public boolean apply(T input) {
                return DescribedPredicate.this.apply(input) && other.apply(input);
            }
        };
    }

    public DescribedPredicate or(final DescribedPredicate other) {
        return new DescribedPredicate(description + " or " + other.getDescription()) {
            @Override
            public boolean apply(T input) {
                return DescribedPredicate.this.apply(input) || other.apply(input);
            }
        };
    }

    public  DescribedPredicate onResultOf(final Function function) {
        checkNotNull(function);
        return new DescribedPredicate(description) {
            @Override
            public boolean apply(F input) {
                return DescribedPredicate.this.apply(function.apply(input));
            }
        };
    }

    /**
     * Workaround for the limitations of the Java type system {@code ->} Can't specify this contravariant type at the language level
     */
    @SuppressWarnings("unchecked") // DescribedPredicate is contravariant
    public  DescribedPredicate forSubType() {
        return (DescribedPredicate) this;
    }

    @Override
    public String toString() {
        return getDescription();
    }

    @SuppressWarnings("unchecked")
    public static  DescribedPredicate alwaysTrue() {
        return (DescribedPredicate) ALWAYS_TRUE;
    }

    private static final DescribedPredicate ALWAYS_TRUE = new DescribedPredicate("always true") {
        @Override
        public boolean apply(Object input) {
            return true;
        }
    };

    @SuppressWarnings("unchecked")
    public static  DescribedPredicate alwaysFalse() {
        return (DescribedPredicate) ALWAYS_FALSE;
    }

    private static final DescribedPredicate ALWAYS_FALSE = new DescribedPredicate("always false") {
        @Override
        public boolean apply(Object input) {
            return false;
        }
    };

    public static  DescribedPredicate equalTo(final T object) {
        checkNotNull(object);
        return new DescribedPredicate("equal to '%s'", object) {
            @Override
            public boolean apply(T input) {
                return object.equals(input);
            }
        };
    }

    public static  DescribedPredicate doesnt(final DescribedPredicate predicate) {
        return not(predicate).as("doesn't %s", predicate.getDescription());
    }

    public static  DescribedPredicate dont(final DescribedPredicate predicate) {
        return not(predicate).as("don't %s", predicate.getDescription());
    }

    public static  DescribedPredicate not(final DescribedPredicate predicate) {
        checkNotNull(predicate);
        return new DescribedPredicate("not " + predicate.getDescription()) {
            @Override
            public boolean apply(T input) {
                return !predicate.apply(input);
            }
        };
    }
}