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