Class Filter<T>

  • Type Parameters:
    T - Common type of all objects being operated on

    public class Filter<T>
    extends java.lang.Object
    Filter is a utility class for easily sorting and selecting a single object or a group of objects based on simple rules.
    • Constructor Summary

      Constructors 
      Constructor Description
      Filter​(java.util.List<T> items)
      Create a Filter from a list of items
      Filter​(T[] items)
      Create a Filter from an array of items
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void forEach​(java.util.function.BiConsumer<T,​java.lang.Double> consumer)
      Iterate over each item, along with it's score
      void forEach​(java.util.function.Consumer<T> consumer)
      Iterate over each item
      void forEachAboveThreshold​(double threshold, java.util.function.BiConsumer<T,​java.lang.Double> consumer)
      Iterate over each item with a score greater than the threshold
      void forEachAboveThreshold​(double threshold, java.util.function.Consumer<T> consumer)
      Iterate over each item with a score greater than the threshold
      void forEachBelowThreshold​(double threshold, java.util.function.BiConsumer<T,​java.lang.Double> consumer)
      Iterate over each item with a score less than the threshold
      void forEachBelowThreshold​(double threshold, java.util.function.Consumer<T> consumer)
      Iterate over each item with a score less than the threshold
      void forEachRemoved​(java.util.function.Consumer<T> consumer)
      Iterate over each item that has been removed from the Filter
      com.google.common.collect.ImmutableList<T> getAboveThreshold​(double threshold)
      Get an ImmutableList of items that have a score greater than the threshold
      com.google.common.collect.ImmutableList<T> getBelowThreshold​(double threshold)
      Get an ImmutableList of items that have a score less than the threshold
      T getBest()
      Get the best item, or null if none matches that criteria
      int getCount()
      Get the number of items that have not been removed
      com.google.common.collect.ImmutableList<T> getOrdered()
      Get an ordered ImmutableList of all items, sorted by score
      com.google.common.collect.ImmutableList<T> getRemaining()
      Get an ImmutableList of all remaining items
      com.google.common.collect.ImmutableList<T> getRemoved()
      Get an ImmutableList of all removed items
      T getWorst()
      Get the worst item, or null if none matches that criteria
      boolean isEmpty()
      Get if the Filter has no items left in it
      void keepOnly​(java.util.function.BiFunction<T,​java.lang.Double,​java.lang.Boolean> fun)
      Keep any marked items based on their score, remove the rest
      void keepOnly​(java.util.function.Function<T,​java.lang.Boolean> fun)
      Keep only marked items, remove the rest
      void remove​(java.util.function.BiFunction<T,​java.lang.Double,​java.lang.Boolean> fun)
      Remove any marked items based on their score
      void remove​(java.util.function.Function<T,​java.lang.Boolean> fun)
      Remove any marked items
      void remove​(T singleItem)
      Remove a single item
      void reset()
      Resets all scores, and re-adds removed items
      void score​(java.util.function.BiFunction<T,​java.lang.Double,​java.lang.Double> fun)
      Assign a score for each item, based on it's previous score
      void score​(java.util.function.Function<T,​java.lang.Double> fun)
      Assign a score for each item
      void withBest​(java.util.function.Consumer<T> consumer)
      Pass the best item (item with the highest score) to a function
      void withWorst​(java.util.function.Consumer<T> consumer)
      Pass the worst item (item with the lowest score) to a function
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Filter

        public Filter​(java.util.List<T> items)
        Create a Filter from a list of items
        Parameters:
        items - Items
      • Filter

        public Filter​(T[] items)
        Create a Filter from an array of items
        Parameters:
        items - Items
    • Method Detail

      • score

        public void score​(java.util.function.Function<T,​java.lang.Double> fun)
        Assign a score for each item
        Parameters:
        fun - Scoring function
         (T item) -> double
                    
      • score

        public void score​(java.util.function.BiFunction<T,​java.lang.Double,​java.lang.Double> fun)
        Assign a score for each item, based on it's previous score
        Parameters:
        fun - Scoring function
         (T item, double previousScore) -> double
                    
      • remove

        public void remove​(java.util.function.Function<T,​java.lang.Boolean> fun)
        Remove any marked items
        Parameters:
        fun - Function that returns true when an item should be removed
         (T item) -> boolean
                    
      • remove

        public void remove​(java.util.function.BiFunction<T,​java.lang.Double,​java.lang.Boolean> fun)
        Remove any marked items based on their score
        Parameters:
        fun - Function that returns true when an item should be removed
         (T item, double score) -> boolean
                    
      • remove

        public void remove​(T singleItem)
        Remove a single item
        Parameters:
        singleItem - Item
      • keepOnly

        public void keepOnly​(java.util.function.Function<T,​java.lang.Boolean> fun)
        Keep only marked items, remove the rest
        Parameters:
        fun - Function that returns true when an item should be kept
         (T item) -> boolean
                    
      • keepOnly

        public void keepOnly​(java.util.function.BiFunction<T,​java.lang.Double,​java.lang.Boolean> fun)
        Keep any marked items based on their score, remove the rest
        Parameters:
        fun - Function that returns true when an item should be kept
         (T item, double score) -> boolean
                    
      • reset

        public void reset()
        Resets all scores, and re-adds removed items
      • getCount

        public int getCount()
        Get the number of items that have not been removed
        Returns:
        Number of items
      • isEmpty

        public boolean isEmpty()
        Get if the Filter has no items left in it
        Returns:
        Is empty?
      • forEach

        public void forEach​(java.util.function.Consumer<T> consumer)
        Iterate over each item
        Parameters:
        consumer - Callback for every item
         (T item) -> void
                         
      • forEach

        public void forEach​(java.util.function.BiConsumer<T,​java.lang.Double> consumer)
        Iterate over each item, along with it's score
        Parameters:
        consumer - Callback for every item
         (T item, double score) -> void
                         
      • forEachRemoved

        public void forEachRemoved​(java.util.function.Consumer<T> consumer)
        Iterate over each item that has been removed from the Filter
        Parameters:
        consumer - Callback for every item
         (T item) -> void
                         
      • getBest

        @Nullable
        public T getBest()
        Get the best item, or null if none matches that criteria
        Returns:
        The item with the highest score
      • withBest

        public void withBest​(java.util.function.Consumer<T> consumer)
        Pass the best item (item with the highest score) to a function
        Parameters:
        consumer - Function
         (T item) -> void
                         
      • getWorst

        @Nullable
        public T getWorst()
        Get the worst item, or null if none matches that criteria
        Returns:
        The item with the lowest score (not including removed items)
      • withWorst

        public void withWorst​(java.util.function.Consumer<T> consumer)
        Pass the worst item (item with the lowest score) to a function
        Parameters:
        consumer - Function
         (T item) -> void
                         
      • getAboveThreshold

        public com.google.common.collect.ImmutableList<T> getAboveThreshold​(double threshold)
        Get an ImmutableList of items that have a score greater than the threshold
        Parameters:
        threshold - Threshold value
        Returns:
        List of items above threshold
      • forEachAboveThreshold

        public void forEachAboveThreshold​(double threshold,
                                          java.util.function.Consumer<T> consumer)
        Iterate over each item with a score greater than the threshold
        Parameters:
        threshold - Threshold value
        consumer - Function
         (T item) -> void
                          
      • forEachAboveThreshold

        public void forEachAboveThreshold​(double threshold,
                                          java.util.function.BiConsumer<T,​java.lang.Double> consumer)
        Iterate over each item with a score greater than the threshold
        Parameters:
        threshold - Threshold value
        consumer - Function
         (T item, double score) -> void
                          
      • getBelowThreshold

        public com.google.common.collect.ImmutableList<T> getBelowThreshold​(double threshold)
        Get an ImmutableList of items that have a score less than the threshold
        Parameters:
        threshold - Threshold value
        Returns:
        List of items below threshold
      • forEachBelowThreshold

        public void forEachBelowThreshold​(double threshold,
                                          java.util.function.Consumer<T> consumer)
        Iterate over each item with a score less than the threshold
        Parameters:
        threshold - Threshold value
        consumer - Function
         (T item) -> void
                          
      • forEachBelowThreshold

        public void forEachBelowThreshold​(double threshold,
                                          java.util.function.BiConsumer<T,​java.lang.Double> consumer)
        Iterate over each item with a score less than the threshold
        Parameters:
        threshold - Threshold value
        consumer - Function
         (T item, double score) -> void
                          
      • getOrdered

        public com.google.common.collect.ImmutableList<T> getOrdered()
        Get an ordered ImmutableList of all items, sorted by score
        Returns:
        Ordered list
      • getRemaining

        public com.google.common.collect.ImmutableList<T> getRemaining()
        Get an ImmutableList of all remaining items
        Returns:
        Remaining items
      • getRemoved

        public com.google.common.collect.ImmutableList<T> getRemoved()
        Get an ImmutableList of all removed items
        Returns:
        Removed items