|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.samskivert.util.ListUtil
public class ListUtil
List util is for the times when you just can't bear the overhead of an
ArrayList object to manage your list of objects. It
provides a suite of list management routines that operate on bare
Object arrays. Some of those routines mimic the
behavior of array lists, others provide other more specialized
(generally faster but making requirements of the caller) list behavior.
An example is probably in order:
Object[] list = null; String foo = "foo"; String bar = "bar"; // add our objects to a list list = ListUtil.add(list, foo); list = ListUtil.add(list, bar); // remove foo from the list (does so by clearing out that index, but it // doesn't slide subsequent elements down) ListUtil.clear(list, foo); // use the version of remove that calls equals() rather than just // checking for equality of references String anotherBar = "bar"; ListUtil.clear(list, anotherBar); // append our objects to the end of the list letting list util know // that we're tracking the list size list = ListUtil.add(list, 0, foo); list = ListUtil.add(list, 1, bar); // remove the elements from the list, compacting it to preserve // element continuity ListUtil.remove(list, 0); ListUtil.remove(list, bar);See the documentation for the individual functions for their exact behavior.
| Nested Class Summary | |
|---|---|
protected static interface |
ListUtil.EqualityComparator
Used to allow the same code to optionally use reference equality and Object.equals(java.lang.Object).equality. |
| Field Summary | |
|---|---|
protected static int |
DEFAULT_LIST_SIZE
The size of a list to create if we have to create one entirely from scratch rather than just expand it. |
protected static ListUtil.EqualityComparator |
EQUALS_COMP
|
protected static ListUtil.EqualityComparator |
REFERENCE_COMP
|
| Constructor Summary | |
|---|---|
ListUtil()
|
|
| Method Summary | |
|---|---|
protected static Object[] |
accomodate(Object[] list,
int index)
Creates a new list that will accomodate the specified index and copies the contents of the old list to the first. |
static Object[] |
add(Object[] list,
int startIdx,
Object element)
Adds the specified element to the next empty slot in the specified list. |
static Object[] |
add(Object[] list,
Object element)
Adds the specified element to the first empty slot in the specified list. |
protected static Object |
clear(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
Helper function for clearRef(java.lang.Object[], java.lang.Object), etc. |
static Object |
clear(Object[] list,
Object element)
Clears out the first element that is functionally equal to the supplied element ( list[idx].equals(element)). |
static Object |
clearRef(Object[] list,
Object element)
Clears out the first element that is referentially equal to the supplied element ( list[idx] == element). |
protected static boolean |
contains(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
Helper function for containsRef(java.lang.Object[], java.lang.Object), etc. |
static boolean |
contains(Object[] list,
Object element)
Looks for an element that is functionally equal to the supplied element ( list[idx].equals(element)). |
static boolean |
containsRef(Object[] list,
Object element)
Looks for an object that is referentially equal to the supplied element ( list[idx] == element). |
protected static int |
indexOf(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
Helper function for indexOfRef(java.lang.Object[], java.lang.Object), etc. |
static int |
indexOf(Object[] list,
Object element)
Looks for an element that is functionally equal to the supplied element ( list[idx].equals(element)). |
static int |
indexOfNull(Object[] list)
Returns the lowest index in the array that contains null, or -1 if there is no room to add elements without expanding the array. |
static int |
indexOfRef(Object[] list,
Object element)
Looks for an object that is referentially equal to the supplied element ( list[idx] == element) and returns its index
in the array. |
static Object[] |
insert(Object[] list,
int index,
Object element)
Inserts the supplied element at the specified position in the array, shifting the remaining elements down. |
static void |
main(String[] args)
Run some tests. |
static int |
nextPowerOfTwo(int value)
Rounds the specified value up to the next nearest power of two. |
protected static NullPointerException |
NPE()
Throw a NullPointerException with the bad news. |
protected static Object |
remove(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
Helper function for removeRef(java.lang.Object[], java.lang.Object), etc. |
static Object |
remove(Object[] list,
int index)
Removes the element at the specified index. |
static Object |
remove(Object[] list,
Object element)
Removes the first element that is functionally equal to the supplied element ( list[idx].equals(element)). |
static Object |
removeRef(Object[] list,
Object element)
Removes the first element that is referentially equal to the supplied element ( list[idx] == element). |
static int |
size(Object[] list)
Returns the number of elements in the supplied list. |
protected static Object[] |
testAndAdd(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
Helper function for testAndAddRef(java.lang.Object[], java.lang.Object), etc. |
static Object[] |
testAndAdd(Object[] list,
Object element)
Searches through the list checking to see if the element supplied is already in the list (using equals() to check for
equality) and adds it if it is not. |
static Object[] |
testAndAddRef(Object[] list,
Object element)
Searches through the list checking to see if the element supplied is already in the list (using reference equality to check for existence) and adds it if it is not. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static final ListUtil.EqualityComparator REFERENCE_COMP
protected static final ListUtil.EqualityComparator EQUALS_COMP
protected static final int DEFAULT_LIST_SIZE
| Constructor Detail |
|---|
public ListUtil()
| Method Detail |
|---|
public static int nextPowerOfTwo(int value)
public static Object[] add(Object[] list,
Object element)
list - the list to which to add the element. Can be null.element - the element to add.
public static Object[] add(Object[] list,
int startIdx,
Object element)
list - the list to which to add the element. Can be null.startIdx - the index at which to start looking for a spot.element - the element to add.
public static Object[] insert(Object[] list,
int index,
Object element)
list - the list in which to insert the element. Can be null.index - the index at which to insert the element.element - the element to insert.
public static Object[] testAndAddRef(Object[] list,
Object element)
list - the list to which to add the element. Can be null.element - the element to test and add.
public static Object[] testAndAdd(Object[] list,
Object element)
equals() to check for
equality) and adds it if it is not.
list - the list to which to add the element. Can be null.element - the element to test and add.
protected static Object[] testAndAdd(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
testAndAddRef(java.lang.Object[], java.lang.Object), etc.
public static boolean containsRef(Object[] list,
Object element)
list[idx] == element).
public static boolean contains(Object[] list,
Object element)
list[idx].equals(element)).
protected static boolean contains(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
containsRef(java.lang.Object[], java.lang.Object), etc.
public static int indexOfNull(Object[] list)
public static int indexOfRef(Object[] list,
Object element)
list[idx] == element) and returns its index
in the array.
public static int indexOf(Object[] list,
Object element)
list[idx].equals(element)).
protected static int indexOf(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
indexOfRef(java.lang.Object[], java.lang.Object), etc.
public static Object clearRef(Object[] list,
Object element)
list[idx] == element).
public static Object clear(Object[] list,
Object element)
list[idx].equals(element)).
protected static Object clear(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
clearRef(java.lang.Object[], java.lang.Object), etc.
public static Object removeRef(Object[] list,
Object element)
list[idx] == element). The elements
after the removed element will be slid down the array one spot to
fill the place of the removed element.
public static Object remove(Object[] list,
Object element)
list[idx].equals(element)). The
elements after the removed element will be slid down the array one
spot to fill the place of the removed element.
protected static Object remove(ListUtil.EqualityComparator eqc,
Object[] list,
Object element)
removeRef(java.lang.Object[], java.lang.Object), etc.
public static Object remove(Object[] list,
int index)
public static int size(Object[] list)
protected static Object[] accomodate(Object[] list,
int index)
protected static NullPointerException NPE()
public static void main(String[] args)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||