eu.cqse.check.framework.scanner.OrTokenMatcher Maven / Gradle / Ivy
/*
* 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} matching when any of the provided matchers matches.
*
* @see ITokenMatcher#or(ITokenMatcher...)
* @see ITokenMatcher#or(Collection)
*/
/* package */ class OrTokenMatcher implements ITokenMatcher {
private final List matchers;
public OrTokenMatcher(Collection extends ITokenMatcher> 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().anyMatch(mather -> mather.matches(token));
}
@Override
public ITokenMatcher or(ITokenMatcher... further) {
return ITokenMatcher.or(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