001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.collections.iterators; 018 019 import java.util.ListIterator; 020 021 import org.apache.commons.collections.Unmodifiable; 022 023 /** 024 * Decorates a list iterator such that it cannot be modified. 025 * 026 * @since Commons Collections 3.0 027 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 028 * 029 * @author Stephen Colebourne 030 */ 031 public final class UnmodifiableListIterator implements ListIterator, Unmodifiable { 032 033 /** The iterator being decorated */ 034 private ListIterator iterator; 035 036 //----------------------------------------------------------------------- 037 /** 038 * Decorates the specified iterator such that it cannot be modified. 039 * 040 * @param iterator the iterator to decorate 041 * @throws IllegalArgumentException if the iterator is null 042 */ 043 public static ListIterator decorate(ListIterator iterator) { 044 if (iterator == null) { 045 throw new IllegalArgumentException("ListIterator must not be null"); 046 } 047 if (iterator instanceof Unmodifiable) { 048 return iterator; 049 } 050 return new UnmodifiableListIterator(iterator); 051 } 052 053 //----------------------------------------------------------------------- 054 /** 055 * Constructor. 056 * 057 * @param iterator the iterator to decorate 058 */ 059 private UnmodifiableListIterator(ListIterator iterator) { 060 super(); 061 this.iterator = iterator; 062 } 063 064 //----------------------------------------------------------------------- 065 public boolean hasNext() { 066 return iterator.hasNext(); 067 } 068 069 public Object next() { 070 return iterator.next(); 071 } 072 073 public int nextIndex() { 074 return iterator.nextIndex(); 075 } 076 077 public boolean hasPrevious() { 078 return iterator.hasPrevious(); 079 } 080 081 public Object previous() { 082 return iterator.previous(); 083 } 084 085 public int previousIndex() { 086 return iterator.previousIndex(); 087 } 088 089 public void remove() { 090 throw new UnsupportedOperationException("remove() is not supported"); 091 } 092 093 public void set(Object obj) { 094 throw new UnsupportedOperationException("set() is not supported"); 095 } 096 097 public void add(Object obj) { 098 throw new UnsupportedOperationException("add() is not supported"); 099 } 100 101 }