Class FilteredIterableSet<T,I extends Iterable<T>>

java.lang.Object
com.github.tommyettinger.ds.ObjectSet<I>
com.github.tommyettinger.ds.FilteredIterableSet<T,I>
All Implemented Interfaces:
EnhancedCollection<I>, Iterable<I>, Collection<I>, Set<I>

public class FilteredIterableSet<T,I extends Iterable<T>> extends ObjectSet<I>
A customizable variant on ObjectSet that uses Iterable items made of T sub-items, and only considers a sub-item (for equality and hashing purposes) if that sub-item satisfies a predicate. This can also edit the sub-items that pass the filter, such as by normalizing their data during comparisons (and hashing). You will usually want to call setFilter(ObjPredicate) and/or setEditor(ObjToSameFunction) to change the behavior of hashing and equality before you enter any items, unless you have specified the filter and/or editor you want in the constructor. Calling setModifiers(ObjPredicate, ObjToSameFunction) is recommended if you need to set both the filter and the editor; you could also set them in the constructor.
This class is related to FilteredStringSet, which can be seen as using a String as an item and the characters of that String as its sub-items. That means this is also similar to CaseInsensitiveSet, which is essentially a specialized version of FilteredIterableSet (which can be useful for serialization).
  • Field Details

    • filter

      protected com.github.tommyettinger.function.ObjPredicate<T> filter
    • editor

      protected com.github.tommyettinger.function.ObjToSameFunction<T> editor
  • Constructor Details

    • FilteredIterableSet

      public FilteredIterableSet()
      Creates a new set with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor(). This considers all sub-items in an Iterable item and does not edit any sub-items.
    • FilteredIterableSet

      public FilteredIterableSet(int initialCapacity)
      Creates a new set with the specified initial capacity and a load factor of Utilities.getDefaultLoadFactor(). This set will hold initialCapacity items before growing the backing table. This considers all sub-items in an Iterable item and does not edit any sub-items.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • FilteredIterableSet

      public FilteredIterableSet(int initialCapacity, float loadFactor)
      Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table. This considers all sub-items in an Iterable item and does not edit any sub-items.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
    • FilteredIterableSet

      public FilteredIterableSet(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor)
      Creates a new set with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor(). This uses the specified filter and editor.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
    • FilteredIterableSet

      public FilteredIterableSet(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, int initialCapacity)
      Creates a new set with the specified initial capacity and a load factor of Utilities.getDefaultLoadFactor(). This set will hold initialCapacity items before growing the backing table. This uses the specified filter and editor.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • FilteredIterableSet

      public FilteredIterableSet(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, int initialCapacity, float loadFactor)
      Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table. This uses the specified filter and editor.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
    • FilteredIterableSet

      public FilteredIterableSet(FilteredIterableSet<T,? extends I> set)
      Creates a new set identical to the specified set.
      Parameters:
      set - another FilteredIterableSet to copy
    • FilteredIterableSet

      public FilteredIterableSet(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, Collection<? extends I> coll)
      Creates a new set that contains all distinct elements in coll. This uses the specified filter and editor, including while it enters the items in coll.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
      coll - a Collection implementation to copy, such as an ObjectList or a Set that isn't a FilteredIterableSet
    • FilteredIterableSet

      public FilteredIterableSet(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I[] array, int offset, int length)
      Creates a new set using length items from the given array, starting at offset (inclusive). This uses the specified filter and editor, including while it enters the items in array.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
      array - an array to draw items from
      offset - the first index in array to draw an item from
      length - how many items to take from array; bounds-checking is the responsibility of the using code
    • FilteredIterableSet

      public FilteredIterableSet(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I[] array)
      Creates a new set containing all the items in the given array. This uses the specified filter and editor, including while it enters the items in array.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
      array - an array that will be used in full, except for duplicate items
  • Method Details

    • getFilter

      public com.github.tommyettinger.function.ObjPredicate<T> getFilter()
    • setFilter

      public FilteredIterableSet<T,I> setFilter(com.github.tommyettinger.function.ObjPredicate<T> filter)
      Sets the filter that determines which sub-items in an Iterable are considered for equality and hashing, then returns this object, for chaining. ObjPredicate filters could be lambdas or method references that take a sub-item and return true if that sub-item will be used for hashing/equality, or return false to ignore it. The default filter always returns true. If the filter changes, that invalidates anything previously entered into this, so before changing the filter this clears the entire data structure, removing all existing items.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      Returns:
      this, for chaining
    • getEditor

      public com.github.tommyettinger.function.ObjToSameFunction<T> getEditor()
    • setEditor

      public FilteredIterableSet<T,I> setEditor(com.github.tommyettinger.function.ObjToSameFunction<T> editor)
      Sets the editor that can alter the sub-items in an Iterable item when they are being used for equality and hashing. This does not apply any changes to the items in this data structure; it only affects how they are hashed or compared. An editor could be a lambda or method reference; the only real requirement is that it takes a T sub-item and returns a T sub-item. The default filter returns the sub-item it is passed without changes. If the editor changes, that invalidates anything previously entered into this, so before changing the editor this clears the entire data structure, removing all existing items.
      Parameters:
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
      Returns:
      this, for chaining
    • setModifiers

      public FilteredIterableSet<T,I> setModifiers(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor)
      Equivalent to calling mySet.setFilter(filter).setEditor(editor), but only clears the data structure once.
      Parameters:
      filter - a ObjPredicate that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction that will be given a sub-item and may return a potentially different T sub-item
      Returns:
      this, for chaining
      See Also:
    • hashHelper

      protected int hashHelper(I s)
    • place

      protected int place(Object item)
      Description copied from class: ObjectSet
      Returns an index >= 0 and <= ObjectSet.mask for the specified item, mixed.
      Overrides:
      place in class ObjectSet<I extends Iterable<T>>
      Parameters:
      item - a non-null Object; its hashCode() method should be used by most implementations
      Returns:
      an index between 0 and ObjectSet.mask (both inclusive)
    • equate

      public boolean equate(Object left, Object right)
      Compares two objects for equality by the rules this filtered data structure uses for keys. This will return true if the arguments are reference-equivalent or both null. Otherwise, it requires that both are Iterables and compares them using the filter and editor of this object.
      Overrides:
      equate in class ObjectSet<I extends Iterable<T>>
      Parameters:
      left - must be non-null; typically a key being compared, but not necessarily
      right - may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
      Returns:
      true if left and right are equivalent according to the rules this filtered type uses
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<T>
      Specified by:
      hashCode in interface Set<T>
      Overrides:
      hashCode in class ObjectSet<I extends Iterable<T>>
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor)
      Constructs a new FilteredIterableSet with the given filter and editor, without contents, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      Returns:
      a new FilteredIterableSet containing nothing
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts item into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item - the one item to initially include in the set
      Returns:
      a new FilteredIterableSet containing item
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item0, I item1)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts items into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item0 - an Iterable of T to initially include in the set
      item1 - an Iterable of T to initially include in the set
      Returns:
      a new FilteredIterableSet containing the given items
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item0, I item1, I item2)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts items into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item0 - an Iterable of T to initially include in the set
      item1 - an Iterable of T to initially include in the set
      item2 - an Iterable of T to initially include in the set
      Returns:
      a new FilteredIterableSet containing the given items
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item0, I item1, I item2, I item3)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts items into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item0 - an Iterable of T to initially include in the set
      item1 - an Iterable of T to initially include in the set
      item2 - an Iterable of T to initially include in the set
      item3 - an Iterable of T to initially include in the set
      Returns:
      a new FilteredIterableSet containing the given items
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item0, I item1, I item2, I item3, I item4)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts items into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item0 - an Iterable of T to initially include in the set
      item1 - an Iterable of T to initially include in the set
      item2 - an Iterable of T to initially include in the set
      item3 - an Iterable of T to initially include in the set
      item4 - an Iterable of T to initially include in the set
      Returns:
      a new FilteredIterableSet containing the given items
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item0, I item1, I item2, I item3, I item4, I item5)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts items into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item0 - an Iterable of T to initially include in the set
      item1 - an Iterable of T to initially include in the set
      item2 - an Iterable of T to initially include in the set
      item3 - an Iterable of T to initially include in the set
      item4 - an Iterable of T to initially include in the set
      item5 - an Iterable of T to initially include in the set
      Returns:
      a new FilteredIterableSet containing the given items
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item0, I item1, I item2, I item3, I item4, I item5, I item6)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts items into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item0 - an Iterable of T to initially include in the set
      item1 - an Iterable of T to initially include in the set
      item2 - an Iterable of T to initially include in the set
      item3 - an Iterable of T to initially include in the set
      item4 - an Iterable of T to initially include in the set
      item5 - an Iterable of T to initially include in the set
      item6 - an Iterable of T to initially include in the set
      Returns:
      a new FilteredIterableSet containing the given items
    • with

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I item0, I item1, I item2, I item3, I item4, I item5, I item6, I item7)
      Constructs a new FilteredIterableSet with the given filter and editor, inserts items into it, and returns the set.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      item0 - an Iterable of T to initially include in the set
      item1 - an Iterable of T to initially include in the set
      item2 - an Iterable of T to initially include in the set
      item3 - an Iterable of T to initially include in the set
      item4 - an Iterable of T to initially include in the set
      item5 - an Iterable of T to initially include in the set
      item6 - an Iterable of T to initially include in the set
      item7 - an Iterable of T to initially include in the set
      Returns:
      a new FilteredIterableSet containing the given items
    • with

      @SafeVarargs public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> with(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, I... items)
      This is the same as FilteredIterableSet(ObjPredicate, ObjToSameFunction, Iterable[]), but can take the array argument as either an array or as varargs. It can be useful for code-generation scenarios.
      Type Parameters:
      T - the type of sub-items
      I - the type of items, which must be either Iterable or an implementing class, containing T sub-items
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      items - an array or varargs of I that will be used in the new set
      Returns:
      a new FilteredIterableSet containing the entirety of items, as the filter and editor permit
    • parse

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> parse(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, String str, String delimiter, PartialParser<I> parser)
      Type Parameters:
      T - the type of item in each Iterable
      I - the Iterable of T type this holds
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      str - a String that will be parsed in full
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns an I item from a section of str
      Returns:
      a new collection parsed from str
    • parse

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> parse(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, String str, String delimiter, PartialParser<I> parser, boolean brackets)
      Creates a new HolderSet using extractor and fills it by calling EnhancedCollection.addLegible(String, String, PartialParser, int, int) on either all of str (if brackets is false) or str without its first and last chars (if brackets is true). Each item is expected to be separated by delimiter.
      Type Parameters:
      T - the type of item in each Iterable
      I - the Iterable of T type this holds
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      str - a String that will be parsed in full (depending on brackets)
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns an I item from a section of str
      brackets - if true, the first and last chars in str will be ignored
      Returns:
      a new collection parsed from str
    • parse

      public static <T, I extends Iterable<T>> FilteredIterableSet<T,I> parse(com.github.tommyettinger.function.ObjPredicate<T> filter, com.github.tommyettinger.function.ObjToSameFunction<T> editor, String str, String delimiter, PartialParser<I> parser, int offset, int length)
      Creates a new HolderSet using extractor and fills it by calling EnhancedCollection.addLegible(String, String, PartialParser, int, int) with the other five parameters as-is.
      Type Parameters:
      T - the type of item in each Iterable
      I - the Iterable of T type this holds
      Parameters:
      filter - a ObjPredicate<T> that should return true iff a sub-item should be considered for equality/hashing
      editor - a ObjToSameFunction<T> that will be given a sub-item and may return a potentially different T sub-item
      str - a String that will have the given section parsed
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns an I item from a section of str
      offset - the first position to parse in str, inclusive
      length - how many chars to parse, starting from offset
      Returns:
      a new collection parsed from str