1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.collection.AbstractSequentialList; 13 14 import hunt.collection.AbstractList; 15 import hunt.collection.Deque; 16 import hunt.collection.List; 17 18 import hunt.Exceptions; 19 20 21 /** 22 * This class provides a skeletal implementation of the <tt>List</tt> 23 * interface to minimize the effort required to implement this interface 24 * backed by a "sequential access" data store (such as a linked list). For 25 * random access data (such as an array), <tt>AbstractList</tt> should be used 26 * in preference to this class.<p> 27 * 28 * This class is the opposite of the <tt>AbstractList</tt> class in the sense 29 * that it implements the "random access" methods (<tt>get(int index)</tt>, 30 * <tt>set(int index, E element)</tt>, <tt>add(int index, E element)</tt> and 31 * <tt>remove(int index)</tt>) on top of the list's list iterator, instead of 32 * the other way around.<p> 33 * 34 * To implement a list the programmer needs only to extend this class and 35 * provide implementations for the <tt>listIterator</tt> and <tt>size</tt> 36 * methods. For an unmodifiable list, the programmer need only implement the 37 * list iterator's <tt>hasNext</tt>, <tt>next</tt>, <tt>hasPrevious</tt>, 38 * <tt>previous</tt> and <tt>index</tt> methods.<p> 39 * 40 * For a modifiable list the programmer should additionally implement the list 41 * iterator's <tt>set</tt> method. For a variable-size list the programmer 42 * should additionally implement the list iterator's <tt>remove</tt> and 43 * <tt>add</tt> methods.<p> 44 * 45 * The programmer should generally provide a void (no argument) and collection 46 * constructor, as per the recommendation in the <tt>Collection</tt> interface 47 * specification.<p> 48 * 49 * This class is a member of the 50 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 51 * Java Collections Framework</a>. 52 * 53 * @author Josh Bloch 54 * @author Neal Gafter 55 * @see Collection 56 * @see List 57 * @see AbstractList 58 * @see AbstractCollection 59 * @since 1.2 60 */ 61 62 abstract class AbstractSequentialList(E) : AbstractList!E { 63 /** 64 * Sole constructor. (For invocation by subclass constructors, typically 65 * implicit.) 66 */ 67 protected this() { 68 } 69 70 // /** 71 // * Returns the element at the specified position in this list. 72 // * 73 // * <p>This implementation first gets a list iterator pointing to the 74 // * indexed element (with <tt>listIterator(index)</tt>). Then, it gets 75 // * the element using <tt>ListIterator.next</tt> and returns it. 76 // * 77 // * @throws IndexOutOfBoundsException {@inheritDoc} 78 // */ 79 // override E get(int index) { 80 // try { 81 // return listIterator(index).next(); 82 // } catch (NoSuchElementException exc) { 83 // throw new IndexOutOfBoundsException("Index: " ~ index); 84 // } 85 // } 86 87 // /** 88 // * Replaces the element at the specified position in this list with the 89 // * specified element (optional operation). 90 // * 91 // * <p>This implementation first gets a list iterator pointing to the 92 // * indexed element (with <tt>listIterator(index)</tt>). Then, it gets 93 // * the current element using <tt>ListIterator.next</tt> and replaces it 94 // * with <tt>ListIterator.set</tt>. 95 // * 96 // * <p>Note that this implementation will throw an 97 // * <tt>UnsupportedOperationException</tt> if the list iterator does not 98 // * implement the <tt>set</tt> operation. 99 // * 100 // * @throws UnsupportedOperationException {@inheritDoc} 101 // * @throws ClassCastException {@inheritDoc} 102 // * @throws NullPointerException {@inheritDoc} 103 // * @throws IllegalArgumentException {@inheritDoc} 104 // * @throws IndexOutOfBoundsException {@inheritDoc} 105 // */ 106 // E set(int index, E element) { 107 // try { 108 // ListIterator!E e = listIterator(index); 109 // E oldVal = e.next(); 110 // e.set(element); 111 // return oldVal; 112 // } catch (NoSuchElementException exc) { 113 // throw new IndexOutOfBoundsException("Index: " ~index); 114 // } 115 // } 116 117 // /** 118 // * Inserts the specified element at the specified position in this list 119 // * (optional operation). Shifts the element currently at that position 120 // * (if any) and any subsequent elements to the right (adds one to their 121 // * indices). 122 // * 123 // * <p>This implementation first gets a list iterator pointing to the 124 // * indexed element (with <tt>listIterator(index)</tt>). Then, it 125 // * inserts the specified element with <tt>ListIterator.add</tt>. 126 // * 127 // * <p>Note that this implementation will throw an 128 // * <tt>UnsupportedOperationException</tt> if the list iterator does not 129 // * implement the <tt>add</tt> operation. 130 // * 131 // * @throws UnsupportedOperationException {@inheritDoc} 132 // * @throws ClassCastException {@inheritDoc} 133 // * @throws NullPointerException {@inheritDoc} 134 // * @throws IllegalArgumentException {@inheritDoc} 135 // * @throws IndexOutOfBoundsException {@inheritDoc} 136 // */ 137 // void add(int index, E element) { 138 // try { 139 // listIterator(index).add(element); 140 // } catch (NoSuchElementException exc) { 141 // throw new IndexOutOfBoundsException("Index: " ~ index.to!string); 142 // } 143 // } 144 145 // /** 146 // * Removes the element at the specified position in this list (optional 147 // * operation). Shifts any subsequent elements to the left (subtracts one 148 // * from their indices). Returns the element that was removed from the 149 // * list. 150 // * 151 // * <p>This implementation first gets a list iterator pointing to the 152 // * indexed element (with <tt>listIterator(index)</tt>). Then, it removes 153 // * the element with <tt>ListIterator.remove</tt>. 154 // * 155 // * <p>Note that this implementation will throw an 156 // * <tt>UnsupportedOperationException</tt> if the list iterator does not 157 // * implement the <tt>remove</tt> operation. 158 // * 159 // * @throws UnsupportedOperationException {@inheritDoc} 160 // * @throws IndexOutOfBoundsException {@inheritDoc} 161 // */ 162 // E remove(int index) { 163 // try { 164 // ListIterator!E e = listIterator(index); 165 // E outCast = e.next(); 166 // e.remove(); 167 // return outCast; 168 // } catch (NoSuchElementException exc) { 169 // throw new IndexOutOfBoundsException("Index: " ~ index.to!string); 170 // } 171 // } 172 173 174 // // Bulk Operations 175 176 // /** 177 // * Inserts all of the elements in the specified collection into this 178 // * list at the specified position (optional operation). Shifts the 179 // * element currently at that position (if any) and any subsequent 180 // * elements to the right (increases their indices). The new elements 181 // * will appear in this list in the order that they are returned by the 182 // * specified collection's iterator. The behavior of this operation is 183 // * undefined if the specified collection is modified while the 184 // * operation is in progress. (Note that this will occur if the specified 185 // * collection is this list, and it's nonempty.) 186 // * 187 // * <p>This implementation gets an iterator over the specified collection and 188 // * a list iterator over this list pointing to the indexed element (with 189 // * <tt>listIterator(index)</tt>). Then, it iterates over the specified 190 // * collection, inserting the elements obtained from the iterator into this 191 // * list, one at a time, using <tt>ListIterator.add</tt> followed by 192 // * <tt>ListIterator.next</tt> (to skip over the added element). 193 // * 194 // * <p>Note that this implementation will throw an 195 // * <tt>UnsupportedOperationException</tt> if the list iterator returned by 196 // * the <tt>listIterator</tt> method does not implement the <tt>add</tt> 197 // * operation. 198 // * 199 // * @throws UnsupportedOperationException {@inheritDoc} 200 // * @throws ClassCastException {@inheritDoc} 201 // * @throws NullPointerException {@inheritDoc} 202 // * @throws IllegalArgumentException {@inheritDoc} 203 // * @throws IndexOutOfBoundsException {@inheritDoc} 204 // */ 205 // // bool addAll(int index, Collection<E> c) { 206 // // try { 207 // // bool modified = false; 208 // // ListIterator!E e1 = listIterator(index); 209 // // Iterator<E> e2 = c.iterator(); 210 // // while (e2.hasNext()) { 211 // // e1.add(e2.next()); 212 // // modified = true; 213 // // } 214 // // return modified; 215 // // } catch (NoSuchElementException exc) { 216 // // throw new IndexOutOfBoundsException("Index: " ~index); 217 // // } 218 // // } 219 220 221 // Iterators 222 223 }