/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2002
 *    Copyright by ESO (in the framework of the ALMA collaboration)
 *
 *    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
 */
package alma.acs.logging.table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The data structure containing all the rows of the table
 * (in the model coordinate space).
 * <P>
 * The entry in position <i>i</i> contains the key of the
 * log to show in the row <i>i</i> of the table.
 * 
 * TODO: add at the beginning of the list results in very poor performance:
 *       we should always append new items and convert the indexes properly!
 *         
 * @author acaproni
 *
 */
public class RowEntries {
	
	/**
	 * The entries i.e. the keys.
	 */
	private final List<Integer> entries = Collections.synchronizedList(new ArrayList<Integer>(20000));

	/**
	 * Remove the first <code>numOfEntries</code> entries from the 
	 * array.
	 * 
	 * @param numOfEntries
	 */
	public void removeFirstEntries(int numOfEntries) {
		if (numOfEntries<=0) {
			throw new IllegalArgumentException("Invalid number of entries to remove: "+numOfEntries);
		}
		synchronized (entries) {
			for (int t=0; t<numOfEntries; t++) {
				entries.remove(0);
			}
		}
	}
	
	/**
	 * Remove the last <code>numOfEntries</code> entries from the 
	 * array.
	 * 
	 * @param numOfEntries
	 */
	public void removeLastEntries(int numOfEntries) {
		if (numOfEntries<=0) {
			throw new IllegalArgumentException("Invalid number of entries to remove: "+numOfEntries);
		}
		synchronized (entries) {
			for (int t=0; t<numOfEntries && !entries.isEmpty(); t++) {
				entries.remove(entries.size()-1);
			}
		}
	}

	/**
	 * @return
	 * @see java.util.List#size()
	 */
	public int size() {
		return entries.size();
	}

	/**
	 * TODO: for better performance we should always add new elements at the end
	 * @param index
	 * @param element
	 * @see java.util.List#add(int, java.lang.Object)
	 */
	public void add(int index, Integer element) {
		entries.add(index, element);
	}

	/**
	 * @param index
	 * @return
	 * @see java.util.List#get(int)
	 */
	public Integer get(int index) {
		return entries.get(index);
	}

	/**
	 * 
	 * @see java.util.List#clear()
	 */
	public void clear() {
		entries.clear();
	}

	/**
	 * @param o
	 * @return
	 * @see java.util.List#indexOf(java.lang.Object)
	 */
	public int indexOf(Object o) {
		return entries.indexOf(o);
	}
}

@@ -1749,28 +1749,37 @@
  */%0A%09public 
-void
+List%3CInteger%3E
  removeFirst
@@ -1920,32 +1920,81 @@
 OfEntries);%0A%09%09%7D%0A
+%09%09List%3CInteger%3E temp = new ArrayList%3CInteger%3E();%0A
 %09%09synchronized (
@@ -2036,32 +2036,62 @@
 Entries; t++) %7B%0A
+%09%09%09%09temp.add(entries.get(0));%0A
 %09%09%09%09entries.remo
@@ -2106,16 +2106,31 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09return temp;%0A
 %09%7D%0A%09%0A%09/*
@@ -2238,30 +2238,68 @@
 ries%0A%09 *
-/%0A%09public void
+ @return the keys removed%0A%09 */%0A%09public List%3CInteger%3E
  removeL
@@ -2451,16 +2451,65 @@
 s);%0A%09%09%7D%0A
+%09%09List%3CInteger%3E temp = new ArrayList%3CInteger%3E();%0A
 %09%09synchr
@@ -2585,24 +2585,69 @@
 y(); t++) %7B%0A
+%09%09%09%09temp.add(entries.get(entries.size()-1));%0A
 %09%09%09%09entries.
@@ -2675,21 +2675,41 @@
 -1);%0A%09%09%09
-%7D%0A%09%09%7D
+%09%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return temp;
 %0A%09%7D%0A%0A%09/*

removeFirstEntries(...) and removeLastEntries(...) return a list with the keys they removed.