/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: ArrayList.h,v 1.27 2003/01/11 11:44:23 florian Exp $
 */


#ifndef INCLUDE_JAKELIB2_UTIL_ARRAYLIST_H
# define INCLUDE_JAKELIB2_UTIL_ARRAYLIST_H
#include "AbstractList.h"

namespace jakelib {
  namespace util {
    class ArrayListIterator;
    class Comparator;

    /**
     * @brief This class implements a simple to use, self-growing list.
     * Objects to be stored in an ArrayList must be derived from
     * \c Object.
     *
     * Each list has a capacity, and as the array reaches that capacity it
     * is automatically transferred to a larger array. You also have access to
     * ensureCapacity and trimToSize to control the backing array's size, avoiding
     * reallocation or wasted memory.
     *
     * \c ArrayList is not synchronized, so if you need multi-threaded access,
     * consider using \c Vector.
     *
     * @class ArrayList
     * @version $Id: ArrayList.h,v 1.27 2003/01/11 11:44:23 florian Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     */
    class ArrayList : public AbstractList
      {
      public:
	typedef enum {ASCENDING, DESCENDING} SortDirection;

	/**
	 * @brief Constructs an empty \c ArrayList with the specified initial capacity.
	 *
	 * @param   initialCapacity The initial capacity of the ArrayList.
	 */
	ArrayList(int initialCapacity = 10);

	~ArrayList();
  
	/**
	 * @brief Appends the supplied element to the end of this list.
	 *
	 * @param e the element to be appended to this list
	 * @return \c true, the add will always succeed
	 */
	jboolean add(jakelib::lang::Object* e);
  
	/**
	 * @brief Guarantees that this list will have at least enough capacity to
	 * hold \c minCapacity elements.
	 *
	 * This implementation will grow the list to
	 * max(current * 2, minCapacity) if (minCapacity > current).
	 *
	 * @param minCapacity the minimum guaranteed capacity
	 */
	void ensureCapacity(int minCapacity);
  
	/**
	 * @brief Trims the capacity of this List to be equal to its size;
	 * a memory saver.
	 */
	void trimToSize();

        /**
         * Returns the n-th element.
         *
         * @param n Index of element to retrieve (\c 0 .. \c size())
         * @throw ArrayIndexOutOfBoundsException If \c n >= \c size()
         * @throw IllegalArgumentException If \c n < 0
         */
        jakelib::lang::Object* get(int n);
  
	/**
	 * Returns the number of elements stored in the \c ArrayList.
	 *
	 * @return Number of elements.
	 */
	int size();
  
	/**
	 * @brief Determines whether the <tt>ArrayList</tt> is empty.
	 *
	 * @return \c true if the \c ArrayList is empty, \c false if it contains at least one item.
	 */
	jboolean isEmpty();
  
	/**
	 * @brief Remove all elements from the \c ArrayList.
	 */
	void clear();
  
	/**
	 * @brief Sets the element at the specified index.
	 *
	 * @param index the index at which the element is being set
	 * @param e the element to be set
	 * @return the element previously at the specified index
	 * @throws ArrayIndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
	 */
	jakelib::lang::Object* set(int index, jakelib::lang::Object* e);
  
	void insert(int index, jakelib::lang::Object* object);
  
	/**
	 * @brief Returns the lowest index at which element appears in this List, or
	 * \c -1 if it does not appear.
	 *
	 * @param e the element whose inclusion in the List is to be tested
	 * @return the index where \c e was found
	 */
	int indexOf(jakelib::lang::Object* e);
  
	/**
	 * @brief Returns \c true if element is in this \c ArrayList.
	 *
	 * @param e the element whose inclusion in the List is to be tested
	 * @return \c true if the list contains \c e
	 */
	jboolean contains(jakelib::lang::Object* e);
  
        /**
         * Removes the last element from this list. If the list is already
         * empty, it does nothing.
         */
	void removeLastElement();
  
	/**
	 * @brief Removes the element at the user-supplied index.
	 *
	 * @param index the index of the element to be removed
	 * @return the removed Object
	 * @throw IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
	 */
	jakelib::lang::Object* remove(int index);
  
        /**
         * Removes the given element from this list.
         * Only the first occurrence is removed from the list.
         *
         * @param object A pointer to the element to be removed.
         * @return \c true if the element could be found and removed,
         *         \c false if the element could be found.
         */
	jboolean remove(jakelib::lang::Object* object);

        /**
         * Returns a new \c ArrayListIterator for this list.
         */
	Iterator* iterator();

        /**
         * Performs a QSort on this \c ArrayList.
         *
         * @param comparator The comparator to be used to sort
         *                   the array.
         */
	virtual void qsort(Comparator* comparator);

        /**
         * Performs a Bubble Sort on this \c ArrayList.
         *
         * @param comparator The comparator to be used to sort
         *                   the array.
         * @param dir        Ordering direction, can be:
         *                   \li \c ASCENDING
         *                   \li \c DESCENDING
         */
	virtual void bubbleSort(Comparator* comparator, SortDirection dir = ASCENDING);

        /**
         * @brief Returns a string representation of this \c ArrayList.
         */
        jakelib::lang::String* toString();

        /**
         * Frees all resources. Don't use the \c ArrayList afterwards!
         */
        virtual void destroy();

      protected:

	/** Where the data is stored. */
	jakelib::lang::Object** elementData;
  
	/** The number of elements in this list. */
	int elementCount;
  
	/** The current capacity. */
	int capacity;

        /**
         * Internally used for QSort.
         */
	virtual void qsorti(Comparator* comparator, int left, int right);

	JAKELIB_DECLARE_CLASS(ArrayList);
      };

  }
}

#endif

