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.markup.repeater.RefreshingView;
022import org.apache.wicket.model.IModel;
023import org.apache.wicket.util.lang.Args;
024
025
026/**
027 * Iterator adapter that wraps adaptee's elements with a model. Convenient when implementing
028 * {@link RefreshingView}.
029 * 
030 * @author Igor Vaynberg (ivaynberg)
031 * @param <T>
032 * 
033 */
034public abstract class ModelIteratorAdapter<T> implements Iterator<IModel<T>>
035{
036        private final Iterator<T> delegate;
037
038        /**
039         * Constructor
040         * 
041         * @param iterable
042         *            iterable whose iterator will be wrapped
043         */
044        public ModelIteratorAdapter(Iterable<T> iterable)
045        {
046                Args.notNull(iterable, "iterable");
047                this.delegate = iterable.iterator();
048        }
049
050
051        /**
052         * Constructor
053         * 
054         * @param delegate
055         *            iterator that will be wrapped
056         */
057        public ModelIteratorAdapter(Iterator<T> delegate)
058        {
059                this.delegate = delegate;
060        }
061
062        /**
063         * @see java.util.Iterator#hasNext()
064         */
065        @Override
066        public boolean hasNext()
067        {
068                return delegate.hasNext();
069        }
070
071        /**
072         * @see java.util.Iterator#next()
073         */
074        @Override
075        public IModel<T> next()
076        {
077                return model(delegate.next());
078        }
079
080        /**
081         * @see java.util.Iterator#remove()
082         */
083        @Override
084        public void remove()
085        {
086                delegate.remove();
087        }
088
089        /**
090         * This method is used to wrap the provided object with an implementation of IModel. The
091         * provided object is guaranteed to be returned from the delegate iterator.
092         * 
093         * @param object
094         *            object to be wrapped
095         * @return IModel wrapper for the object
096         */
097        abstract protected IModel<T> model(T object);
098}