Hi,
Ich hab mich gefragt wie groß der Aufwand ist um ein eigenes Collection-Framework zu basteln. Ich hab selbst alle Interfaces und Implementierungen konzipiert und geschrieben (Ähnlichkeit mit denen der Java API sind nicht zu vermeiden). Das einzige was ich aus der API verwende, ist die Klasse "Object" (wer hätte es gedacht) und noch in dieser Version "NoSuchElementException".
Mir ist klar, dass 645 Zeilen Code eine menge Holz ist, ihr müsst ja nicht alles genau, Zeile für Zeile, durch gehen. Ich verlange auch nicht dass ihr hier anfangt das Ding zu debuggen. Nur mal überfliegen und ne Meinung abgeben, was gut, was schlecht ist.
Es gibt noch Schwachstelle im Bezug auf Multithreading und man kann die Interator For-Schleife nicht nutzen da mein Interface "Enumeration" nicht von der Java Klasse "Interator" abgeleitet ist, aber ansonsten bin ich mit meiner Umsetzung sehr zufrieden. Ich setzt bei meinen Projekten nun immer erfolgreich mein eigenes Collections-Framework ein.
Natürlich, um eine genauere Aussage zu treffen wäre es gut ein komplettes UML Diagramm meines Frameworks zu posten.
Dennoch meine Fragen:
1. Ist mir der Versuch gelungen (im Bezug auf diese Klasse).
2. Sieht das was ich da fabriziert habe halbwegs wie professioneller Code aus.
[HIGHLIGHT="Java"]package jay_ware.home.girls;
import java.util.NoSuchElementException;
import jay_ware.home.boys.Collection;
import jay_ware.home.boys.Enumeration;
import jay_ware.home.boys.List;
import jay_ware.hustelers.Revision;
/**
* A default implementation of the interface <code>List<code>.
*
* @param <E> the type of elements maintained by this XList.
*
* @author El Jay
* @version 1.0 19.04.2008
* @version 1.1 05.05.2008
*
*/
public class XList<E>
implements List<E>
{
/**
* The elements stored in this list.
*/
protected Object[] elements;
/**
* The maximum number of elements in this list.
*/
protected int size;
/**
* The first size of this list.
*/
protected final int INITIAL_SIZE;
/**
* The amount of elements in this list.
*/
protected int count;
/**
* Constructs an empty list with a default capacity of 8.
*/
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public XList()
{
this(8);
}
/**
* Constructs a list containing the elements of the specified collection.
*
* @param c the collection which hold the elements to add.
*
*/
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public XList(Collection<E> c)
{
this(c.count());
this.addAll(c);
}
/**
* Constructs a list with the specified capacity.
*
* @param capacity the capacity for the new list.
*
*/
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public XList(int capacity)
{
this.INITIAL_SIZE = capacity;
this.clear();
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean add(E e)
{
//Check whether the specified element is a null reference.
if (e == null)
{
//Throw a NullPointerException, because
//the specified element is null.
throw new NullPointerException
("Specified element is null");
}
//Check whether in the array is
//enough space for a new element.
if (count < size)
{
//Delegate the specified element and the
//index of a free position to the method 'set()'
return set(e, indexOf(null));
}
else
{
//There is not enough space for a new element,
//so resize the array to get more.
resize(size + INITIAL_SIZE);
//Now is enough space, so delegate the specified element
//and the index of a free position to the method 'set()'
return set(e, indexOf(null));
}
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean addAll(Collection<? extends E> c)
{
//Check first the specified collection whether it is null.
if (c == null)
{
throw new NullPointerException
("The specified collection is null");
}
//Now check the necessary space in the array to
//add all elements of the specified collection.
if ((size - count) < c.count())
{
//Get the amount of necessary space.
int necessary = c.count() + count;
//Resize this list, because there is not enough space
//for all elements of the specified collection. The new
//size is the calculated "necessary" size plus the initial
//size to get more space like a buffer.
resize(necessary + INITIAL_SIZE);
}
//
//Now it is ready to start the process of adding.
//
//The final result of this operation.
boolean result = true;
//Maybe a empty position in the array.
int empty = 0;
//The number of elements which was already added.
int added = 0;
//An Enumeration over the elements of the specified collection.
Enumeration<? extends E> e = c.elements();
//Repeat it until the last element is added.
do
{
//Check whether the position in the array is empty.
if (elements[empty] == null)
{
//The position is empty, add a element.
//Delegates the index of the empty position and the
//next element in the enumeration to the method set();
boolean successful = set(e.next(), empty);
//Check whether the process was successful.
if (!successful)
{
//The final result is false if one of the
//elements couldn't added to this list.
result = false;
}
//Increment the index of the next empty position.
empty++;
//Increment the variable which count
//the number of added elements.
added++;
}
//If the position is not empty find the next one.
else
{
empty = indexOf(null);
}
//Check whether all elements are added to the array.
} while (added < c.count());
//Return the final result of this operation.
return result;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
protected void checkIndex(int index)
{
//Check whether the specified index is a legal index.
//Check whether the specified index is greater than Integer.MAX_VALUE.
if (index > Integer.MAX_VALUE)
{
throw new ArrayIndexOutOfBoundsException
("The specified index is greater than Integer.MAX_VALUE" +
" -> index = " + index + "!!");
}
//Check whether the specified index is less than 0.
if (index < 0)
{
throw new ArrayIndexOutOfBoundsException
("The specified index is less than 0 -> index = " + index + "!!");
}
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public void clear()
{
//Resizes the array to the first given size,
//all old elements were deleted and the list is clear.
this.elements = new Object[this.INITIAL_SIZE];
//Restore to defaults
this.size = this.INITIAL_SIZE;
this.count = 0;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean contains(Object obj)
{
//Check whether the specified object is not null.
if (obj != null)
{
//Use the method indexOf() to find out whether
//this list contain the specified object.
return indexOf(obj) >= 0;
}
//The specified object is null, so it can't be present in this list.
return false;
}
@Revision(author = "El Jay", version = "1.0", date = "05.05.2008")
public boolean containsAll(Collection<?> c)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether this collection or the specified one is empty, if so
//return false, because the test is not necessary.
if (this.isEmpty() || c.isEmpty())
{
return false;
}
//Get a enumeration over the specified collection.
Enumeration<?> e = c.elements();
//The final result of this operation.
boolean result = false;
//Walk through the enumeration.
do
{
//Check whether the enumeration has more elements.
if (e.hasNext())
{
//Check whether this collection contain
//the next element in the enumeration.
result = contains(e.next());
}
//Repeat this until the enumeration has no more elements.
else break;
//Only one false if necessary to say that this list doesn't contain
//all elements of the specified collection.
} while (result);
//Return the final result of this operation.
return result;
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public int count()
{
return this.count;
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
public Enumeration<E> elements()
{
return new Enumeration<E>()
{
//The courser of this enumeration.
int courser = 0;
public boolean hasNext()
{
//Returns true if the courser is less than the array's size.
return courser < size;
}
public E next()
{
if (isEmpty())
{
throw new NoSuchElementException
("The list dosen't contain any element!!");
}
//Hold an element at the current courser position.
E element = null;
//Check that the enumeration don't reach the end of the array.
if (hasNext())
{
//May repeat the following lines to skip a null reference.
//Repeat this until the element is not null.
while (element == null && hasNext())
{
//Get the element at the courser position and
//increment the courser of this enumeration.
element = (E) elements[courser++];
}
//Return the element at the current courser position.
return element;
}
//Return the last element.
return element;
}
};
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
public E get(int index)
{
//Delegate the specified index to the method checkIndex()
//to check whether the specified index is a legal index.
checkIndex(index);
//Returns the element at the specified index.
return (E) elements[index];
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public int indexOf(Object obj)
{
//Walk through the array.
for (int i = 0; i < size; i++)
{
//Check whether the specified object is null, that
//indicates the operation looks for an empty position.
if (obj == null)
{
//Check whether the element at the index i is null.
if (elements == null)
{
//The element at the index i
//is null, so return the index.
return i;
}
}
//The specified object isn't null so the operation look
//for a occurrence of the specified object in this list
else
{
//Prevent an exception, so check that the
//element at the index i is not null.
if (elements != null)
{
//Compare the element at the index i
//with the specified object.
if (elements.equals(obj))
{
//The comparison was successful, so return the index.
return i;
}
}
}
}
//The operation failed, it didn't find a occurrence of specified
//object in this list or there is no empty position in this list.
return -1;
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public boolean isEmpty()
{
return this.count == 0;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean remove(int index)
{
//Delegate the specified index to the method checkIndex()
//to check whether the specified index is a legal index.
checkIndex(index);
//Remove the element at the specified index.
elements[index] = null;
//Decrement the element count.
count--;
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean remove(Object obj)
{
//Find the index of the specified object in this list.
int index = indexOf(obj);
//Check whether is greater than 0, so the
//specified object is present in this list.
if (index > 0)
{
//Delegate the specified index to the method remove(index) to
//remove the specified object by using the index of it.
return remove(index);
}
return false;
}
@Revision(author = "El Jay", version = "1.0", date = "06.05.2008")
public boolean removeAll(Collection<? extends E> c)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether this collection or the specified one is empty, if so
//return false, because the operation is not necessary.
if (this.isEmpty() || c.isEmpty())
{
return false;
}
//With this variable can test whether this list could changed.
int test = this.count;
//The index of the element.
int index = -1;
//Get a enumeration over the specified collection.
Enumeration<?> e = c.elements();
//Walk through the enumeration.
while (e.hasNext())
{
//get the index of the next element
//in the enumeration in this list.
index = indexOf(e.next());
//Check whether the specified index isn't less
//than 0, so the element is present in this list.
if (index >= 0)
{
elements[index] = null;
count--;
}
}
//Return true if the test variable is greater than the current list's
//count, so the operation could remove some elements.
return test > count;
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
protected void resize(int size)
{
// //
//It is important to know, is the specified size greater or //
//less than current size. If the specified size is less than the//
//current size some elements would be deleted, because they may //
//be out of new bounds after resizing, but the smallest size is //
//the number of elements in the current array. //
// //
if (size < this.size)
{
//The smallest size that a list can get is the number of elements.
//An array to hold the data of the current array.
Object[] old = new Object[this.count];
//To get the elements without the empty positions in the array the
//operation use a enumeration over the elements of this list.
Enumeration<E> en = this.elements();
//Walk throw the enumeration.
for (int i = 0; i < this.count; i++)
{
old = en.next();
}
//All elements were now copied in the array,
//now resize the list's array.
elements = new Object[this.count];
//Copy the elements back in the list's array.
System.arraycopy(old, 0, elements, 0, this.count);
}
//The size is greater so resize the array to the new size.
else
{
//An array as a backup of the current array's data.
Object[] old = new Object[this.size];
//Copy the data in the new array.
System.arraycopy(elements, 0, old, 0, this.size);
//...resizing...
elements = new Object[size];
//After copy the elements of the backup back in the list's array.
System.arraycopy(old, 0, elements, 0, this.size);
}
//Set the list's size to the new size.
this.size = size;
}
@Revision(author = "El Jay", version = "1.0", date = "06.05.2008")
public boolean retainAll(Collection<? extends E> c)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether the specified collection is empty.
if (c.isEmpty())
{
return false;
}
//Walk through the array of this list.
for (int i = 0; i < size; i++)
{
//Get the element at the index i.
Object obj = elements;
//Check whether the specified object isn't null
if (obj != null)
{
//Check whether the specified collection
//doesn't contain the object.
if (!c.contains(obj))
{
elements = null;
count--;
}
}
}
//Always true.
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
public boolean set(E e, int index)
{
//Check the specified element first.
if (e == null)
{
throw new NullPointerException
("The specified element is null");
}
//Check the specified index.
if (index < 0)
{
throw new ArrayIndexOutOfBoundsException
("The specified index is less than 0 -> index = " + index);
}
//The specified index is greater 0.
else
{
//Check that the specified index is in the array's bounds,
//so the specified index is less than the array's size.
if (index < size)
{
//...
}
//The specified index is out of bounds.
else
{
//Resize the array to a size that the specified index is
//in the array's bounds, to more formally the index is the
//last index in the array.
resize(index + 1);
}
}
//The current element at the specified index.
E current = (E) elements[index];
//When all parameters are tested add the
//specified element to this list.
elements[index] = e;
//Increment the element counter if at the specified index
//is no element to replace and the specified one is a new.
if (current == null)
{
count++;
}
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "06.05.2008")
public boolean setAll(Collection<? extends E> c, int index)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether the specified collection is empty.
if (c.isEmpty())
{
return false;
}
//
//Check the index.
//
if (index > (size - c.count() - 1))
{
resize(size + index + c.count());
}
//Get an enumeration over the elements of the specified collection.
Enumeration<? extends E> e = c.elements();
//Walk through the enumeration until
//the enumeration doesn't has more.
while (e.hasNext())
{
//To add an element of the collection, delegate one
//element at time to the method set() with the index
//which increment after every adding.
set(e.next(), index++);
}
//Always true.
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public int size()
{
return this.size;
}
@Revision(author = "El Jay", version = "1.0", date = "05.05.2008")
public List<E> subList(int start, int end)
{
//Check the parameters first.
//Whether the start index is greater than the end index.
if (start >= end)
{
throw new IllegalArgumentException
("start index is greater than the end index!!");
}
//Whether the start index is out of bounds.
checkIndex(start);
//Whether the end index is out of bounds.
checkIndex(end);
//Get the capacity of the sublist.
int capacity = end - start + 1;
//Instantiate a new list object.
XList<E> l = new XList<E>(capacity);
//Copy the elements into the new list.
System.arraycopy(elements, start, l.elements, 0, capacity);
//Set the element count of the sub list.
l.count = capacity;
//Return the sub list.
return l;
}
}[/HIGHLIGHT]
Wenn ihr euch fragt was das für komische Packages sind (Z.1, Z.5-7, Z.9), das ist creative Namensgebung
El Jay
Ich hab mich gefragt wie groß der Aufwand ist um ein eigenes Collection-Framework zu basteln. Ich hab selbst alle Interfaces und Implementierungen konzipiert und geschrieben (Ähnlichkeit mit denen der Java API sind nicht zu vermeiden). Das einzige was ich aus der API verwende, ist die Klasse "Object" (wer hätte es gedacht) und noch in dieser Version "NoSuchElementException".
Mir ist klar, dass 645 Zeilen Code eine menge Holz ist, ihr müsst ja nicht alles genau, Zeile für Zeile, durch gehen. Ich verlange auch nicht dass ihr hier anfangt das Ding zu debuggen. Nur mal überfliegen und ne Meinung abgeben, was gut, was schlecht ist.
Es gibt noch Schwachstelle im Bezug auf Multithreading und man kann die Interator For-Schleife nicht nutzen da mein Interface "Enumeration" nicht von der Java Klasse "Interator" abgeleitet ist, aber ansonsten bin ich mit meiner Umsetzung sehr zufrieden. Ich setzt bei meinen Projekten nun immer erfolgreich mein eigenes Collections-Framework ein.
Natürlich, um eine genauere Aussage zu treffen wäre es gut ein komplettes UML Diagramm meines Frameworks zu posten.
Dennoch meine Fragen:
1. Ist mir der Versuch gelungen (im Bezug auf diese Klasse).
2. Sieht das was ich da fabriziert habe halbwegs wie professioneller Code aus.
[HIGHLIGHT="Java"]package jay_ware.home.girls;
import java.util.NoSuchElementException;
import jay_ware.home.boys.Collection;
import jay_ware.home.boys.Enumeration;
import jay_ware.home.boys.List;
import jay_ware.hustelers.Revision;
/**
* A default implementation of the interface <code>List<code>.
*
* @param <E> the type of elements maintained by this XList.
*
* @author El Jay
* @version 1.0 19.04.2008
* @version 1.1 05.05.2008
*
*/
public class XList<E>
implements List<E>
{
/**
* The elements stored in this list.
*/
protected Object[] elements;
/**
* The maximum number of elements in this list.
*/
protected int size;
/**
* The first size of this list.
*/
protected final int INITIAL_SIZE;
/**
* The amount of elements in this list.
*/
protected int count;
/**
* Constructs an empty list with a default capacity of 8.
*/
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public XList()
{
this(8);
}
/**
* Constructs a list containing the elements of the specified collection.
*
* @param c the collection which hold the elements to add.
*
*/
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public XList(Collection<E> c)
{
this(c.count());
this.addAll(c);
}
/**
* Constructs a list with the specified capacity.
*
* @param capacity the capacity for the new list.
*
*/
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public XList(int capacity)
{
this.INITIAL_SIZE = capacity;
this.clear();
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean add(E e)
{
//Check whether the specified element is a null reference.
if (e == null)
{
//Throw a NullPointerException, because
//the specified element is null.
throw new NullPointerException
("Specified element is null");
}
//Check whether in the array is
//enough space for a new element.
if (count < size)
{
//Delegate the specified element and the
//index of a free position to the method 'set()'
return set(e, indexOf(null));
}
else
{
//There is not enough space for a new element,
//so resize the array to get more.
resize(size + INITIAL_SIZE);
//Now is enough space, so delegate the specified element
//and the index of a free position to the method 'set()'
return set(e, indexOf(null));
}
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean addAll(Collection<? extends E> c)
{
//Check first the specified collection whether it is null.
if (c == null)
{
throw new NullPointerException
("The specified collection is null");
}
//Now check the necessary space in the array to
//add all elements of the specified collection.
if ((size - count) < c.count())
{
//Get the amount of necessary space.
int necessary = c.count() + count;
//Resize this list, because there is not enough space
//for all elements of the specified collection. The new
//size is the calculated "necessary" size plus the initial
//size to get more space like a buffer.
resize(necessary + INITIAL_SIZE);
}
//
//Now it is ready to start the process of adding.
//
//The final result of this operation.
boolean result = true;
//Maybe a empty position in the array.
int empty = 0;
//The number of elements which was already added.
int added = 0;
//An Enumeration over the elements of the specified collection.
Enumeration<? extends E> e = c.elements();
//Repeat it until the last element is added.
do
{
//Check whether the position in the array is empty.
if (elements[empty] == null)
{
//The position is empty, add a element.
//Delegates the index of the empty position and the
//next element in the enumeration to the method set();
boolean successful = set(e.next(), empty);
//Check whether the process was successful.
if (!successful)
{
//The final result is false if one of the
//elements couldn't added to this list.
result = false;
}
//Increment the index of the next empty position.
empty++;
//Increment the variable which count
//the number of added elements.
added++;
}
//If the position is not empty find the next one.
else
{
empty = indexOf(null);
}
//Check whether all elements are added to the array.
} while (added < c.count());
//Return the final result of this operation.
return result;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
protected void checkIndex(int index)
{
//Check whether the specified index is a legal index.
//Check whether the specified index is greater than Integer.MAX_VALUE.
if (index > Integer.MAX_VALUE)
{
throw new ArrayIndexOutOfBoundsException
("The specified index is greater than Integer.MAX_VALUE" +
" -> index = " + index + "!!");
}
//Check whether the specified index is less than 0.
if (index < 0)
{
throw new ArrayIndexOutOfBoundsException
("The specified index is less than 0 -> index = " + index + "!!");
}
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public void clear()
{
//Resizes the array to the first given size,
//all old elements were deleted and the list is clear.
this.elements = new Object[this.INITIAL_SIZE];
//Restore to defaults
this.size = this.INITIAL_SIZE;
this.count = 0;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean contains(Object obj)
{
//Check whether the specified object is not null.
if (obj != null)
{
//Use the method indexOf() to find out whether
//this list contain the specified object.
return indexOf(obj) >= 0;
}
//The specified object is null, so it can't be present in this list.
return false;
}
@Revision(author = "El Jay", version = "1.0", date = "05.05.2008")
public boolean containsAll(Collection<?> c)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether this collection or the specified one is empty, if so
//return false, because the test is not necessary.
if (this.isEmpty() || c.isEmpty())
{
return false;
}
//Get a enumeration over the specified collection.
Enumeration<?> e = c.elements();
//The final result of this operation.
boolean result = false;
//Walk through the enumeration.
do
{
//Check whether the enumeration has more elements.
if (e.hasNext())
{
//Check whether this collection contain
//the next element in the enumeration.
result = contains(e.next());
}
//Repeat this until the enumeration has no more elements.
else break;
//Only one false if necessary to say that this list doesn't contain
//all elements of the specified collection.
} while (result);
//Return the final result of this operation.
return result;
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public int count()
{
return this.count;
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
public Enumeration<E> elements()
{
return new Enumeration<E>()
{
//The courser of this enumeration.
int courser = 0;
public boolean hasNext()
{
//Returns true if the courser is less than the array's size.
return courser < size;
}
public E next()
{
if (isEmpty())
{
throw new NoSuchElementException
("The list dosen't contain any element!!");
}
//Hold an element at the current courser position.
E element = null;
//Check that the enumeration don't reach the end of the array.
if (hasNext())
{
//May repeat the following lines to skip a null reference.
//Repeat this until the element is not null.
while (element == null && hasNext())
{
//Get the element at the courser position and
//increment the courser of this enumeration.
element = (E) elements[courser++];
}
//Return the element at the current courser position.
return element;
}
//Return the last element.
return element;
}
};
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
public E get(int index)
{
//Delegate the specified index to the method checkIndex()
//to check whether the specified index is a legal index.
checkIndex(index);
//Returns the element at the specified index.
return (E) elements[index];
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public int indexOf(Object obj)
{
//Walk through the array.
for (int i = 0; i < size; i++)
{
//Check whether the specified object is null, that
//indicates the operation looks for an empty position.
if (obj == null)
{
//Check whether the element at the index i is null.
if (elements == null)
{
//The element at the index i
//is null, so return the index.
return i;
}
}
//The specified object isn't null so the operation look
//for a occurrence of the specified object in this list
else
{
//Prevent an exception, so check that the
//element at the index i is not null.
if (elements != null)
{
//Compare the element at the index i
//with the specified object.
if (elements.equals(obj))
{
//The comparison was successful, so return the index.
return i;
}
}
}
}
//The operation failed, it didn't find a occurrence of specified
//object in this list or there is no empty position in this list.
return -1;
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public boolean isEmpty()
{
return this.count == 0;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean remove(int index)
{
//Delegate the specified index to the method checkIndex()
//to check whether the specified index is a legal index.
checkIndex(index);
//Remove the element at the specified index.
elements[index] = null;
//Decrement the element count.
count--;
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "27.04.2008")
public boolean remove(Object obj)
{
//Find the index of the specified object in this list.
int index = indexOf(obj);
//Check whether is greater than 0, so the
//specified object is present in this list.
if (index > 0)
{
//Delegate the specified index to the method remove(index) to
//remove the specified object by using the index of it.
return remove(index);
}
return false;
}
@Revision(author = "El Jay", version = "1.0", date = "06.05.2008")
public boolean removeAll(Collection<? extends E> c)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether this collection or the specified one is empty, if so
//return false, because the operation is not necessary.
if (this.isEmpty() || c.isEmpty())
{
return false;
}
//With this variable can test whether this list could changed.
int test = this.count;
//The index of the element.
int index = -1;
//Get a enumeration over the specified collection.
Enumeration<?> e = c.elements();
//Walk through the enumeration.
while (e.hasNext())
{
//get the index of the next element
//in the enumeration in this list.
index = indexOf(e.next());
//Check whether the specified index isn't less
//than 0, so the element is present in this list.
if (index >= 0)
{
elements[index] = null;
count--;
}
}
//Return true if the test variable is greater than the current list's
//count, so the operation could remove some elements.
return test > count;
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
protected void resize(int size)
{
// //
//It is important to know, is the specified size greater or //
//less than current size. If the specified size is less than the//
//current size some elements would be deleted, because they may //
//be out of new bounds after resizing, but the smallest size is //
//the number of elements in the current array. //
// //
if (size < this.size)
{
//The smallest size that a list can get is the number of elements.
//An array to hold the data of the current array.
Object[] old = new Object[this.count];
//To get the elements without the empty positions in the array the
//operation use a enumeration over the elements of this list.
Enumeration<E> en = this.elements();
//Walk throw the enumeration.
for (int i = 0; i < this.count; i++)
{
old = en.next();
}
//All elements were now copied in the array,
//now resize the list's array.
elements = new Object[this.count];
//Copy the elements back in the list's array.
System.arraycopy(old, 0, elements, 0, this.count);
}
//The size is greater so resize the array to the new size.
else
{
//An array as a backup of the current array's data.
Object[] old = new Object[this.size];
//Copy the data in the new array.
System.arraycopy(elements, 0, old, 0, this.size);
//...resizing...
elements = new Object[size];
//After copy the elements of the backup back in the list's array.
System.arraycopy(old, 0, elements, 0, this.size);
}
//Set the list's size to the new size.
this.size = size;
}
@Revision(author = "El Jay", version = "1.0", date = "06.05.2008")
public boolean retainAll(Collection<? extends E> c)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether the specified collection is empty.
if (c.isEmpty())
{
return false;
}
//Walk through the array of this list.
for (int i = 0; i < size; i++)
{
//Get the element at the index i.
Object obj = elements;
//Check whether the specified object isn't null
if (obj != null)
{
//Check whether the specified collection
//doesn't contain the object.
if (!c.contains(obj))
{
elements = null;
count--;
}
}
}
//Always true.
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "28.04.2008")
public boolean set(E e, int index)
{
//Check the specified element first.
if (e == null)
{
throw new NullPointerException
("The specified element is null");
}
//Check the specified index.
if (index < 0)
{
throw new ArrayIndexOutOfBoundsException
("The specified index is less than 0 -> index = " + index);
}
//The specified index is greater 0.
else
{
//Check that the specified index is in the array's bounds,
//so the specified index is less than the array's size.
if (index < size)
{
//...
}
//The specified index is out of bounds.
else
{
//Resize the array to a size that the specified index is
//in the array's bounds, to more formally the index is the
//last index in the array.
resize(index + 1);
}
}
//The current element at the specified index.
E current = (E) elements[index];
//When all parameters are tested add the
//specified element to this list.
elements[index] = e;
//Increment the element counter if at the specified index
//is no element to replace and the specified one is a new.
if (current == null)
{
count++;
}
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "06.05.2008")
public boolean setAll(Collection<? extends E> c, int index)
{
//Check whether the specified collection is null.
if (c == null)
{
throw new NullPointerException
("Specified collection can't be null");
}
//Check whether the specified collection is empty.
if (c.isEmpty())
{
return false;
}
//
//Check the index.
//
if (index > (size - c.count() - 1))
{
resize(size + index + c.count());
}
//Get an enumeration over the elements of the specified collection.
Enumeration<? extends E> e = c.elements();
//Walk through the enumeration until
//the enumeration doesn't has more.
while (e.hasNext())
{
//To add an element of the collection, delegate one
//element at time to the method set() with the index
//which increment after every adding.
set(e.next(), index++);
}
//Always true.
return true;
}
@Revision(author = "El Jay", version = "1.0", date = "19.04.2008")
public int size()
{
return this.size;
}
@Revision(author = "El Jay", version = "1.0", date = "05.05.2008")
public List<E> subList(int start, int end)
{
//Check the parameters first.
//Whether the start index is greater than the end index.
if (start >= end)
{
throw new IllegalArgumentException
("start index is greater than the end index!!");
}
//Whether the start index is out of bounds.
checkIndex(start);
//Whether the end index is out of bounds.
checkIndex(end);
//Get the capacity of the sublist.
int capacity = end - start + 1;
//Instantiate a new list object.
XList<E> l = new XList<E>(capacity);
//Copy the elements into the new list.
System.arraycopy(elements, start, l.elements, 0, capacity);
//Set the element count of the sub list.
l.count = capacity;
//Return the sub list.
return l;
}
}[/HIGHLIGHT]
Wenn ihr euch fragt was das für komische Packages sind (Z.1, Z.5-7, Z.9), das ist creative Namensgebung
El Jay