1 16 package org.mortbay.util; 17 import java.util.AbstractList ; 18 import java.util.Iterator ; 19 import java.util.ListIterator ; 20 import java.util.NoSuchElementException ; 21 22 23 31 public class SingletonList extends AbstractList 32 { 33 private Object o; 34 35 36 private SingletonList(Object o) 37 { 38 this.o=o; 39 } 40 41 42 public static SingletonList newSingletonList(Object o) 43 { 44 return new SingletonList(o); 45 } 46 47 48 public Object get(int i) 49 { 50 if (i!=0) 51 throw new IndexOutOfBoundsException ("index "+i); 52 return o; 53 } 54 55 56 public int size() 57 { 58 return 1; 59 } 60 61 62 public ListIterator listIterator() 63 { 64 return new SIterator(); 65 } 66 67 68 public ListIterator listIterator(int i) 69 { 70 return new SIterator(i); 71 } 72 73 74 public Iterator iterator() 75 { 76 return new SIterator(); 77 } 78 79 80 81 private class SIterator implements ListIterator 82 { 83 int i; 84 85 SIterator(){i=0;} 86 SIterator(int i) 87 { 88 if (i<0||i>1) 89 throw new IndexOutOfBoundsException ("index "+i); 90 this.i=i; 91 } 92 public void add(Object o){throw new UnsupportedOperationException ("SingletonList.add()");} 93 public boolean hasNext() {return i==0;} 94 public boolean hasPrevious() {return i==1;} 95 public Object next() {if (i!=0) throw new NoSuchElementException ();i++;return o;} 96 public int nextIndex() {return i;} 97 public Object previous() {if (i!=1) throw new NoSuchElementException ();i--;return o;} 98 public int previousIndex() {return i-1;} 99 public void remove(){throw new UnsupportedOperationException ("SingletonList.remove()");} 100 public void set(Object o){throw new UnsupportedOperationException ("SingletonList.add()");} 101 } 102 } 103 | Popular Tags |