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.data; 018 019import java.io.Serializable; 020import java.util.Collections; 021import java.util.Iterator; 022import java.util.List; 023 024import org.apache.wicket.model.IModel; 025import org.apache.wicket.model.Model; 026 027 028/** 029 * Allows the use of lists with {@link DataView}. The only requirement is that either list items 030 * must be serializable or model(Object) needs to be overridden to provide the proper model 031 * implementation. 032 * 033 * @author Igor Vaynberg ( ivaynberg ) 034 * @param <T> 035 * 036 */ 037public class ListDataProvider<T extends Serializable> implements IDataProvider<T> 038{ 039 private static final long serialVersionUID = 1L; 040 041 /** reference to the list used as dataprovider for the dataview */ 042 private final List<T> list; 043 044 /** 045 * Constructs an empty provider. Useful for lazy loading together with {@linkplain #getData()} 046 */ 047 public ListDataProvider() 048 { 049 this(Collections.<T> emptyList()); 050 } 051 052 /** 053 * 054 * @param list 055 * the list used as dataprovider for the dataview 056 */ 057 public ListDataProvider(List<T> list) 058 { 059 if (list == null) 060 { 061 throw new IllegalArgumentException("argument [list] cannot be null"); 062 } 063 064 this.list = list; 065 } 066 067 /** 068 * Subclass to lazy load the list 069 * 070 * @return The list 071 */ 072 protected List<T> getData() 073 { 074 return list; 075 } 076 077 @Override 078 public Iterator<T> iterator(final long first, final long count) 079 { 080 List<T> list = getData(); 081 082 long toIndex = first + count; 083 if (toIndex > list.size()) 084 { 085 toIndex = list.size(); 086 } 087 return list.subList((int)first, (int)toIndex).listIterator(); 088 } 089 090 @Override 091 public long size() 092 { 093 return getData().size(); 094 } 095 096 @Override 097 public IModel<T> model(T object) 098 { 099 return new Model<>(object); 100 } 101}