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

eu.cqse.check.framework.scanner.AndTokenMatcher Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) CQSE 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 eu.cqse.check.framework.scanner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * {@link ITokenMatcher} only matching when all the provided matchers match.
 *
 * @see ITokenMatcher#and(ITokenMatcher...)
 * @see ITokenMatcher#and(Collection)
 */
/* package */ class AndTokenMatcher implements ITokenMatcher {

	private final List matchers;

	public AndTokenMatcher(Collection matchers) {
		Objects.requireNonNull(matchers, "matchers");
		if (matchers.isEmpty()) {
			throw new IllegalArgumentException("matchers must not be empty");
		}
		this.matchers = new ArrayList<>(matchers);
	}

	@Override
	public boolean matches(IToken token) {
		return matchers.stream().allMatch(mather -> mather.matches(token));
	}

	@Override
	public ITokenMatcher and(ITokenMatcher... further) {
		return ITokenMatcher.and(Stream.concat(matchers.stream(), Arrays.stream(further)).toList());
	}

	@Override
	public String humanReadable() {
		return matchers.stream().map(ITokenMatcher::humanReadable).collect(Collectors.joining(" && ", "(", ")"));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy