• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012, Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 package com.android.tools.smali.util;
32 
33 import javax.annotation.Nonnull;
34 import javax.annotation.Nullable;
35 import java.util.AbstractSequentialList;
36 import java.util.Iterator;
37 import java.util.ListIterator;
38 import java.util.NoSuchElementException;
39 
40 public abstract class AbstractForwardSequentialList<T> extends AbstractSequentialList<T> {
41 
iterator(int index)42     @Nonnull private Iterator<T> iterator(int index) {
43         if (index < 0) {
44             throw new NoSuchElementException();
45         }
46 
47         Iterator<T> it = iterator();
48         for (int i=0; i<index; i++) {
49             it.next();
50         }
51         return it;
52     }
53 
iterator()54     @Override @Nonnull public abstract Iterator<T> iterator();
55 
listIterator(final int initialIndex)56     @Override @Nonnull public ListIterator<T> listIterator(final int initialIndex) {
57 
58         final Iterator<T> initialIterator;
59         try {
60             initialIterator = iterator(initialIndex);
61         } catch (NoSuchElementException ex) {
62             throw new IndexOutOfBoundsException();
63         }
64 
65         return new AbstractListIterator<T>() {
66             private int index = initialIndex - 1;
67             @Nullable private Iterator<T> forwardIterator = initialIterator;
68 
69             @Nonnull
70             private Iterator<T> getForwardIterator() {
71                 if (forwardIterator == null) {
72                     try {
73                         forwardIterator = iterator(index+1);
74                     } catch (IndexOutOfBoundsException ex) {
75                         throw new NoSuchElementException();
76                     }
77                 }
78                 return forwardIterator;
79             }
80 
81             @Override public boolean hasNext() {
82                 return getForwardIterator().hasNext();
83             }
84 
85             @Override public boolean hasPrevious() {
86                 return index >= 0;
87             }
88 
89             @Override public T next() {
90                 T ret = getForwardIterator().next();
91                 index++;
92                 return ret;
93             }
94 
95             @Override public int nextIndex() {
96                 return index+1;
97             }
98 
99             @Override public T previous() {
100                 forwardIterator = null;
101                 try {
102                     return iterator(index--).next();
103                 } catch (IndexOutOfBoundsException ex) {
104                     throw new NoSuchElementException();
105                 }
106             }
107 
108             @Override public int previousIndex() {
109                 return index;
110             }
111         };
112     }
113 
listIterator()114     @Override @Nonnull public ListIterator<T> listIterator() {
115         return listIterator(0);
116     }
117 }
118