Class ContainsFilter
java.lang.Object
com.invirgance.convirgance.transform.filter.ContainsFilter
- All Implemented Interfaces:
Filter,Transformer,Predicate<JSONObject>
Filters out JSONObjects based on string containment comparison.
This class is useful for scenarios where large datasets are being processed, and we need to filter records based on partial string matches. For example, finding all records where an industry field contains "Services" as part of its value.
Example use case:
String key = "industry";
String value = "Services";
ArrayList<JSONObject> wanted = new ArrayList<JSONObject>();
FileSource source = new FileSource("generic_data_sorted_id.json");
Iterator<JSONObject> records = new JSONInput().read(source).iterator();
ContainsFilter filter = new ContainsFilter(key, value);
Iterator<JSONObject> filtered = filter.transform(records);
Important notes:
- The filter performs case-sensitive containment checks
- Null values in either the record or comparison value will result in the test returning false
- Both the record value and comparison value are converted to strings before comparison
- Supports
ValueGeneratoras a value.
- Author:
- tadghh
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new ContainsFilter.ContainsFilter(String key, Object value) Creates a new ContainsFilter with the specified key and value to check for containment. -
Method Summary
Modifier and TypeMethodDescriptiongetKey()Gets the comparison key in use.getValue()Retrieves the value used for the key containment check.voidSet the comparison key.voidSets the value that will be checked for containment within the specified key's value.booleantest(JSONObject record) Tests if the value of the specified key in the record contains the comparison value.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
-
ContainsFilter
public ContainsFilter()Creates a new ContainsFilter. -
ContainsFilter
Creates a new ContainsFilter with the specified key and value to check for containment.- Parameters:
key- The key to evaluate in the JSONObject.value- The value to check for containment.
-
-
Method Details
-
getKey
Gets the comparison key in use.- Returns:
- The key.
-
setKey
Set the comparison key.- Parameters:
key- The key.
-
getValue
Retrieves the value used for the key containment check.- Returns:
- The filter value.
-
setValue
Sets the value that will be checked for containment within the specified key's value.- Parameters:
value- The substring to search for.
-
test
Tests if the value of the specified key in the record contains the comparison value. Both values are converted to strings for the containment check.- Specified by:
testin interfaceFilter- Specified by:
testin interfacePredicate<JSONObject>- Parameters:
record- The JSONObject to evaluate.- Returns:
- True if the string representation of the record's value contains the string representation of the comparison value, false otherwise.
-