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 @Override
46 public Object get(int i)
47 {
48 if (i!=0)
49 throw new IndexOutOfBoundsException("index "+i);
50 return o;
51 }
52
53
54 @Override
55 public int size()
56 {
57 return 1;
58 }
59
60
61 @Override
62 public ListIterator listIterator()
63 {
64 return new SIterator();
65 }
66
67
68 @Override
69 public ListIterator listIterator(int i)
70 {
71 return new SIterator(i);
72 }
73
74
75 @Override
76 public Iterator iterator()
77 {
78 return new SIterator();
79 }
80
81
82
83 private class SIterator implements ListIterator
84 {
85 int i;
86
87 SIterator(){i=0;}
88 SIterator(int i)
89 {
90 if (i<0||i>1)
91 throw new IndexOutOfBoundsException("index "+i);
92 this.i=i;
93 }
94 public void add(Object o){throw new UnsupportedOperationException("SingletonList.add()");}
95 public boolean hasNext() {return i==0;}
96 public boolean hasPrevious() {return i==1;}
97 public Object next() {if (i!=0) throw new NoSuchElementException();i++;return o;}
98 public int nextIndex() {return i;}
99 public Object previous() {if (i!=1) throw new NoSuchElementException();i--;return o;}
100 public int previousIndex() {return i-1;}
101 public void remove(){throw new UnsupportedOperationException("SingletonList.remove()");}
102 public void set(Object o){throw new UnsupportedOperationException("SingletonList.add()");}
103 }
104 }