001package conexp.fx.core.collections;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2019 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025
026import java.util.NoSuchElementException;
027
028import com.google.common.collect.UnmodifiableListIterator;
029
030public abstract class SimpleListIterator<E> extends UnmodifiableListIterator<E> {
031
032  private int i = 0;
033  private E   n = null;
034  private E   p = null;
035
036  public SimpleListIterator() {
037    this(0);
038  }
039
040  public SimpleListIterator(final int i) {
041    super();
042    createFirst(i);
043  }
044
045  public SimpleListIterator(final boolean dontCreateFirst) {
046    super();
047    if (!dontCreateFirst)
048      createFirst(0);
049  }
050
051  protected final void createFirst(final int i) {
052    int j = 0;
053    n = createNext();
054    while (j++ < i && hasNext())
055      next();
056  }
057
058  protected abstract E createNext();
059
060  protected abstract E createPrevious();
061
062  public final boolean hasNext() {
063    return n != null;
064  }
065
066  public final boolean hasPrevious() {
067    return p != null;
068  }
069
070  public final E next() {
071    if (n == null)
072      throw new NoSuchElementException();
073    ++i;
074    p = n;
075    n = createNext();
076    return p;
077  }
078
079  public final E previous() {
080    if (p == null)
081      throw new NoSuchElementException();
082    --i;
083    n = p;
084    p = createPrevious();
085    return n;
086  }
087
088  public final int nextIndex() {
089    return i;
090  }
091
092  public final int previousIndex() {
093    return i - 1;
094  }
095}