Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.mockrunner.jdbc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.mockrunner.util.common.StringUtil;
/**
* Helper class for finding matching SQL statements based on various
* search parameters. The search parameters are:
*
* caseSensitive do a case sensitive match (default is false)
*
* exactMatch the strings must match exactly, the parameter caseSensitive
* is recognized, but useRegularExpression is irrelevant,
* if exactMatch is true (default is false)
*
* useRegularExpression use regular expressions for matching, if this parameter is
* false, strings match, if one string starts with the other
* (default is false)
*/
public class SQLStatementMatcher
{
private boolean caseSensitive = false;
private boolean exactMatch = false;
private boolean useRegularExpressions = false;
public SQLStatementMatcher(boolean caseSensitive, boolean exactMatch)
{
this(caseSensitive, exactMatch, false);
}
public SQLStatementMatcher(boolean caseSensitive, boolean exactMatch, boolean useRegularExpressions)
{
this.caseSensitive = caseSensitive;
this.exactMatch = exactMatch;
this.useRegularExpressions = useRegularExpressions;
}
/**
* Compares all keys in the specified Map with the
* specified query string using the method {@link #doStringsMatch}.
* If the strings match, the corresponding object from the Map
* is added to the resulting List.
* @param dataMap the source Map
* @param query the query string that must match the keys in dataMap
* @param queryContainsMapData only matters if isExactMatch is false,
* specifies if query must be contained in the Map keys (false)
* or if query must contain the Map keys (true)
* @return the result List
*/
public List getMatchingObjects(Map dataMap, String query, boolean resolveCollection, boolean queryContainsMapData)
{
if(null == query) query = "";
Iterator iterator = dataMap.keySet().iterator();
ArrayList resultList = new ArrayList();
while(iterator.hasNext())
{
String nextKey = (String)iterator.next();
String source, currentQuery;
if(queryContainsMapData)
{
source = query;
currentQuery = nextKey;
}
else
{
source = nextKey;
currentQuery = query;
}
if(doStringsMatch(source, currentQuery))
{
Object matchingObject = dataMap.get(nextKey);
if(resolveCollection && (matchingObject instanceof Collection))
{
resultList.addAll((Collection)matchingObject);
}
else
{
resultList.add(dataMap.get(nextKey));
}
}
}
return resultList;
}
/**
* Compares all elements in the specified Collection with the
* specified query string using the method {@link #doStringsMatch}.
* @param col the Collections
* @param query the query string that must match the keys in col
* @param queryContainsData only matters if exactMatch is false,
* specifies if query must be contained in the Collection data (false)
* or if query must contain the Collection data (true)
* @return true if col contains query, false otherwise
*/
public boolean contains(Collection col, String query, boolean queryContainsData)
{
Iterator iterator = col.iterator();
while(iterator.hasNext())
{
String nextKey = (String)iterator.next();
String source, currentQuery;
if(queryContainsData)
{
source = query;
currentQuery = nextKey;
}
else
{
source = nextKey;
currentQuery = query;
}
if(doStringsMatch(source, currentQuery)) return true;
}
return false;
}
/**
* Compares two strings and returns if they match.
* @param query the query string that must match source
* @param source the source string
* @return true of the strings match, false otherwise
*/
public boolean doStringsMatch(String source, String query)
{
if(null == source) source = "";
if(null == query) query = "";
if(useRegularExpressions && !exactMatch)
{
return doPerl5Match(source, query);
}
else
{
return doSimpleMatch(source, query);
}
}
private boolean doSimpleMatch(String source, String query)
{
if(exactMatch)
{
return StringUtil.matchesExact(source, query, caseSensitive);
}
else
{
return StringUtil.matchesContains(source, query, caseSensitive);
}
}
private boolean doPerl5Match(String source, String query)
{
return StringUtil.matchesPerl5(source, query, caseSensitive);
}
}