public abstract class SortedList
extends java.lang.Object
implements java.util.List
getKey(Object)
. The considered types of Object should be
all Types of instances, there will be added to the SortedList. Typically it may be
only one special type. Write at example:
class MySortedList extends SortedList { ... public String getKey(Object obj) { MyListItem item = (MyListItem)obj; //casting return item.stringxyz; //returns a string expression from items data. } }The method getKey is not defined as method from the stored object, but as method from this SortedList using the object. Therefore the Object should not implement any interface, the evaluation of the object is done here. But the Object have to be from the expected type, otherwise a ClassCastException is thrown when calling getKey(falseObject).
List mySortedList = new SortedList(new ArrayList(1000)) { public String getKey(Object obj) { MyListItem item = (MyListItem)obj; //casting return item.stringxyz; //returns a string expression from items data. } }In this sample the instance
mySortedList
is created with the given special
getKey()
-Method applicated to the added items on search algoritms,
but no extra class declaration is necessary. Just as, the constructor is called. You don't need an extra
constructor delegating the input list to the super class. This is a optimized programming.
date who change
2007-03-07 JcHartmut implements List, now it is useable as 'normal' List from outside.
2007-03-05 JcHartmut The old version of users derivated SortedList should implement the Comparator interface,
but now, the Comparators are private inside. Instead a Method getKey() should be implemented.
This is more explicit for using. The idea of SortedList is, it is a String based sorting.
The method search(String)
is one of the base methods of this idea. One List can sorting
Objects in one sorting order, but the same Objects may be contained in another SortedList in another
sorting order. So the SortedList should determine the key for ordering, the sorting should not
depends on the sorted Objects itself. This is a concept changing without great changing in the users programming.
Only the new Method getKey() should be implemented. The before it required method compare() are not used anymore.
2007-03-03 JcHartmut Constructor for List definition outside.
2006-01-07 JcHarmut initial revision
Modifier and Type | Field and Description |
---|---|
private int |
ix |
private java.util.List |
list |
protected java.util.Comparator |
objectComparator
Instance of a Comparator which compares an Object as item from list
with an input Object, used by add()
|
private static long |
serialVersionUID |
protected java.util.Comparator |
stringComparator
Instance of a Comparator which compares an Object as item from list
with a String search key, used by search()
|
Constructor and Description |
---|
SortedList() |
SortedList(java.util.List theList)
Uses a list outside, may be an java.util.ArrayList or a LinkedList.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
java.lang.Object element)
This Method throws a RuntimeException because the adding to the list
at a specified position is not concept of the SortedList.
|
boolean |
add(java.lang.Object obj)
Adds an Object independend from the existence of a object with the same key in list.
|
boolean |
addAll(java.util.Collection list) |
boolean |
addAll(int index,
java.util.Collection c)
This Method throws a RuntimeException because the adding to the list
at a specified position is not concept of the SortedList.
|
boolean |
addNew(java.lang.Object obj)
Adds only an Object if the key is not already content in list,
returns false otherwise.
|
void |
addTextFile(java.io.File fileIn,
java.lang.Class typeElement)
Deprecated.
it is from the old implementation ... TODO
|
void |
clear()
Delegates to List inside, see java.util.List.
|
int |
compare(java.lang.Object o1,
java.lang.Object o2)
The compare Method requested by interface Comparator.
|
boolean |
contains(java.lang.Object o)
Delegates to List inside, see java.util.List.
|
boolean |
containsAll(java.util.Collection c)
Delegates to List inside, see java.util.List.
|
java.lang.Object |
get(int ix)
returns the Object at the given index position.
|
java.lang.Object |
get(java.lang.String sKey)
Searches the first Object with the given Key.
|
int |
getIx() |
abstract java.lang.String |
getKey(java.lang.Object item) |
int |
indexOf(java.lang.Object o)
Delegates to List inside, see java.util.List.
|
boolean |
isEmpty()
Delegates to List inside, see java.util.List.
|
java.util.Iterator |
iterator()
Gets an iterator through the associated list.
|
int |
lastIndexOf(java.lang.Object o)
Delegates to List inside, see java.util.List.
|
java.util.ListIterator |
listIterator()
Delegates to List inside, see java.util.List.
|
java.util.ListIterator |
listIterator(int index)
Delegates to List inside, see java.util.List.
|
java.lang.Object |
remove(int index)
Delegates to List inside, see java.util.List.
|
boolean |
remove(java.lang.Object o)
Delegates to List inside, see java.util.List.
|
boolean |
removeAll(java.util.Collection c)
Delegates to List inside, see java.util.List.
|
void |
report(Report report,
java.lang.String sLine) |
boolean |
retainAll(java.util.Collection c)
Delegates to List inside, see java.util.List.
|
int |
search(java.lang.String sKey) |
java.lang.Object |
set(int index,
java.lang.Object element)
This Method throws a RuntimeException because the adding to the list
at a specified position is not concept of the SortedList.
|
int |
size()
Returns the actual size of the list (nr of stored Objects).
|
java.util.List |
subList(int fromIndex,
int toIndex)
Delegates to List inside, see java.util.List.
|
java.lang.Object[] |
toArray()
Delegates to List inside, see java.util.List.
|
java.lang.Object[] |
toArray(java.lang.Object[] a)
Delegates to List inside, see java.util.List.
|
void |
writeTextFile(java.io.File fileOut)
Deprecated.
it is from the old implementation ... TODO
|
private static final long serialVersionUID
private int ix
private final java.util.List list
protected final java.util.Comparator stringComparator
protected final java.util.Comparator objectComparator
public SortedList()
public SortedList(java.util.List theList)
public abstract java.lang.String getKey(java.lang.Object item)
public int compare(java.lang.Object o1, java.lang.Object o2)
public int getIx()
public int size()
size
in interface java.util.Collection
size
in interface java.util.List
public boolean addNew(java.lang.Object obj)
The
- Object to add.SortedListException
public boolean add(java.lang.Object obj)
add
in interface java.util.Collection
add
in interface java.util.List
The
- Object to add.SortedListException
public boolean addAll(java.util.Collection list)
addAll
in interface java.util.Collection
addAll
in interface java.util.List
public java.lang.Object get(java.lang.String sKey)
sKey
- public int search(java.lang.String sKey)
public java.lang.Object get(int ix)
search(String)
. If the ix ist negative, the Object
before in key order is returned. If the key is less than all Objects, it is
if the ix is -1, null will be returnedget
in interface java.util.List
ix
- Index of the element or negativ Indexpublic void report(Report report, java.lang.String sLine)
public void writeTextFile(java.io.File fileOut) throws java.io.IOException
fileOut
- java.io.IOException
public void addTextFile(java.io.File fileIn, java.lang.Class typeElement) throws java.io.IOException
typeElement
- Class object to build a new instance for a new element of the list
//@param cloneItem a blank instance of the item with the requested typejava.io.IOException
public java.util.Iterator iterator()
iterator
in interface java.lang.Iterable
iterator
in interface java.util.Collection
iterator
in interface java.util.List
public void add(int index, java.lang.Object element)
add
in interface java.util.List
public boolean addAll(int index, java.util.Collection c)
addAll
in interface java.util.List
public void clear()
clear
in interface java.util.Collection
clear
in interface java.util.List
public boolean contains(java.lang.Object o)
contains
in interface java.util.Collection
contains
in interface java.util.List
public boolean containsAll(java.util.Collection c)
containsAll
in interface java.util.Collection
containsAll
in interface java.util.List
public int indexOf(java.lang.Object o)
indexOf
in interface java.util.List
public boolean isEmpty()
isEmpty
in interface java.util.Collection
isEmpty
in interface java.util.List
public int lastIndexOf(java.lang.Object o)
lastIndexOf
in interface java.util.List
public java.util.ListIterator listIterator()
listIterator
in interface java.util.List
public java.util.ListIterator listIterator(int index)
listIterator
in interface java.util.List
public java.lang.Object remove(int index)
remove
in interface java.util.List
public boolean remove(java.lang.Object o)
remove
in interface java.util.Collection
remove
in interface java.util.List
public boolean removeAll(java.util.Collection c)
removeAll
in interface java.util.Collection
removeAll
in interface java.util.List
public boolean retainAll(java.util.Collection c)
retainAll
in interface java.util.Collection
retainAll
in interface java.util.List
public java.lang.Object set(int index, java.lang.Object element)
set
in interface java.util.List
public java.util.List subList(int fromIndex, int toIndex)
subList
in interface java.util.List
public java.lang.Object[] toArray()
toArray
in interface java.util.Collection
toArray
in interface java.util.List
public java.lang.Object[] toArray(java.lang.Object[] a)
toArray
in interface java.util.Collection
toArray
in interface java.util.List