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 */ 017package org.apache.commons.collections4.iterators; 018 019import java.util.ListIterator; 020 021import org.apache.commons.collections4.Unmodifiable; 022 023/** 024 * Decorates a list iterator such that it cannot be modified. 025 * <p> 026 * Attempts to modify it will result in an UnsupportedOperationException. 027 * 028 * @since 3.0 029 * @version $Id: UnmodifiableListIterator.html 972421 2015-11-14 20:00:04Z tn $ 030 */ 031public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable { 032 033 /** The iterator being decorated */ 034 private final ListIterator<? extends E> iterator; 035 036 //----------------------------------------------------------------------- 037 /** 038 * Decorates the specified iterator such that it cannot be modified. 039 * 040 * @param <E> the element type 041 * @param iterator the iterator to decorate 042 * @return a new unmodifiable list iterator 043 * @throws IllegalArgumentException if the iterator is null 044 */ 045 public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) { 046 if (iterator == null) { 047 throw new IllegalArgumentException("ListIterator must not be null"); 048 } 049 if (iterator instanceof Unmodifiable) { 050 @SuppressWarnings("unchecked") // safe to upcast 051 final ListIterator<E> tmpIterator = (ListIterator<E>) iterator; 052 return tmpIterator; 053 } 054 return new UnmodifiableListIterator<E>(iterator); 055 } 056 057 //----------------------------------------------------------------------- 058 /** 059 * Constructor. 060 * 061 * @param iterator the iterator to decorate 062 */ 063 private UnmodifiableListIterator(final ListIterator<? extends E> iterator) { 064 super(); 065 this.iterator = iterator; 066 } 067 068 //----------------------------------------------------------------------- 069 public boolean hasNext() { 070 return iterator.hasNext(); 071 } 072 073 public E next() { 074 return iterator.next(); 075 } 076 077 public int nextIndex() { 078 return iterator.nextIndex(); 079 } 080 081 public boolean hasPrevious() { 082 return iterator.hasPrevious(); 083 } 084 085 public E previous() { 086 return iterator.previous(); 087 } 088 089 public int previousIndex() { 090 return iterator.previousIndex(); 091 } 092 093 public void remove() { 094 throw new UnsupportedOperationException("remove() is not supported"); 095 } 096 097 public void set(final E obj) { 098 throw new UnsupportedOperationException("set() is not supported"); 099 } 100 101 public void add(final E obj) { 102 throw new UnsupportedOperationException("add() is not supported"); 103 } 104 105}