E
- object type this bag holdspublic class Bag<E> extends Object implements ImmutableBag<E>
Modifier and Type | Field and Description |
---|---|
protected int |
size
The amount of elements contained in bag.
|
Constructor and Description |
---|
Bag()
Constructs an empty Bag with an initial capacity of 64.
|
Bag(Class<E> type)
Constructs an empty Bag with an initial capacity of 64.
|
Bag(Class<E> type,
int capacity) |
Bag(int capacity)
Constructs an empty Bag with the specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E e)
Adds the specified element to the end of this bag.
|
void |
addAll(ImmutableBag<E> items)
Add all items into this bag.
|
void |
clear()
Removes all of the elements from this bag.
|
boolean |
contains(E e)
Check if bag contains this element.
|
void |
ensureCapacity(int index)
Check if an item, if added at the given item will fit into the bag.
|
boolean |
equals(Object o) |
E |
get(int index)
Returns the element at the specified position in Bag.
|
int |
getCapacity()
Returns the number of elements the bag can hold without growing.
|
E[] |
getData()
Returns this bag's underlying array.
|
int |
hashCode() |
boolean |
isEmpty()
Returns true if this bag contains no elements.
|
boolean |
isIndexWithinBounds(int index)
Checks if the internal storage supports this index.
|
Iterator<E> |
iterator() |
boolean |
remove(E e)
Removes the first occurrence of the specified element from this Bag, if
it is present.
|
E |
remove(int index)
Removes the element at the specified position in this Bag.
|
boolean |
removeAll(ImmutableBag<E> bag)
Removes from this Bag all of its elements that are contained in the
specified Bag.
|
E |
removeLast()
Remove and return the last object in the bag.
|
E |
safeGet(int index)
Returns the element at the specified position in Bag.
|
void |
set(int index,
E e)
Set element at specified index in the bag.
|
int |
size()
Returns the number of elements in this bag.
|
void |
sort(Comparator<E> comparator)
Sorts the bag using the
comparator . |
String |
toString() |
void |
unsafeSet(int index,
E e)
Unsafe method. Sets element at specified index in the bag,
without updating size.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public Bag()
public Bag(int capacity)
capacity
- the initial capacity of Bagpublic E remove(int index) throws ArrayIndexOutOfBoundsException
It does this by overwriting it was last element then removing last element
index
- the index of element to be removedArrayIndexOutOfBoundsException
public void sort(Comparator<E> comparator)
comparator
.comparator
- public E removeLast()
public boolean remove(E e)
If the Bag does not contain the element, it is unchanged. It does this by overwriting it was last element then removing last element
e
- element to be removed from this list, if presenttrue
if this list contained the specified elementpublic boolean contains(E e)
contains
in interface ImmutableBag<E>
e
- element to checktrue
if the bag contains this elementpublic boolean removeAll(ImmutableBag<E> bag)
bag
- Bag containing elements to be removed from this Bagtrue
if this Bag changed as a result of the callpublic E get(int index) throws ArrayIndexOutOfBoundsException
get
in interface ImmutableBag<E>
index
- index of the element to returnArrayIndexOutOfBoundsException
public E safeGet(int index)
index
- index of the element to returnpublic int size()
size
in interface ImmutableBag<E>
public int getCapacity()
public boolean isIndexWithinBounds(int index)
index
- index to checktrue
if the index is within boundspublic boolean isEmpty()
isEmpty
in interface ImmutableBag<E>
true
if this bag contains no elementspublic void add(E e)
If required, it also increases the capacity of the bag.
e
- element to be added to this listpublic void unsafeSet(int index, E e)
index
- position of elemente
- the elementpublic void set(int index, E e)
index
- position of elemente
- the elementpublic void ensureCapacity(int index)
If not, the bag capacity will be increased to hold an item at the index.
yeah, sorry, it's weird, but we don't want to change existing change behavior
index
- index to checkpublic void clear()
The bag will be empty after this call returns.
public void addAll(ImmutableBag<E> items)
items
- bag with items to addpublic E[] getData()
Use of this method requires typed instantiation, e.g. Bag
size()
Copyright © 2019. All rights reserved.