Class RankedSequence<T>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<T>

    final class RankedSequence<T>
    extends java.util.concurrent.atomic.AtomicReference<RankedSequence.Content>
    implements java.lang.Iterable<T>
    Ordered List that arranges elements by descending rank; supports concurrent iteration and modification.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  RankedSequence.Content
      Represents an immutable snapshot of ranked elements.
      (package private) class  RankedSequence.Itr
      Custom Iterator that copes with modification by repositioning itself in the updated list.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long serialVersionUID  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean contains​(java.lang.Object element)  
      boolean containsThis​(java.lang.Object element)  
      void insert​(T element, int rank)
      Inserts the given element into the ordered list, using the assigned rank as a guide.
      boolean isEmpty()  
      RankedSequence.Itr iterator()  
      T peek()  
      (package private) static long rank2uid​(int rank, int uniq)
      Turns the given (potentially non-unique) rank into a unique id by appending a counter.
      T remove​(java.lang.Object element)  
      boolean removeThis​(T element)  
      (package private) static int safeBinarySearch​(long[] uids, long uid)
      Finds the insertion point with the nearest UID, regardless of whether the UID is in the list or not.
      int size()  
      java.lang.Iterable<T> snapshot()  
      (package private) static int uid2rank​(long uid)
      Extracts the original (potentially non-unique) assigned rank from the given unique id.
      • Methods inherited from class java.util.concurrent.atomic.AtomicReference

        accumulateAndGet, compareAndExchange, compareAndExchangeAcquire, compareAndExchangeRelease, compareAndSet, get, getAcquire, getAndAccumulate, getAndSet, getAndUpdate, getOpaque, getPlain, lazySet, set, setOpaque, setPlain, setRelease, toString, updateAndGet, weakCompareAndSet, weakCompareAndSetAcquire, weakCompareAndSetPlain, weakCompareAndSetRelease, weakCompareAndSetVolatile
      • Methods inherited from class java.lang.Object

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

        forEach, spliterator
    • Constructor Detail

      • RankedSequence

        RankedSequence()
    • Method Detail

      • insert

        public void insert​(T element,
                           int rank)
        Inserts the given element into the ordered list, using the assigned rank as a guide.

        The rank can be any value from Integer.MIN_VALUE to Integer.MAX_VALUE.

        Parameters:
        element - The element to insert
        rank - The assigned rank
      • peek

        public T peek()
      • contains

        public boolean contains​(java.lang.Object element)
      • containsThis

        public boolean containsThis​(java.lang.Object element)
      • remove

        public T remove​(java.lang.Object element)
      • removeThis

        public boolean removeThis​(T element)
      • snapshot

        public java.lang.Iterable<T> snapshot()
      • clear

        public void clear()
      • isEmpty

        public boolean isEmpty()
      • size

        public int size()
      • iterator

        public RankedSequence.Itr iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • rank2uid

        static long rank2uid​(int rank,
                             int uniq)
        Turns the given (potentially non-unique) rank into a unique id by appending a counter.
        Parameters:
        rank - The assigned rank
        uniq - The unique counter
        Returns:
        The unique id
      • uid2rank

        static int uid2rank​(long uid)
        Extracts the original (potentially non-unique) assigned rank from the given unique id.
        Parameters:
        uid - The unique id
        Returns:
        Assigned rank
      • safeBinarySearch

        static int safeBinarySearch​(long[] uids,
                                    long uid)
        Finds the insertion point with the nearest UID, regardless of whether the UID is in the list or not.

        Unlike Arrays.binarySearch(long[], long) this will always return a number from zero to size() inclusive.

        Parameters:
        uids - The UIDs array
        uid - The UID to find
        Returns:
        Index with nearest UID