/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package net.sf.l2j.custom;

import javolution.util.FastList;

/**
 *
 * @author  JOJO
 */
@SuppressWarnings("serial")
public class ExpressList<E> extends FastList
{
	private int _currentIndex = -1;
	private Node _currentNode = null;

	public Object GET(int i) {
		if (_currentNode == null) {
			_currentNode = nodeAt(i);
		} else {
			if (i == _currentIndex) {
				/**/
			} else if (i == _currentIndex + 1) {
				_currentNode = _currentNode.getNext();
			} else if (i == _currentIndex - 1) {
				_currentNode = _currentNode.getPrevious();
			} else {
				_currentNode = nodeAt(i);
			}
		}
		_currentIndex = i;
		return _currentNode.getValue();
	}

	private final Node nodeAt(int i) {
		int j = size();
		if (i <= j / 2) {
			Node node = head();
			for (int k = i; k-- >= 0;)
				node = node.getNext();
			return node;
		} else {
			Node node1 = tail();
			for (int l = j - i; l-- > 0;)
				node1 = node1.getPrevious();
			return node1;
		}
	}

	void test() {
		ExpressList<String> s = new ExpressList<String>();
		s.add("うんこ");
		s.add("ちんこ");
		s.add("まんこ");
		System.out.println(s.GET(0));
		System.out.println(s.GET(1));
		System.out.println(s.GET(2));
		System.out.println(s.GET(2));
		System.out.println(s.GET(1));
		System.out.println(s.GET(0));
	}
}
