Class IdentityOrderedSet<T>

All Implemented Interfaces:
Arrangeable, EnhancedCollection<T>, Ordered<T>, Iterable<T>, Collection<T>, Set<T>

public class IdentityOrderedSet<T> extends ObjectOrderedSet<T>
A variant on ObjectOrderedSet that compares items by identity (using ==) instead of equality (using equals()). It also hashes with System.identityHashCode(Object) instead of calling the hashCode() of an item. This can be useful in some cases where items may have invalid Object.equals(Object) and/or Object.hashCode() implementations, or if items could be very large (making a hashCode() that uses all the parts of the item slow). Oddly, System.identityHashCode(Object) tends to be slower than the hashCode() for most small items, because an explicitly-written hashCode() typically doesn't need to do anything concurrently, but identityHashCode() needs to (concurrently) modify an internal JVM variable that ensures the results are unique, and that requires the JVM to do lots of extra work whenever identityHashCode() is called. Despite that, identityHashCode() doesn't depend on the quantity of variables in the item, so identityHashCode() gets relatively faster for larger items. The equals() method used by ObjectOrderedSet also tends to slow down for large items, relative to the constant-time == this uses.
This can potentially be useful for tracking references to ensure they are all unique by identity, such as to avoid referential cycles. You might prefer this to IdentitySet if you need to ensure fast iteration or want access by index.
  • Constructor Details

    • IdentityOrderedSet

      public IdentityOrderedSet(OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet(int initialCapacity, OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet(int initialCapacity, float loadFactor, OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet(Iterator<? extends T> coll, OrderType type)
      Creates a new instance containing the items in the specified iterator.
      Parameters:
      coll - an iterator that will have its remaining contents added to this
    • IdentityOrderedSet

      public IdentityOrderedSet(ObjectOrderedSet<? extends T> set, OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet(Collection<? extends T> coll, OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet(T[] array, int offset, int length, OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet(T[] array, OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet(Ordered<T> other, int offset, int count, OrderType type)
    • IdentityOrderedSet

      public IdentityOrderedSet()
    • IdentityOrderedSet

      public IdentityOrderedSet(int initialCapacity)
    • IdentityOrderedSet

      public IdentityOrderedSet(int initialCapacity, float loadFactor)
    • IdentityOrderedSet

      public IdentityOrderedSet(Iterator<? extends T> coll)
      Creates a new instance containing the items in the specified iterator.
      Parameters:
      coll - an iterator that will have its remaining contents added to this
    • IdentityOrderedSet

      public IdentityOrderedSet(ObjectOrderedSet<? extends T> set)
    • IdentityOrderedSet

      public IdentityOrderedSet(Collection<? extends T> coll)
    • IdentityOrderedSet

      public IdentityOrderedSet(T[] array, int offset, int length)
    • IdentityOrderedSet

      public IdentityOrderedSet(T[] array)
    • IdentityOrderedSet

      public IdentityOrderedSet(Ordered<T> other, int offset, int count)
  • Method Details

    • 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<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

      protected boolean equate(Object left, Object right)
      Description copied from class: ObjectSet
      Compares the objects left and right, which are usually keys, for equality, returning true if they are considered equal. This is used by the rest of this class to determine whether two keys are considered equal. Normally, this returns left.equals(right), but subclasses can override it to use reference equality, fuzzy equality, deep array equality, or any other custom definition of equality. Usually, ObjectSet.place(Object) is also overridden if this method is.
      Overrides:
      equate in class ObjectSet<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 considered equal for the purposes of this class
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<T>
      Specified by:
      hashCode in interface Set<T>
      Overrides:
      hashCode in class ObjectOrderedSet<T>
    • getHashMultiplier

      public int getHashMultiplier()
      Effectively does nothing here because the hashMultiplier is not used by identity hashing.
      Overrides:
      getHashMultiplier in class ObjectSet<T>
      Returns:
      any int; the value isn't used internally, but may be used by subclasses to identify something
    • setHashMultiplier

      public void setHashMultiplier(int hashMultiplier)
      Effectively does nothing here because the hashMultiplier is not used by identity hashing. Subclasses can use this to set some kind of identifier or user data, though. Unlike the superclass implementation, this does not alter the given int to make it negative or odd.
      Overrides:
      setHashMultiplier in class ObjectSet<T>
      Parameters:
      hashMultiplier - any int; will not be used
    • with

      public static <T> IdentityOrderedSet<T> with()
      Constructs an empty set given the type as a generic type argument. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Type Parameters:
      T - the type of items; must be given explicitly
      Returns:
      a new set containing nothing
    • with

      public static <T> IdentityOrderedSet<T> with(T item)
      Creates a new IdentityOrderedSet that holds only the given item, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item - one T item
      Returns:
      a new IdentityOrderedSet that holds the given item
    • with

      public static <T> IdentityOrderedSet<T> with(T item0, T item1)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item0 - a T item
      item1 - a T item
      Returns:
      a new IdentityOrderedSet that holds the given items
    • with

      public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item0 - a T item
      item1 - a T item
      item2 - a T item
      Returns:
      a new IdentityOrderedSet that holds the given items
    • with

      public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2, T item3)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item0 - a T item
      item1 - a T item
      item2 - a T item
      item3 - a T item
      Returns:
      a new IdentityOrderedSet that holds the given items
    • with

      public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2, T item3, T item4)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item0 - a T item
      item1 - a T item
      item2 - a T item
      item3 - a T item
      item4 - a T item
      Returns:
      a new IdentityOrderedSet that holds the given items
    • with

      public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2, T item3, T item4, T item5)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item0 - a T item
      item1 - a T item
      item2 - a T item
      item3 - a T item
      item4 - a T item
      item5 - a T item
      Returns:
      a new IdentityOrderedSet that holds the given items
    • with

      public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item0 - a T item
      item1 - a T item
      item2 - a T item
      item3 - a T item
      item4 - a T item
      item5 - a T item
      item6 - a T item
      Returns:
      a new IdentityOrderedSet that holds the given items
    • with

      public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      item0 - a T item
      item1 - a T item
      item2 - a T item
      item3 - a T item
      item4 - a T item
      item5 - a T item
      item6 - a T item
      Returns:
      a new IdentityOrderedSet that holds the given items
    • with

      @SafeVarargs public static <T> IdentityOrderedSet<T> with(T... varargs)
      Creates a new IdentityOrderedSet that holds only the given items, but can be resized. This overload will only be used when an array is supplied and the type of the items requested is the component type of the array, or if varargs are used and there are 9 or more arguments.
      Type Parameters:
      T - the type of item, typically inferred
      Parameters:
      varargs - a T varargs or T array; remember that varargs allocate
      Returns:
      a new IdentityOrderedSet that holds the given items
    • parse

      public static <T> IdentityOrderedSet<T> parse(String str, String delimiter, PartialParser<T> parser)
      Calls parse(String, String, PartialParser, boolean) with brackets set to false.
      Parameters:
      str - a String that will be parsed in full
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns a T item from a section of str
      Returns:
      a new collection parsed from str
    • parse

      public static <T> IdentityOrderedSet<T> parse(String str, String delimiter, PartialParser<T> parser, boolean brackets)
      Creates a new collection 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.
      Parameters:
      str - a String that will be parsed in full (depending on brackets)
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns a T 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> IdentityOrderedSet<T> parse(String str, String delimiter, PartialParser<T> parser, int offset, int length)
      Creates a new collection and fills it by calling EnhancedCollection.addLegible(String, String, PartialParser, int, int) with the given five parameters as-is.
      Parameters:
      str - a String that will have the given section parsed
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns a T 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