Class ObjectBag<T>

All Implemented Interfaces:
Arrangeable, Arrangeable.ArrangeableList<T>, EnhancedCollection<T>, Ordered<T>, Serializable, Cloneable, Iterable<T>, Collection<T>, List<T>, RandomAccess

public class ObjectBag<T> extends ObjectList<T>
An unordered List of T items. This allows efficient iteration via a reused iterator or via index. This class avoids a memory copy when removing elements (the last element is moved to the removed element's position). Items are permitted to change position in the ordering when any item is removed or added. Although this won't keep an order during modifications, you can ObjectList.sort() the bag to ensure, if no modifications are made later, that the iteration will happen in sorted order.
See Also:
  • Constructor Details

    • ObjectBag

      public ObjectBag()
      Constructs an empty bag with an initial capacity of 10.
    • ObjectBag

      public ObjectBag(int initialCapacity)
      Constructs an empty bag with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity of the bag
      Throws:
      IllegalArgumentException - if the specified initial capacity is negative
    • ObjectBag

      public ObjectBag(Collection<? extends T> c)
      Constructs a bag containing the elements of the specified collection, in the order they are returned by the collection's iterator.
      Parameters:
      c - the collection whose elements are to be placed into this bag
      Throws:
      NullPointerException - if the specified collection is null
    • ObjectBag

      public ObjectBag(T[] a)
    • ObjectBag

      public ObjectBag(T[] a, int offset, int count)
    • ObjectBag

      public ObjectBag(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
    • ObjectBag

      public ObjectBag(Ordered<T> other, int offset, int count)
      Creates a new bag by copying count items from the given Ordered, starting at offset in that Ordered, into this.
      Parameters:
      other - another Ordered of the same type
      offset - the first index in other's ordering to draw an item from
      count - how many items to copy from other
  • Method Details

    • keepsOrder

      public boolean keepsOrder()
      Returns true if this implementation retains order, which it does not.
      Overrides:
      keepsOrder in class ObjectList<T>
      Returns:
      false
    • add

      public void add(int index, T element)
      This always adds element to the end of this bag's ordering; index is ignored.
      Specified by:
      add in interface List<T>
      Overrides:
      add in class ObjectList<T>
      Parameters:
      index - ignored
      element - element to be inserted
    • insert

      public void insert(int index, T element)
      This always adds element to the end of this bag's ordering; index is ignored. This is an alias for add(int, Object) to improve compatibility with primitive lists.
      Overrides:
      insert in class ObjectList<T>
      Parameters:
      index - ignored
      element - element to be inserted
    • remove

      public T remove(int index)
      This removes the item at the given index and returns it, but also changes the ordering.
      Specified by:
      remove in interface List<T>
      Overrides:
      remove in class ObjectList<T>
      Parameters:
      index - the index of the element to be removed, which must be non-negative and less than ArrayList.size()
      Returns:
      the removed item
      Throws:
      IndexOutOfBoundsException - if the bag is empty
    • removeAt

      public T removeAt(int index)
      This removes the item at the given index and returns it, but also changes the ordering. This is an alias for remove(int) to make the API the same for primitive lists.
      Overrides:
      removeAt in class ObjectList<T>
      Parameters:
      index - must be non-negative and less than ArrayList.size()
      Returns:
      the previously-held item at the given index
      Throws:
      IndexOutOfBoundsException - if the bag is empty
    • equalsIdentity

      public boolean equalsIdentity(Object object)
      Uses == for comparison of the bags; does not compare their items.
      Overrides:
      equalsIdentity in class ObjectList<T>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<T>
      Specified by:
      hashCode in interface List<T>
      Overrides:
      hashCode in class ObjectList<T>
    • with

      public static <T> ObjectBag<T> with()
      Constructs an empty bag 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 bag containing nothing
    • with

      public static <T> ObjectBag<T> with(T item)
      Creates a new ObjectBag 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 ObjectBag that holds the given item
    • with

      public static <T> ObjectBag<T> with(T item0, T item1)
      Creates a new ObjectBag 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 ObjectBag that holds the given items
    • with

      public static <T> ObjectBag<T> with(T item0, T item1, T item2)
      Creates a new ObjectBag 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 ObjectBag that holds the given items
    • with

      public static <T> ObjectBag<T> with(T item0, T item1, T item2, T item3)
      Creates a new ObjectBag 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 ObjectBag that holds the given items
    • with

      public static <T> ObjectBag<T> with(T item0, T item1, T item2, T item3, T item4)
      Creates a new ObjectBag 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 ObjectBag that holds the given items
    • with

      public static <T> ObjectBag<T> with(T item0, T item1, T item2, T item3, T item4, T item5)
      Creates a new ObjectBag 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 ObjectBag that holds the given items
    • with

      public static <T> ObjectBag<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6)
      Creates a new ObjectBag 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 ObjectBag that holds the given items
    • with

      public static <T> ObjectBag<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7)
      Creates a new ObjectBag 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 ObjectBag that holds the given items
    • with

      @SafeVarargs public static <T> ObjectBag<T> with(T... varargs)
      Creates a new ObjectBag that will hold the items in the given array or varargs. 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 items, typically inferred by all the items being the same type
      Parameters:
      varargs - either 0 or more T items, or an array of T
      Returns:
      a new ObjectBag that holds the given T items
    • parse

      public static <T> ObjectBag<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> ObjectBag<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> ObjectBag<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