Class ContainsFilter

java.lang.Object
com.invirgance.convirgance.transform.filter.ContainsFilter
All Implemented Interfaces:
Filter, Transformer, Predicate<JSONObject>

@Wiring public class ContainsFilter extends Object implements Filter
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 ValueGenerator as a value.
Author:
tadghh
See Also:
  • Constructor Details

    • ContainsFilter

      public ContainsFilter()
      Creates a new ContainsFilter.
    • ContainsFilter

      public ContainsFilter(String key, Object value)
      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

      public String getKey()
      Gets the comparison key in use.
      Returns:
      The key.
    • setKey

      public void setKey(String key)
      Set the comparison key.
      Parameters:
      key - The key.
    • getValue

      public Object getValue()
      Retrieves the value used for the key containment check.
      Returns:
      The filter value.
    • setValue

      public void setValue(Object value)
      Sets the value that will be checked for containment within the specified key's value.
      Parameters:
      value - The substring to search for.
    • test

      public boolean test(JSONObject record)
      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:
      test in interface Filter
      Specified by:
      test in interface Predicate<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.