1 // ========================================================================
2 // Copyright (c) 1999-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
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 /** Singleton List.
22 * This simple efficient implementation of a List with a single
23 * element is provided for JDK 1.2 JVMs, which do not provide
24 * the Collections.singletonList method.
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 }