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; 020import java.util.Map; 021 022import org.apache.wicket.model.IModel; 023import org.apache.wicket.util.lang.Generics; 024 025 026/** 027 * Reuse strategy that will reuse an old item if its model is equal to a model inside the newModels 028 * iterator. Useful when state needs to be kept across requests for as long as the item is visible 029 * within the view. 030 * <p> 031 * Notice that the <u>model</u> and not the <u>model object</u> needs to implement the 032 * {@link #equals(Object)} and {@link #hashCode()} methods. Most of the time it is a good idea to 033 * forward the calls to the object, however if a detachable model is used it is often enough to 034 * compare an identifier for the object the models are pointing to ( this saves the model from loading the 035 * object). 036 * 037 * @author Igor Vaynberg (ivaynberg) 038 * 039 */ 040public class ReuseIfModelsEqualStrategy implements IItemReuseStrategy 041{ 042 private static final long serialVersionUID = 1L; 043 044 private static IItemReuseStrategy instance = new ReuseIfModelsEqualStrategy(); 045 046 /** 047 * @return static instance 048 */ 049 public static IItemReuseStrategy getInstance() 050 { 051 return instance; 052 } 053 054 /** 055 * @see org.apache.wicket.markup.repeater.IItemReuseStrategy#getItems(org.apache.wicket.markup.repeater.IItemFactory, 056 * java.util.Iterator, java.util.Iterator) 057 */ 058 @Override 059 public <T> Iterator<Item<T>> getItems(final IItemFactory<T> factory, 060 final Iterator<IModel<T>> newModels, Iterator<Item<T>> existingItems) 061 { 062 final Map<IModel<T>, Item<T>> modelToItem = Generics.newHashMap(); 063 while (existingItems.hasNext()) 064 { 065 final Item<T> item = existingItems.next(); 066 modelToItem.put(item.getModel(), item); 067 } 068 069 return new Iterator<Item<T>>() 070 { 071 private int index = 0; 072 073 @Override 074 public boolean hasNext() 075 { 076 return newModels.hasNext(); 077 } 078 079 @Override 080 public Item<T> next() 081 { 082 final IModel<T> model = newModels.next(); 083 final Item<T> oldItem = modelToItem.get(model); 084 085 final Item<T> item; 086 if (oldItem == null) 087 { 088 item = factory.newItem(index, model); 089 } 090 else 091 { 092 oldItem.setIndex(index); 093 item = oldItem; 094 } 095 index++; 096 097 return item; 098 } 099 100 @Override 101 public void remove() 102 { 103 throw new UnsupportedOperationException(); 104 } 105 106 }; 107 } 108 109}