001: // ========================================================================
002: // Copyright 1999-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import java.util.AbstractList;
018: import java.util.Iterator;
019: import java.util.ListIterator;
020: import java.util.NoSuchElementException;
021:
022: /* ------------------------------------------------------------ */
023: /** Singleton List.
024: * This simple efficient implementation of a List with a single
025: * element is provided for JDK 1.2 JVMs, which do not provide
026: * the Collections.singletonList method.
027: *
028: * @author Greg Wilkins (gregw)
029: */
030: public class SingletonList extends AbstractList {
031: private Object o;
032:
033: /* ------------------------------------------------------------ */
034: private SingletonList(Object o) {
035: this .o = o;
036: }
037:
038: /* ------------------------------------------------------------ */
039: public static SingletonList newSingletonList(Object o) {
040: return new SingletonList(o);
041: }
042:
043: /* ------------------------------------------------------------ */
044: public Object get(int i) {
045: if (i != 0)
046: throw new IndexOutOfBoundsException("index " + i);
047: return o;
048: }
049:
050: /* ------------------------------------------------------------ */
051: public int size() {
052: return 1;
053: }
054:
055: /* ------------------------------------------------------------ */
056: public ListIterator listIterator() {
057: return new SIterator();
058: }
059:
060: /* ------------------------------------------------------------ */
061: public ListIterator listIterator(int i) {
062: return new SIterator(i);
063: }
064:
065: /* ------------------------------------------------------------ */
066: public Iterator iterator() {
067: return new SIterator();
068: }
069:
070: /* ------------------------------------------------------------ */
071: private class SIterator implements ListIterator {
072: int i;
073:
074: SIterator() {
075: i = 0;
076: }
077:
078: SIterator(int i) {
079: if (i < 0 || i > 1)
080: throw new IndexOutOfBoundsException("index " + i);
081: this .i = i;
082: }
083:
084: public void add(Object o) {
085: throw new UnsupportedOperationException(
086: "SingletonList.add()");
087: }
088:
089: public boolean hasNext() {
090: return i == 0;
091: }
092:
093: public boolean hasPrevious() {
094: return i == 1;
095: }
096:
097: public Object next() {
098: if (i != 0)
099: throw new NoSuchElementException();
100: i++;
101: return o;
102: }
103:
104: public int nextIndex() {
105: return i;
106: }
107:
108: public Object previous() {
109: if (i != 1)
110: throw new NoSuchElementException();
111: i--;
112: return o;
113: }
114:
115: public int previousIndex() {
116: return i - 1;
117: }
118:
119: public void remove() {
120: throw new UnsupportedOperationException(
121: "SingletonList.remove()");
122: }
123:
124: public void set(Object o) {
125: throw new UnsupportedOperationException(
126: "SingletonList.add()");
127: }
128: }
129: }
|