1 // ========================================================================
2 // Copyright 1999-2005 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
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 /** Singleton List.
23 * This simple efficient implementation of a List with a single
24 * element is provided for JDK 1.2 JVMs, which do not provide
25 * the Collections.singletonList method.
26 *
27 * @author Greg Wilkins (gregw)
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 }