01: /*
02: * $Id: ReuseIfModelsEqualStrategy.java 461217 2006-06-29 19:15:50Z ivaynberg $
03: * $Revision: 461217 $
04: * $Date: 2006-06-29 21:15:50 +0200 (Thu, 29 Jun 2006) $
05: *
06: * ====================================================================
07: * Licensed under the Apache License, Version 2.0 (the "License");
08: * you may not use this file except in compliance with the License.
09: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19: package wicket.extensions.markup.html.repeater.refreshing;
20:
21: import java.util.HashMap;
22: import java.util.Iterator;
23: import java.util.Map;
24:
25: import wicket.model.IModel;
26:
27: /**
28: * Reuse strategy that will reuse an old item if its model is equal to a model
29: * inside the newModels iterator. Useful when state needs to be kept across
30: * requests for as long as the item is visible within the view.
31: * <p>
32: * Notice that the <u>model</u> and not the <u>model object</u> needs to
33: * implement the equals method. Most of the time it is a good idea to forward
34: * the equals call to the object, however if a detachable model is used it is
35: * often enough to compare object ids models point to ( this saves the model
36: * from loading the object).
37: *
38: * @author Igor Vaynberg (ivaynberg)
39: *
40: */
41: public class ReuseIfModelsEqualStrategy implements IItemReuseStrategy {
42: private static final long serialVersionUID = 1L;
43:
44: private static IItemReuseStrategy instance = new ReuseIfModelsEqualStrategy();
45:
46: /**
47: * @return static instance
48: */
49: public static IItemReuseStrategy getInstance() {
50: return instance;
51: }
52:
53: /**
54: * @see wicket.extensions.markup.html.repeater.refreshing.IItemReuseStrategy#getItems(wicket.extensions.markup.html.repeater.refreshing.IItemFactory,
55: * java.util.Iterator, java.util.Iterator)
56: */
57: public Iterator getItems(final IItemFactory factory,
58: final Iterator newModels, Iterator existingItems) {
59: final Map modelToItem = new HashMap();
60: while (existingItems.hasNext()) {
61: final Item item = (Item) existingItems.next();
62: modelToItem.put(item.getModel(), item);
63: }
64:
65: return new Iterator() {
66: private int index = 0;
67:
68: public boolean hasNext() {
69: return newModels.hasNext();
70: }
71:
72: public Object next() {
73: final IModel model = (IModel) newModels.next();
74: final Item oldItem = (Item) modelToItem.get(model);
75:
76: final Item item;
77: if (oldItem == null) {
78: item = factory.newItem(index, model);
79: } else {
80: oldItem.setIndex(index);
81: item = oldItem;
82: }
83: index++;
84:
85: return item;
86: }
87:
88: public void remove() {
89: throw new UnsupportedOperationException();
90: }
91:
92: };
93: }
94:
95: }
|