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.wicket.markup.repeater.util; 018 019import java.util.Iterator; 020 021import org.apache.wicket.model.IModel; 022 023 024/** 025 * Iterator over an array. Implementation must provide {@link ArrayIteratorAdapter#model(Object) } 026 * method to wrap each item in a model before it is returned through 027 * {@link ArrayIteratorAdapter#next() } method. 028 * 029 * @author Igor Vaynberg (ivaynberg) 030 * @param <T> 031 * type of array element 032 * 033 */ 034public abstract class ArrayIteratorAdapter<T> implements Iterator<IModel<T>> 035{ 036 private final T[] array; 037 private int pos = 0; 038 039 /** 040 * Constructor 041 * 042 * @param array 043 */ 044 public ArrayIteratorAdapter(T[] array) 045 { 046 this.array = array; 047 } 048 049 /** 050 * @see java.util.Iterator#remove() 051 */ 052 @Override 053 public void remove() 054 { 055 throw new UnsupportedOperationException("remove() is not allowed"); 056 } 057 058 /** 059 * @see java.util.Iterator#hasNext() 060 */ 061 @Override 062 public boolean hasNext() 063 { 064 return pos < array.length; 065 } 066 067 /** 068 * @see java.util.Iterator#next() 069 */ 070 @Override 071 public IModel<T> next() 072 { 073 return model(array[pos++]); 074 } 075 076 /** 077 * Resets the iterator position back to the beginning of the array 078 */ 079 public void reset() 080 { 081 pos = 0; 082 } 083 084 /** 085 * This method is used to wrap the provided object with an implementation of IModel. The 086 * provided object is guaranteed to be returned from the delegate iterator. 087 * 088 * @param object 089 * object to be wrapped 090 * @return IModel wrapper for the object 091 */ 092 abstract protected IModel<T> model(T object); 093 094 095}