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;
018
019import java.util.Iterator;
020
021import org.apache.wicket.model.IModel;
022
023
024/**
025 * Implementation of <code>IItemReuseStrategy</code> that returns new items every time.
026 * 
027 * @see org.apache.wicket.markup.repeater.IItemReuseStrategy
028 * 
029 * @author Igor Vaynberg (ivaynberg)
030 * 
031 */
032public class DefaultItemReuseStrategy implements IItemReuseStrategy
033{
034        private static final long serialVersionUID = 1L;
035
036        private static final IItemReuseStrategy instance = new DefaultItemReuseStrategy();
037
038        /**
039         * @return static instance of this strategy
040         */
041        public static IItemReuseStrategy getInstance()
042        {
043                return instance;
044        }
045
046        /**
047         * 
048         * @see org.apache.wicket.markup.repeater.IItemReuseStrategy#getItems(org.apache.wicket.markup.repeater.IItemFactory,
049         *      java.util.Iterator, java.util.Iterator)
050         */
051        @Override
052        public <T> Iterator<Item<T>> getItems(final IItemFactory<T> factory,
053                final Iterator<IModel<T>> newModels, Iterator<Item<T>> existingItems)
054        {
055                return new Iterator<Item<T>>()
056                {
057                        private int index = 0;
058
059                        @Override
060                        public void remove()
061                        {
062                                throw new UnsupportedOperationException();
063                        }
064
065                        @Override
066                        public boolean hasNext()
067                        {
068                                return newModels.hasNext();
069                        }
070
071                        @Override
072                        public Item<T> next()
073                        {
074                                IModel<T> model = newModels.next();
075                                Item<T> item = factory.newItem(index, model);
076                                index++;
077                                return item;
078                        }
079
080                };
081        }
082
083}