Class SortedCopyOnWriteSet<E,C extends Comparable<C>>

java.lang.Object
com.comphenix.protocol.concurrent.SortedCopyOnWriteSet<E,C>
Type Parameters:
E - the type of elements maintained by this set
C - the type of the comparable used for ordering the elements
All Implemented Interfaces:
Iterable<E>

public class SortedCopyOnWriteSet<E,C extends Comparable<C>> extends Object implements Iterable<E>
A collection that stores elements in a sorted order based on a provided Comparable, while ensuring element equality is checked using their equals method.

This class uses a copy-on-write strategy for updates, ensuring that iteration over the collection is safe for concurrent use, even though the collection itself is not thread-safe for modifications.

Elements are inserted into the set in a position determined by their natural ordering. If multiple elements have comparables that are considered equal (i.e., compareTo returns zero), they will maintain their insertion order. If an element is already present in the set (as determined by equals), it will not be added again.

  • Constructor Details

    • SortedCopyOnWriteSet

      public SortedCopyOnWriteSet()
  • Method Details

    • add

      public boolean add(E element, C comparable)
      Adds the specified element to this set in a sorted order based on the provided Comparable. The element will be inserted before the first position that is strictly greater than the element. This ensures that elements maintain their insertion order when their comparables are considered equal (i.e., when compareTo returns zero).

      If the set already contains the element (as determined by equals), the element is not added again.

      Parameters:
      element - the element to be added
      comparable - the comparable used to determine the element's position in the sorted order
      Returns:
      true if the element was added to the set; false if the set already contained the element
      Throws:
      NullPointerException - if the specified element is null
    • remove

      public boolean remove(E element)
      Removes the specified element from this set if it is present.
      Parameters:
      element - the element to be removed
      Returns:
      true if the set contained the specified element; false otherwise
      Throws:
      NullPointerException - if the specified element is null
    • isEmpty

      public boolean isEmpty()
      Returns true if this set contains no elements.
      Returns:
      true if this set contains no elements
    • iterator

      public Iterator<E> iterator()
      Returns an iterator over the elements in this set. The elements are returned in natural order.
      Specified by:
      iterator in interface Iterable<E>
      Returns:
      an iterator over the elements in this set