View Javadoc

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      @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 }