ch.javasoft.util.intcoll
Class DefaultIntList

java.lang.Object
  extended by ch.javasoft.util.intcoll.AbstractIntCollection
      extended by ch.javasoft.util.intcoll.AbstractIntList
          extended by ch.javasoft.util.intcoll.DefaultIntList
All Implemented Interfaces:
IntCollection, IntIterable, IntList, Serializable, Cloneable, Iterable<Integer>, Collection<Integer>, List<Integer>

public class DefaultIntList
extends AbstractIntList
implements IntList, Serializable, Cloneable

The DefaultIntList is a list of integers backed by an IntArray. Integers can be added after construction. This class implements the collection interfaces and is therefore a list of integers.

See Also:
addInt(int), trimToLength(), List, IntList, Serialized Form

Field Summary
 
Fields inherited from class ch.javasoft.util.intcoll.AbstractIntCollection
mod
 
Constructor Summary
DefaultIntList()
           
DefaultIntList(DefaultIntList copy)
           
DefaultIntList(int initialCapacity)
           
DefaultIntList(int[] initialValues)
           
DefaultIntList(IntArray array)
           
DefaultIntList(IntCollection coll)
           
DefaultIntList(Iterable<Integer> values)
           
 
Method Summary
 boolean addAll(int... values)
           
 boolean addAll(IntCollection coll)
           
 boolean addInt(int value)
           
 boolean addInt(int index, int value)
           
 int addIntToSorted(int value)
          Adds the given value at the position where the value fits according to binarySearchInt(int).
 int binarySearchInt(int key)
          Searches this int array for the specified value using the binary search algorithm.
 int binarySearchInt(int key, int fromIndex, int toIndex)
          Searches this int array for the specified value using the binary search algorithm.
 void clear()
           
 DefaultIntList clone()
           
 int firstInt()
           
 int getInt(int index)
           
 boolean isEmpty()
           
 int lastInt()
           
 IntListIterator listIterator(int index)
           
 void mergeSorted(IntList sorted)
          Merges two sorted int lists.
 boolean removeFromTail(int n)
          Removes n elements from the tail of this array.
 int removeIntAt(int index)
          Removes and returns the integer at the given position.
 int removeLastInt()
           
 int setInt(int index, int value)
           
 void setOrAddInt(int index, int value)
          Sets the value at the given position, if the index is in the range or this int array.
 int size()
           
 void sort(boolean ascending)
          Sort the array ascending or descending.
 void sort(boolean ascending, int start, int end)
          Sort the specified array range ascending or descending.
 DefaultIntList subList(int fromIndex, int toIndex)
           
 void swap(int indexA, int indexB)
           
 int[] toIntArray()
           
 String toString()
          Returns a string representation of the contents of this int array.
 void trimToLength()
           
 int[] yieldIntArray()
          Returns the internal array after trimming it to the current length and returns it.
 
Methods inherited from class ch.javasoft.util.intcoll.AbstractIntList
add, addAll, containsInt, get, indexOf, indexOfInt, intIterator, lastIndexOf, lastIndexOfInt, listIterator, remove, removeInt, set
 
Methods inherited from class ch.javasoft.util.intcoll.AbstractIntCollection
add, addAll, contains, containsAll, equals, first, hashCode, iterator, last, remove, removeAll, retainAll, toArray, toArray, toArrayInternal, toIntArray, toIntArrayInternal
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ch.javasoft.util.intcoll.IntList
containsInt, intIterator
 
Methods inherited from interface ch.javasoft.util.intcoll.IntCollection
removeInt, toIntArray
 
Methods inherited from interface java.util.List
add, add, addAll, addAll, contains, containsAll, equals, get, hashCode, indexOf, iterator, lastIndexOf, listIterator, remove, remove, removeAll, retainAll, set, toArray, toArray
 

Constructor Detail

DefaultIntList

public DefaultIntList()

DefaultIntList

public DefaultIntList(int initialCapacity)

DefaultIntList

public DefaultIntList(int[] initialValues)

DefaultIntList

public DefaultIntList(IntArray array)

DefaultIntList

public DefaultIntList(Iterable<Integer> values)

DefaultIntList

public DefaultIntList(DefaultIntList copy)

DefaultIntList

public DefaultIntList(IntCollection coll)
Method Detail

clone

public DefaultIntList clone()
Overrides:
clone in class AbstractIntCollection

size

public int size()
Specified by:
size in interface Collection<Integer>
Specified by:
size in interface List<Integer>
Specified by:
size in class AbstractIntList

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<Integer>
Specified by:
isEmpty in interface List<Integer>
Overrides:
isEmpty in class AbstractIntCollection

getInt

public int getInt(int index)
           throws IndexOutOfBoundsException
Specified by:
getInt in interface IntList
Specified by:
getInt in class AbstractIntList
Throws:
IndexOutOfBoundsException

setInt

public int setInt(int index,
                  int value)
           throws IndexOutOfBoundsException
Specified by:
setInt in interface IntList
Specified by:
setInt in class AbstractIntList
Throws:
IndexOutOfBoundsException

setOrAddInt

public void setOrAddInt(int index,
                        int value)
Sets the value at the given position, if the index is in the range or this int array. If the index is at the end (i.e. index == length()), the value is added to this array. If the index is after the end (i.e. index > length()), the value is added at the specified position, the gap between the added value and the current length of the array is filled with initial values.

Parameters:
index - the index where the new value should be placed, either added or replacing an old one
value - the value to set or add

addIntToSorted

public int addIntToSorted(int value)
Adds the given value at the position where the value fits according to binarySearchInt(int). If this int array is not sorted ascending, the result is undefined.

Parameters:
value - the value to insert
Returns:
the position where the value has been inserted

mergeSorted

public void mergeSorted(IntList sorted)
Merges two sorted int lists. If any of the lists is not sorted ascending, the result is undefined.

Parameters:
sorted - the other sorted int list to merge into this int array

addInt

public boolean addInt(int value)
Specified by:
addInt in interface IntCollection
Overrides:
addInt in class AbstractIntList

addInt

public boolean addInt(int index,
                      int value)
Specified by:
addInt in interface IntList
Specified by:
addInt in class AbstractIntList

addAll

public boolean addAll(IntCollection coll)
Specified by:
addAll in interface IntCollection
Overrides:
addAll in class AbstractIntCollection

addAll

public boolean addAll(int... values)
Specified by:
addAll in interface IntCollection
Overrides:
addAll in class AbstractIntCollection

removeLastInt

public int removeLastInt()

removeFromTail

public boolean removeFromTail(int n)
Removes n elements from the tail of this array. If fewer elements are left in this array, an IndexOutOfBoundsException is thrown.

Parameters:
n - the number of elements to remove from the tail of this array
Returns:
true if this array changed as a consequence of this operation

removeIntAt

public int removeIntAt(int index)
Removes and returns the integer at the given position. Values at the right of the concerned position are shifted to left.

Specified by:
removeIntAt in interface IntList
Specified by:
removeIntAt in class AbstractIntList
Parameters:
index - the index of the value to remove
Returns:
the removed value

clear

public void clear()
Specified by:
clear in interface Collection<Integer>
Specified by:
clear in interface List<Integer>
Specified by:
clear in class AbstractIntList

firstInt

public int firstInt()
Overrides:
firstInt in class AbstractIntCollection

lastInt

public int lastInt()
Overrides:
lastInt in class AbstractIntCollection

subList

public DefaultIntList subList(int fromIndex,
                              int toIndex)
Specified by:
subList in interface IntList
Specified by:
subList in interface List<Integer>
Specified by:
subList in class AbstractIntList

listIterator

public IntListIterator listIterator(int index)
Specified by:
listIterator in interface IntList
Specified by:
listIterator in interface List<Integer>
Specified by:
listIterator in class AbstractIntList

swap

public void swap(int indexA,
                 int indexB)

toIntArray

public int[] toIntArray()
Specified by:
toIntArray in interface IntCollection
Overrides:
toIntArray in class AbstractIntCollection

yieldIntArray

public int[] yieldIntArray()
Returns the internal array after trimming it to the current length and returns it. This DefaultIntList will be empty after this operation.


trimToLength

public void trimToLength()

sort

public void sort(boolean ascending)
Sort the array ascending or descending. If this array is sorted ascending, the binarySearchInt(int) and addIntToSorted(int) methods can be used

Parameters:
ascending - true for ascending sort order

sort

public void sort(boolean ascending,
                 int start,
                 int end)
Sort the specified array range ascending or descending. If this whole array is sorted ascending, the binarySearchInt(int) and addIntToSorted(int) methods can be used.

This method should only be used if one knows that only the defined interval needs sorting. Otherwise, sort(boolean) should be used instead.

Parameters:
ascending - true for ascending sort order
start - the index where the sorting starts (inclusive)
end - the index where the sorting ends (exclusive)

binarySearchInt

public int binarySearchInt(int key)
                    throws IllegalStateException
Searches this int array for the specified value using the binary search algorithm. The array must be sorted ascending (as by the sort(boolean) method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
key - the value to be searched for.
Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
Throws:
IllegalStateException
See Also:
Arrays.binarySearch(int[], int, int, int)

binarySearchInt

public int binarySearchInt(int key,
                           int fromIndex,
                           int toIndex)
                    throws IllegalStateException
Searches this int array for the specified value using the binary search algorithm. The array must be sorted ascending (as by the sort(boolean) method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
key - the value to be searched for.
fromIndex - the index of the first element (inclusive) to be searched
toIndex - the index of the last element (exclusive) to be searched
Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
Throws:
IllegalStateException
See Also:
Arrays.binarySearch(int[], int, int, int)

toString

public String toString()
Returns a string representation of the contents of this int array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(int).

Overrides:
toString in class AbstractIntCollection
Returns:
a string representation of a