1 21 package au.id.jericho.lib.html.nodoc; 22 23 import au.id.jericho.lib.html.*; 24 import java.util.*; 25 26 35 public abstract class SequentialListSegment extends Segment implements List { 36 public SequentialListSegment(final Source source, final int begin, final int end) { 37 super(source,begin,end); 38 } 39 40 44 public abstract int getCount(); 45 46 56 public abstract ListIterator listIterator(int index); 57 58 68 public Object get(final int index) { 69 final ListIterator li=listIterator(index); 70 try { 71 return(li.next()); 72 } catch(NoSuchElementException ex) { 73 throw(new IndexOutOfBoundsException ("index="+index)); 74 } 75 } 76 77 85 public int size() { 86 return getCount(); 87 } 88 89 93 public boolean isEmpty() { 94 return getCount()==0; 95 } 96 97 103 public boolean contains(final Object o) { 104 return indexOf(o)>=0; 105 } 106 107 111 public Object [] toArray() { 112 final Object [] array=new Object [getCount()]; 113 int x=0; 114 for (final ListIterator li=listIterator(0); li.hasNext();) array[x++]=li.next(); 115 return array; 116 } 117 118 134 public Object [] toArray(Object a[]) { 135 final int count=getCount(); 136 if (a.length<count) a=(Object [])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(),count); 137 int x=0; 138 for (final ListIterator li=listIterator(0); li.hasNext();) a[x++]=li.next(); 139 if (a.length>count) a[count]=null; 140 return a; 141 } 142 143 147 public boolean remove(Object o) { 148 throw new UnsupportedOperationException (); 149 } 150 151 158 public boolean containsAll(final Collection collection) { 159 for (final Iterator i=collection.iterator(); i.hasNext();) 160 if(!contains(i.next())) return false; 161 return true; 162 } 163 164 168 public boolean addAll(Collection collection) { 169 throw new UnsupportedOperationException (); 170 } 171 172 176 public boolean removeAll(Collection collection) { 177 throw new UnsupportedOperationException (); 178 } 179 180 184 public boolean retainAll(Collection collection) { 185 throw new UnsupportedOperationException (); 186 } 187 188 192 public boolean add(Object o) { 193 throw new UnsupportedOperationException (); 194 } 195 196 200 public Object set(int index, Object element) { 201 throw new UnsupportedOperationException (); 202 } 203 204 208 public void add(int index, Object element) { 209 throw new UnsupportedOperationException (); 210 } 211 212 216 public Object remove(int index) { 217 throw new UnsupportedOperationException (); 218 } 219 220 226 public int indexOf(final Object o) { 227 final ListIterator li=listIterator(0); 228 if (o==null) { 229 while (li.hasNext()) if (li.next()==null) return li.previousIndex(); 230 } else { 231 while (li.hasNext()) if (o.equals(li.next())) return li.previousIndex(); 232 } 233 return -1; 234 } 235 236 242 public int lastIndexOf(final Object o) { 243 final ListIterator li=listIterator(getCount()); 244 if (o==null) { 245 while (li.hasPrevious()) if (li.previous()==null) return li.nextIndex(); 246 } else { 247 while (li.hasPrevious()) if (o.equals(li.previous())) return li.nextIndex(); 248 } 249 return -1; 250 } 251 252 256 public void clear() { 257 throw new UnsupportedOperationException (); 258 } 259 260 264 public boolean addAll(int index, Collection collection) { 265 throw new UnsupportedOperationException (); 266 } 267 268 272 public Iterator iterator() { 273 return listIterator(); 274 } 275 276 281 public ListIterator listIterator() { 282 return listIterator(0); 283 } 284 285 298 public List subList(final int fromIndex, final int toIndex) { 299 return (new SubList(this,fromIndex,toIndex)); 300 } 301 302 private static class SubList extends AbstractList { 303 private final List list; 304 private final int offset; 305 private final int size; 306 307 SubList(final List list, final int fromIndex, final int toIndex) { 308 if (fromIndex<0) throw new IndexOutOfBoundsException ("fromIndex="+fromIndex); 309 if (toIndex>list.size()) throw new IndexOutOfBoundsException ("toIndex="+toIndex); 310 if (fromIndex>toIndex) throw new IllegalArgumentException ("fromIndex("+fromIndex+") > toIndex("+toIndex+")"); 311 this.list=list; 312 offset=fromIndex; 313 size=toIndex-fromIndex; 314 } 315 316 public Object get(final int index) { 317 return list.get(getSuperListIndex(index)); 318 } 319 320 public int size() { 321 return size; 322 } 323 324 public Iterator iterator() { 325 return listIterator(); 326 } 327 328 public ListIterator listIterator(final int index) { 329 return new ListIterator() { 330 private final ListIterator i=list.listIterator(getSuperListIndex(index)); 331 public boolean hasNext() { 332 return nextIndex()<size; 333 } 334 public Object next() { 335 if (!hasNext()) throw new NoSuchElementException(); 336 return i.next(); 337 } 338 public boolean hasPrevious() { 339 return previousIndex()>=0; 340 } 341 public Object previous() { 342 if (!hasPrevious()) throw new NoSuchElementException(); 343 return i.previous(); 344 } 345 public int nextIndex() { 346 return i.nextIndex()-offset; 347 } 348 public int previousIndex() { 349 return i.previousIndex()-offset; 350 } 351 public void remove() { 352 throw new UnsupportedOperationException (); 353 } 354 public void set(Object o) { 355 throw new UnsupportedOperationException (); 356 } 357 public void add(Object o) { 358 throw new UnsupportedOperationException (); 359 } 360 }; 361 } 362 363 public List subList(final int fromIndex, final int toIndex) { 364 return new SubList(this,fromIndex,toIndex); 365 } 366 367 private int getSuperListIndex(final int index) { 368 if (index<0 || index>=size) throw new IndexOutOfBoundsException ("index="+index+", size="+size); 369 return index+offset; 370 } 371 } 372 } 373 | Popular Tags |