001package components.sequence;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005import java.util.List;
006import java.util.NoSuchElementException;
007
008/**
009 * {@code Sequence} represented as a {@link java.util.ArrayList
010 * java.util.ArrayList} with implementations of primary methods.
011 *
012 * @param <T>
013 *            type of {@code Sequence} entries
014 * @correspondence this = [value of $this.rep]
015 */
016public class Sequence1L<T> extends SequenceSecondary<T> {
017
018    /*
019     * Private members --------------------------------------------------------
020     */
021
022    /**
023     * Representation of {@code this}.
024     */
025    private List<T> rep;
026
027    /**
028     * Creator of initial representation.
029     */
030    private void createNewRep() {
031        this.rep = new ArrayList<T>();
032    }
033
034    /*
035     * Constructors -----------------------------------------------------------
036     */
037
038    /**
039     * No-argument constructor.
040     */
041    public Sequence1L() {
042        this.createNewRep();
043    }
044
045    /*
046     * Standard methods -------------------------------------------------------
047     */
048
049    @SuppressWarnings("unchecked")
050    @Override
051    public final Sequence<T> newInstance() {
052        try {
053            return this.getClass().getConstructor().newInstance();
054        } catch (ReflectiveOperationException e) {
055            throw new AssertionError(
056                    "Cannot construct object of type " + this.getClass());
057        }
058    }
059
060    @Override
061    public final void clear() {
062        this.createNewRep();
063    }
064
065    @Override
066    public final void transferFrom(Sequence<T> source) {
067        assert source != null : "Violation of: source is not null";
068        assert source != this : "Violation of: source is not this";
069        assert source instanceof Sequence1L<?>
070                : "Violation of: source is of dynamic type Sequence1L<?>";
071        /*
072         * This cast cannot fail since the assert above would have stopped
073         * execution in that case: source must be of dynamic type Sequence1L<?>,
074         * and the ? must be T or the call would not have compiled.
075         */
076        Sequence1L<T> localSource = (Sequence1L<T>) source;
077        this.rep = localSource.rep;
078        localSource.createNewRep();
079    }
080
081    /*
082     * Kernel methods ---------------------------------------------------------
083     */
084
085    @Override
086    public final void add(int pos, T x) {
087        assert x != null : "Violation of: x is not null";
088        assert 0 <= pos : "Violation of: 0 <= pos";
089        assert pos <= this.length() : "Violation of: pos <= |this|";
090        this.rep.add(pos, x);
091    }
092
093    @Override
094    public final T remove(int pos) {
095        assert 0 <= pos : "Violation of: 0 <= pos";
096        assert pos < this.length() : "Violation of: pos < |this|";
097        T result = this.rep.get(pos);
098        this.rep.remove(pos);
099        return result;
100    }
101
102    @Override
103    public final int length() {
104        return this.rep.size();
105    }
106
107    @Override
108    public final Iterator<T> iterator() {
109        return new Sequence1LIterator();
110    }
111
112    /**
113     * Implementation of {@code Iterator} interface for {@code Sequence1L}.
114     */
115    private final class Sequence1LIterator implements Iterator<T> {
116
117        /**
118         * Representation iterator.
119         */
120        private final Iterator<T> iterator;
121
122        /**
123         * No-argument constructor.
124         */
125        private Sequence1LIterator() {
126            this.iterator = Sequence1L.this.rep.iterator();
127        }
128
129        @Override
130        public boolean hasNext() {
131            return this.iterator.hasNext();
132        }
133
134        @Override
135        public T next() {
136            assert this.hasNext() : "Violation of: ~this.unseen /= <>";
137            if (!this.hasNext()) {
138                /*
139                 * Exception is supposed to be thrown in this case, but with
140                 * assertion-checking enabled it cannot happen because of assert
141                 * above.
142                 */
143                throw new NoSuchElementException();
144            }
145            return this.iterator.next();
146        }
147
148        @Override
149        public void remove() {
150            throw new UnsupportedOperationException("remove operation not supported");
151        }
152
153    }
154
155    /*
156     * Other methods (overridden for performance reasons) ---------------------
157     */
158
159    @Override
160    public final T entry(int pos) {
161        assert 0 <= pos : "Violation of: 0 <= pos";
162        assert pos < this.length() : "Violation of: pos < |this|";
163        return this.rep.get(pos);
164    }
165
166}