001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.collections; 018 019import java.io.Serializable; 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.Collection; 023import java.util.List; 024import java.util.regex.Pattern; 025 026import org.conqat.lib.commons.assertion.CCSMAssert; 027 028/** 029 * A list of RegEx pattern. the reason for this name ("Basic") is that it 030 * contains slightly less functions that the version found in the ConQAT engine. 031 */ 032public class BasicPatternList extends ArrayList<Pattern> implements Serializable { 033 034 /** Version used for serialization. */ 035 private static final long serialVersionUID = 1; 036 037 /** Creates an empty {@link BasicPatternList} */ 038 public BasicPatternList() { 039 // do nothing 040 } 041 042 /** Creates a {@link BasicPatternList} with the collection of patterns */ 043 public BasicPatternList(Collection<? extends Pattern> patterns) { 044 super(patterns); 045 } 046 047 /** Creates a pattern list for the specified patterns. */ 048 public BasicPatternList(Pattern... patterns) { 049 this(Arrays.asList(patterns)); 050 } 051 052 /** 053 * Returns true, if the pattern list is empty or {@link #matchesAny(String)} 054 * returns true. 055 */ 056 public boolean emptyOrMatchesAny(String s) { 057 return isEmpty() || matchesAny(s); 058 } 059 060 /** 061 * Returns whether the given string matches at least one of the contained 062 * pattern. For this the <code>Matcher.matches()</code> method is used. 063 */ 064 public boolean matchesAny(String s) { 065 for (Pattern p : this) { 066 CCSMAssert.isNotNull(p, "Encountered null pattern!"); 067 068 if (p.matcher(s).matches()) { 069 return true; 070 } 071 } 072 return false; 073 } 074 075 /** 076 * Returns whether in the given string at least one of the contained pattern is 077 * found. For this the <code>Matcher.find()</code> method is used. 078 */ 079 public boolean findsAnyIn(String s) { 080 for (Pattern p : this) { 081 CCSMAssert.isNotNull(p, "Encountered null pattern!"); 082 083 if (p.matcher(s).find()) { 084 return true; 085 } 086 } 087 return false; 088 } 089 090 /** 091 * Returns a list with the regular expressions from which the patters were 092 * compiled 093 */ 094 public List<String> asStringList() { 095 List<String> patterns = new ArrayList<>(); 096 for (Pattern pattern : this) { 097 patterns.add(pattern.pattern()); 098 } 099 return patterns; 100 } 101}