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