Class Limiter<T>

  • Type Parameters:
    T - type of input elements
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>

    class Limiter<T>
    extends java.util.AbstractCollection<T>
    Extracts least limit elements from the input sorting them according to the given comparator. Works for 2 <= limit < Integer.MAX_VALUE/2. Uses O(min(limit, inputSize)) additional memory.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Comparator<? super T> comparator  
      private T[] data  
      private boolean initial  
      private int limit  
      private int size  
    • Constructor Summary

      Constructors 
      Constructor Description
      Limiter​(int limit, java.util.Comparator<? super T> comparator)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Iterator<T> iterator()  
      boolean put​(T t)
      Accumulate new element
      Limiter<T> putAll​(Limiter<T> ls)
      Merge other Limiter object into this (other object becomes unusable after that).
      int size()  
      void sort()
      Must be called after accumulation is finished.
      private void sortTail()  
      java.lang.Object[] toArray()  
      • Methods inherited from class java.util.AbstractCollection

        add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • data

        private T[] data
      • limit

        private final int limit
      • comparator

        private final java.util.Comparator<? super T> comparator
      • size

        private int size
      • initial

        private boolean initial
    • Constructor Detail

      • Limiter

        public Limiter​(int limit,
                       java.util.Comparator<? super T> comparator)
    • Method Detail

      • put

        public boolean put​(T t)
        Accumulate new element
        Parameters:
        t - element to accumulate
        Returns:
        false if the element is definitely not included into result, so any bigger element could be skipped as well, or true if element will probably be included into result.
      • putAll

        public Limiter<T> putAll​(Limiter<T> ls)
        Merge other Limiter object into this (other object becomes unusable after that).
        Parameters:
        ls - other object to merge
        Returns:
        this object
      • sortTail

        private void sortTail()
      • sort

        public void sort()
        Must be called after accumulation is finished. After calling sort() this Limiter represents the resulting collection.
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
        Overrides:
        toArray in class java.util.AbstractCollection<T>
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in class java.util.AbstractCollection<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in class java.util.AbstractCollection<T>