1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.mozilla.org/MPL/MPL-1.1.html 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import org.as2lib.core.BasicClass; 18 import org.as2lib.data.holder.Iterator; 19 import org.as2lib.env.except.IllegalArgumentException; 20 import org.as2lib.env.except.UnsupportedOperationException; 21 22 /** 23 * {@code ProtectedIterator} is used to iterate over any data holder 24 * without being able to remove elements. 25 * 26 * <p>This class acts as a wrapper for any class that implements the 27 * {@code Iterator} interface and wants to be protected. 28 * 29 * @author Simon Wacker 30 * @author Michael Herrmann 31 */ 32 class org.as2lib.data.holder.ProtectedIterator extends BasicClass implements Iterator { 33 34 /** Holds the iterator this protected iterator delegates to. */ 35 private var iterator:Iterator; 36 37 /** 38 * Constructs a new {@code ProtectedIterator} instance. 39 * 40 * <p>This iterator forwards all functionality to the wrapped passed-in 41 * {@code iterator}, except the removal of the current element. 42 * 43 * @param iterator the iterator to protect 44 * @throws IllegalArgumentException if the passed-in {@code iterator} 45 * is {@code null} or {@code undefined} 46 */ 47 public function ProtectedIterator(iterator:Iterator) { 48 if (!iterator) throw new IllegalArgumentException("Argument 'iterator' [" + iterator + "] to protect is not allowed to be 'null' or 'undefined'.", this, arguments); 49 this.iterator = iterator; 50 } 51 52 /** 53 * Returns whether there are more elements to iterate over. 54 * 55 * <p>For any special functionality that may be performed refer to the 56 * wrapped iterator that has been passed-in on construction. This method 57 * simply delegates to the wrapped iterator. 58 * 59 * @return {@code true} if there is a next element else {@code false} 60 */ 61 public function hasNext(Void):Boolean { 62 return iterator.hasNext(); 63 } 64 65 /** 66 * Returns the next element. 67 * 68 * <p>For any special functionality that may be performed refer to the 69 * wrapped iterator that has been passed-in on construction. This method 70 * simply delegates to the wrapped iterator. 71 * 72 * @return the next element 73 * @throws org.as2lib.data.holder.NoSuchElementException if there is 74 * no next element 75 */ 76 public function next(Void) { 77 return iterator.next(); 78 } 79 80 /** 81 * This method always throws an {@code UnsupportedOperationException} 82 * because this method is not supported by this iterator and has the 83 * duty to not let the removal of elements happen. 84 * 85 * @throws UnsupportedOperationException 86 */ 87 public function remove(Void):Void { 88 throw new UnsupportedOperationException("This Iterator does not support the remove() method.", this, arguments); 89 } 90 91 }