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.Iterator;
20:
21: import org.apache.wicket.WicketRuntimeException;
22: import org.apache.wicket.model.IModel;
23:
24: /**
25: * Implementation of <code>IItemReuseStrategy</code> that returns new items
26: * every time.
27: *
28: * @see org.apache.wicket.extensions.markup.html.repeater.refreshing.IItemReuseStrategy
29: *
30: * @author Igor Vaynberg (ivaynberg)
31: *
32: */
33: public class DefaultItemReuseStrategy implements IItemReuseStrategy {
34: private static final long serialVersionUID = 1L;
35:
36: private static final IItemReuseStrategy instance = new DefaultItemReuseStrategy();
37:
38: /**
39: * @return static instance of this strategy
40: */
41: public static IItemReuseStrategy getInstance() {
42: return instance;
43: }
44:
45: /**
46: * @see org.apache.wicket.extensions.markup.html.repeater.refreshing.IItemReuseStrategy#getItems(org.apache.wicket.extensions.markup.html.repeater.refreshing.IItemFactory,
47: * java.util.Iterator, java.util.Iterator)
48: */
49: public Iterator getItems(final IItemFactory factory,
50: final Iterator newModels, final Iterator existingItems) {
51: return new Iterator() {
52: private int index = 0;
53:
54: public void remove() {
55: throw new UnsupportedOperationException();
56: }
57:
58: public boolean hasNext() {
59: return newModels.hasNext();
60: }
61:
62: public Object next() {
63: Object next = newModels.next();
64: if (next != null && !(next instanceof IModel)) {
65: throw new WicketRuntimeException(
66: "Expecting an instance of "
67: + IModel.class.getName() + ", got "
68: + next.getClass().getName());
69: }
70: final IModel model = (IModel) next;
71:
72: Item item = factory.newItem(index, model);
73: index++;
74:
75: return item;
76: }
77:
78: };
79: }
80:
81: }
|