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