/* 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 java.util.LinkedList;
import java.util.ListIterator;

/**
 *
 * @author  JOJO
 */
@SuppressWarnings("serial")
public class ExpressLink<E> extends LinkedList
{
	private int _currentIndex = -1;
	private E _currentNode = null;
	private ListIterator<E> _iterator = null;

	public E GET(int index) {
		if (index == _currentIndex) {
			_currentIndex = index;
			return _currentNode;
		} else if (index == _currentIndex + 1) {
			_currentIndex = index;
			if (_iterator.hasNext())
				return _currentNode = _iterator.next();
		} else if (index == _currentIndex - 1) {
			_currentIndex = index;
			if (_iterator.hasPrevious())
				return _currentNode = _iterator.previous();
		} else {
			_currentIndex = index;
			_iterator = listIterator(index);
			return _currentNode = _iterator.next();
		}
		return null;
	}

	void test() {
		ExpressLink<String> s = new ExpressLink<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));
	}
}
