Class LikeFilter
java.lang.Object
com.invirgance.convirgance.transform.filter.LikeFilter
- All Implemented Interfaces:
Filter,Transformer,Predicate<JSONObject>
Filter out JSONObjects by using a SQL-esque Like operator.
This class is useful for scenarios where large datasets are being processed, and we need to filter records based on like patterns. For example, finding all records where an industry field contains "Government" as part of its pattern.
Example use case:
String key = "industry";
String pattern = "_o__rnment%io_";
ArrayList<JSONObject> wanted = new ArrayList<JSONObject>();
FileSource source = new FileSource("src/test/resources/generic/generic_data_sorted_id.json");
Iterator<JSONObject> records = new JSONInput().read(source).iterator();
LikeFilter filter = new LikeFilter(key, pattern);
Iterator<JSONObject> filtered = filter.transform(records);
- Author:
- tadghh
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new LikeFilter.LikeFilter(String key, String pattern) Creates a new LikeFilter with the specified key and pattern to pattern match with. -
Method Summary
Modifier and TypeMethodDescriptiongetKey()Gets the comparison key in use.Retrieves the pattern used for comparison.charGets the current character used as the pattern wildcard.voidSet the comparison key.voidsetPattern(String value) Sets the value that will be used as a pattern to match with.voidsetWildcard(char value) Sets the character that will be the 'wildcard' in the pattern.booleantest(JSONObject record) Tests if the given record meets the filter condition.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.invirgance.convirgance.transform.Transformer
transform
-
Constructor Details
-
LikeFilter
public LikeFilter()Creates a new LikeFilter. -
LikeFilter
Creates a new LikeFilter with the specified key and pattern to pattern match with.- Parameters:
key- The key to evaluate in the JSONObject.pattern- The pattern to match key values with.
-
-
Method Details
-
getKey
Gets the comparison key in use.- Returns:
- The key.
-
setKey
Set the comparison key.- Parameters:
key- The key.
-
setPattern
Sets the value that will be used as a pattern to match with. You can use an alternative wildcard if needed. Supports wildcards '%' (can be overridden) and '_'- Parameters:
value- The pattern to match values with.
-
getPattern
Retrieves the pattern used for comparison.- Returns:
- The pattern pattern.
-
setWildcard
public void setWildcard(char value) Sets the character that will be the 'wildcard' in the pattern.- Parameters:
value- Any character
-
getWildcard
public char getWildcard()Gets the current character used as the pattern wildcard.- Returns:
- The wildcard character
-
test
Description copied from interface:FilterTests if the given record meets the filter condition.- Specified by:
testin interfaceFilter- Specified by:
testin interfacePredicate<JSONObject>- Parameters:
record- The JSONObject to test.- Returns:
- If the record passed or failed.
-