001package com.ericlam.mc.eldgui; 002 003import java.util.Collection; 004import java.util.ListIterator; 005 006/** 007 * 輔助用,循環迭代器 008 * @param <E> 元素類型 009 */ 010public final class CircuitIterator<E> implements ListIterator<E> { 011 012 private final E[] elements; 013 private int index = -1; 014 015 public CircuitIterator(E[] elements) { 016 this.elements = elements; 017 } 018 019 public CircuitIterator(E[] elements, int index) { 020 this.elements = elements; 021 this.index = index; 022 } 023 024 @SuppressWarnings("unchecked") 025 public CircuitIterator(Collection<E> elements) { 026 this.elements = (E[]) elements.toArray(); 027 } 028 029 @SuppressWarnings("unchecked") 030 public CircuitIterator(Collection<E> elements, int index) { 031 this.elements = (E[]) elements.toArray(); 032 this.index = index; 033 } 034 035 @Override 036 public boolean hasNext() { 037 return elements.length > 0; 038 } 039 040 @Override 041 public E next() { 042 if (index == elements.length - 1) { 043 index = -1; 044 } 045 return elements[++index]; 046 } 047 048 @Override 049 public boolean hasPrevious() { 050 return elements.length > 0; 051 } 052 053 @Override 054 public E previous() { 055 if (index <= 0) { 056 index = elements.length; 057 } 058 return elements[--index]; 059 } 060 061 @Override 062 public int nextIndex() { 063 return index + 1; 064 } 065 066 @Override 067 public int previousIndex() { 068 return index - 1; 069 } 070 071 @Override 072 public void remove() { 073 throw new UnsupportedOperationException("remove"); 074 } 075 076 @Override 077 public void set(E e) { 078 throw new UnsupportedOperationException("set"); 079 } 080 081 @Override 082 public void add(E e) { 083 throw new UnsupportedOperationException("add"); 084 } 085}