com.tngtech.archunit.base.DescribedPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of archunit Show documentation
Show all versions of archunit Show documentation
A Java architecture test library, to specify and assert architecture rules in plain Java - Module 'archunit'
/*
* 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