org.conqat.lib.commons.collections.BasicPatternList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
/*
* 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 org.conqat.lib.commons.collections;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.test.IndexValueClass;
/**
* A list of RegEx pattern. the reason for this name ("Basic") is that it contains slightly less
* functions that the version found in the ConQAT engine.
*/
@IndexValueClass
public class BasicPatternList extends ArrayList implements Serializable {
/** Version used for serialization. */
private static final long serialVersionUID = 1;
/** Creates an empty {@link BasicPatternList} */
public BasicPatternList() {
// do nothing
}
/** Creates a {@link BasicPatternList} with the collection of patterns */
public BasicPatternList(Collection patterns) {
super(patterns);
}
/** Creates a pattern list for the specified patterns. */
public BasicPatternList(Pattern... patterns) {
this(Arrays.asList(patterns));
}
/**
* Returns true, if the pattern list is empty or {@link #matchesAny(String)} returns true.
*/
public boolean emptyOrMatchesAny(String s) {
return isEmpty() || matchesAny(s);
}
/**
* Returns whether the given string matches at least one of the contained pattern. For this the
* Matcher.matches()
method is used.
*/
public boolean matchesAny(String s) {
for (Pattern p : this) {
CCSMAssert.isNotNull(p, "Encountered null pattern!");
if (p.matcher(s).matches()) {
return true;
}
}
return false;
}
/**
* Returns whether in the given string at least one of the contained pattern is found. For this the
* Matcher.find()
method is used.
*/
public boolean findsAnyIn(String s) {
for (Pattern p : this) {
CCSMAssert.isNotNull(p, "Encountered null pattern!");
if (p.matcher(s).find()) {
return true;
}
}
return false;
}
/**
* Returns a list with the regular expressions from which the patters were compiled
*/
public List asStringList() {
List patterns = new ArrayList<>();
for (Pattern pattern : this) {
patterns.add(pattern.pattern());
}
return patterns;
}
}