1 30 31 32 package org.hsqldb.lib; 33 34 import java.util.NoSuchElementException ; 35 36 38 48 public class HsqlDeque extends BaseList implements HsqlList { 49 50 private Object [] list; 51 private int firstindex = 0; private int endindex = 0; 54 private static final int DEFAULT_INITIAL_CAPACITY = 10; 57 58 public HsqlDeque() { 59 list = new Object [DEFAULT_INITIAL_CAPACITY]; 60 } 61 62 public int size() { 63 return elementCount; 64 } 65 66 public Object getFirst() throws NoSuchElementException { 67 68 if (elementCount == 0) { 69 throw new NoSuchElementException (); 70 } 71 72 return list[firstindex]; 73 } 74 75 public Object getLast() throws NoSuchElementException { 76 77 if (elementCount == 0) { 78 throw new NoSuchElementException (); 79 } 80 81 return list[endindex - 1]; 82 } 83 84 public Object get(int i) throws IndexOutOfBoundsException { 85 86 int index = getInternalIndex(i); 87 88 return list[index]; 89 } 90 91 public void add(int i, Object o) throws IndexOutOfBoundsException { 92 throw new java.lang.RuntimeException (); 93 } 94 95 public Object set(int i, Object o) throws IndexOutOfBoundsException { 96 97 int index = getInternalIndex(i); 98 Object result = list[index]; 99 100 list[index] = o; 101 102 return result; 103 } 104 105 public Object removeFirst() throws NoSuchElementException { 106 107 if (elementCount == 0) { 108 throw new NoSuchElementException (); 109 } 110 111 Object o = list[firstindex]; 112 113 list[firstindex] = null; 114 115 firstindex++; 116 elementCount--; 117 118 if (elementCount == 0) { 119 firstindex = endindex = 0; 120 } else if (firstindex == list.length) { 121 firstindex = 0; 122 } 123 124 return o; 125 } 126 127 public Object removeLast() throws NoSuchElementException { 128 129 if (elementCount == 0) { 130 throw new NoSuchElementException (); 131 } 132 133 endindex--; 134 135 Object o = list[endindex]; 136 137 list[endindex] = null; 138 139 elementCount--; 140 141 if (elementCount == 0) { 142 firstindex = endindex = 0; 143 } else if (endindex == 0) { 144 endindex = list.length; 145 } 146 147 return o; 148 } 149 150 159 public boolean add(Object o) { 160 161 resetCapacity(); 162 163 if (endindex == list.length) { 164 endindex = 0; 165 } 166 167 list[endindex] = o; 168 169 elementCount++; 170 endindex++; 171 172 return true; 173 } 174 175 public boolean addLast(Object o) { 176 return add(o); 177 } 178 179 public boolean addFirst(Object o) { 180 181 resetCapacity(); 182 183 firstindex--; 184 185 if (firstindex < 0) { 186 firstindex = list.length - 1; 187 188 if (endindex == 0) { 189 endindex = list.length; 190 } 191 } 192 193 list[firstindex] = o; 194 195 elementCount++; 196 197 return true; 198 } 199 200 public void clear() { 201 202 firstindex = endindex = elementCount = 0; 203 204 for (int i = 0; i < list.length; i++) { 205 list[i] = null; 206 } 207 } 208 209 public Object remove(int index) { 210 211 int target = getInternalIndex(index); 212 Object value = list[target]; 213 214 if (target >= firstindex) { 215 System.arraycopy(list, firstindex, list, firstindex + 1, 216 target - firstindex); 217 218 list[firstindex] = null; 219 220 firstindex++; 221 222 if (firstindex == list.length) { 223 firstindex = 0; 224 } 225 } else { 226 System.arraycopy(list, target + 1, list, target, 227 endindex - target - 1); 228 229 list[endindex] = null; 230 231 endindex--; 232 233 if (endindex == 0) { 234 endindex = list.length; 235 } 236 } 237 238 if (elementCount == 0) { 239 firstindex = endindex = 0; 240 } 241 242 return value; 243 } 244 245 private int getInternalIndex(int i) throws IndexOutOfBoundsException { 246 247 if (i < 0 || i >= elementCount) { 248 throw new IndexOutOfBoundsException (); 249 } 250 251 int index = firstindex + i; 252 253 if (index >= list.length) { 254 index -= list.length; 255 } 256 257 return index; 258 } 259 260 private void resetCapacity() { 261 262 if (elementCount < list.length) { 263 return; 264 } 265 266 Object [] newList = new Object [list.length * 2]; 268 269 for (int i = 0; i < list.length; i++) { 270 newList[i] = list[i]; 271 } 272 273 list = newList; 274 newList = null; 275 276 if (endindex <= firstindex) { 277 int tail = firstindex + elementCount - endindex; 278 279 for (int i = 0; i < endindex; i++) { 280 list[tail + i] = list[i]; 281 list[i] = null; 282 } 283 284 endindex = firstindex + elementCount; 285 } 286 } 287 340 } 341 | Popular Tags |