KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > SingletonList


1 // ========================================================================
2
// $Id: SingletonList.java,v 1.3 2004/05/09 20:33:04 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17 import java.util.AbstractList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.ListIterator JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21
22 /* ------------------------------------------------------------ */
23 /** Singleton List.
24  * This simple efficient implementation of a List with a single
25  * element is provided for JDK 1.2 JVMs, which do not provide
26  * the Collections.singletonList method.
27  *
28  * @version $Revision: 1.3 $
29  * @author Greg Wilkins (gregw)
30  */

31 public class SingletonList extends AbstractList JavaDoc
32 {
33     private Object JavaDoc o;
34     
35     /* ------------------------------------------------------------ */
36     private SingletonList(Object JavaDoc o)
37     {
38         this.o=o;
39     }
40
41     /* ------------------------------------------------------------ */
42     public static SingletonList newSingletonList(Object JavaDoc o)
43     {
44         return new SingletonList(o);
45     }
46
47     /* ------------------------------------------------------------ */
48     public Object JavaDoc get(int i)
49     {
50         if (i!=0)
51             throw new IndexOutOfBoundsException JavaDoc("index "+i);
52         return o;
53     }
54
55     /* ------------------------------------------------------------ */
56     public int size()
57     {
58         return 1;
59     }
60
61     /* ------------------------------------------------------------ */
62     public ListIterator JavaDoc listIterator()
63     {
64         return new SIterator();
65     }
66     
67     /* ------------------------------------------------------------ */
68     public ListIterator JavaDoc listIterator(int i)
69     {
70         return new SIterator(i);
71     }
72     
73     /* ------------------------------------------------------------ */
74     public Iterator JavaDoc iterator()
75     {
76         return new SIterator();
77     }
78
79
80     /* ------------------------------------------------------------ */
81     private class SIterator implements ListIterator JavaDoc
82     {
83         int i;
84         
85         SIterator(){i=0;}
86         SIterator(int i)
87         {
88             if (i<0||i>1)
89                 throw new IndexOutOfBoundsException JavaDoc("index "+i);
90             this.i=i;
91         }
92         public void add(Object JavaDoc o){throw new UnsupportedOperationException JavaDoc("SingletonList.add()");}
93         public boolean hasNext() {return i==0;}
94         public boolean hasPrevious() {return i==1;}
95         public Object JavaDoc next() {if (i!=0) throw new NoSuchElementException JavaDoc();i++;return o;}
96         public int nextIndex() {return i;}
97         public Object JavaDoc previous() {if (i!=1) throw new NoSuchElementException JavaDoc();i--;return o;}
98         public int previousIndex() {return i-1;}
99         public void remove(){throw new UnsupportedOperationException JavaDoc("SingletonList.remove()");}
100         public void set(Object JavaDoc o){throw new UnsupportedOperationException JavaDoc("SingletonList.add()");}
101     }
102 }
103
Popular Tags