package org.voovan.tools.collection;

import org.voovan.tools.FastThread;
import org.voovan.tools.FastThreadLocal;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 对象链
 *
 * @author helyho
 *
 * Voovan Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class Chain<E>  extends ArrayList<E> {
	public static FastThreadLocal<AtomicInteger> iteratorLocal = new FastThreadLocal<AtomicInteger>();
	public static FastThreadLocal<AtomicInteger> invertedIteratorLocal = new FastThreadLocal<AtomicInteger>();
	private boolean isStop;
	private E currentObj;

	/**
	 * 构造函数
	 */
	public Chain(){
		isStop = false;
		rewind();
	}

	/**
	 * 重置链的迭代器
	 * @return 链对象
	 */
	public Chain<E> rewind(){
		isStop = false;
		if(iteratorLocal.get()==null) {
			iteratorLocal.set(new AtomicInteger(0));
		} else {
			iteratorLocal.get().set(0);
		}

		if(invertedIteratorLocal.get() == null) {
			invertedIteratorLocal.set(new AtomicInteger(this.size() - 1));
		} else {
			invertedIteratorLocal.get().set(this.size() - 1);
		}

		return this;
	}

	/**
	 * 迭代完成
	 */
	public void stop(){
		this.isStop = true;
	}

	/**
	 * 迭代器当前元素
	 * @return 当前元素
	 */
	public E current(){
		return currentObj;
	}

	/**
	 * 迭代器下一个元素
	 * @return 下一个元素
	 */
	public E next(){
		if(isStop){
			return null;
		} else {
			if(this.hasNext()){
				return this.get(iteratorLocal.get().getAndIncrement());
			} else {
				return null;
			}
		}
	}

	/**
	 * 迭代器是否有下一个对象
	 * @return 是否有下一个对象
	 */
	public boolean hasNext(){
		if(isStop){
			return false;
		} else {
			return iteratorLocal.get().get() <= this.size() - 1;
		}
	}

	/**
	 * 迭代器上一个元素
	 * @return 上一个元素
	 */
	public E previous(){
		if(isStop){
			return null;
		} else {
			if(this.hasPrevious()){
				return this.get(invertedIteratorLocal.get().getAndDecrement());
			} else {
				return null;
			}
		}
	}

	/**
	 * 迭代器是否有上一个对象
	 * @return 是否有上一个对象
	 */
	public boolean hasPrevious(){
		if(isStop){
			return false;
		} else {
			return invertedIteratorLocal.get().get() >= 0;
		}
	}

}

@@ -422,36 +422,32 @@
  iteratorLocal =
- new
  FastThreadLocal
@@ -438,33 +438,53 @@
  FastThreadLocal
-%3C
+.withInitial(()-%3Enew 
 AtomicInteger%3E()
@@ -476,26 +476,27 @@
 tomicInteger
-%3E
 (
+0)
 );%0A%09public s
@@ -559,12 +559,8 @@
 al =
- new
  Fas
@@ -563,33 +563,53 @@
  FastThreadLocal
-%3C
+.withInitial(()-%3Enew 
 AtomicInteger%3E()
@@ -609,10 +609,11 @@
 eger
-%3E
 (
+0)
 );%0A%09
@@ -815,19 +815,16 @@
 alse;%0A%09%09
-if(
 iterator
@@ -838,231 +838,17 @@
 et()
-==null) %7B%0A%09%09%09iteratorLocal.set(new AtomicInteger(0));%0A%09%09%7D else %7B%0A%09%09%09iteratorLocal.get().set(0);%0A%09%09%7D%0A%0A%09%09if(invertedIteratorLocal.get() == null) %7B%0A%09%09%09invertedIteratorLocal.set(new AtomicInteger(this.size() - 1));%0A%09%09%7D else %7B%0A%09
+.set(0);%0A
 %09%09in
@@ -894,20 +894,16 @@
 () - 1);
-%0A%09%09%7D
 %0A%0A%09%09retu

优化 Chain