001// Copyright 2007, 2008 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.grid; 016 017import java.util.List; 018 019/** 020 * Defines how a {@link org.apache.tapestry5.corelib.components.Grid} component (and its sub-components) gain access to 021 * the row data that is displayed on the page. In many cases, this is just a wrapper around a simple List, but the 022 * abstractions exist to support access to a large data set that is accessible in sections. 023 */ 024public interface GridDataSource 025{ 026 027 /** 028 * Return whether the data source is empty, i.e. does not have any rows available. 029 */ 030 default public boolean isEmpty() 031 { 032 return getAvailableRows(1) == 0; 033 } 034 035 /** 036 * Return the number of rows available in the data source with an upper limit. 037 * If determining the total number of rows is expensive, this method should be overridden to provide a more 038 * efficient implementation. 039 * Please note that the default Grid pager will still determine the total number of rows, so for this to have 040 * an effect, a custom pager should be used. 041 * 042 * @param limit the upper limit 043 * @return the number of rows or {@code limit}, whichever is lower 044 */ 045 default public int getAvailableRows(final int limit) 046 { 047 int availableRows = getAvailableRows(); 048 if (availableRows >= limit) 049 { 050 return limit; 051 } 052 return availableRows; 053 } 054 055 /** 056 * Returns the number of rows available in the data source. 057 */ 058 int getAvailableRows(); 059 060 /** 061 * Invoked to allow the source to prepare to present values. This gives the source a chance to pre-fetch data (when 062 * appropriate) and informs the source of the desired sort order. Sorting comes first, then extraction by range. 063 * 064 * @param startIndex the starting index to be retrieved 065 * @param endIndex the ending index to be retrieved 066 * @param sortConstraints identify how data is to be sorted 067 */ 068 void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints); 069 070 /** 071 * Returns the row value at the provided index. This method will be invoked in sequential order. In rare instances, 072 * {@link #getAvailableRows()} may return a different number of rows than are actually available (i.e., the database 073 * was changed between calls to {@link #getAvailableRows()} and the call to {@link #prepare(int, int, 074 * java.util.List)}). In that case, this method should return null for any out-of-range indexes. 075 */ 076 Object getRowValue(int index); 077 078 /** 079 * Returns the type of value in the rows, or null if not known. This value is used to create a default {@link 080 * org.apache.tapestry5.beanmodel.BeanModel} when no such model is explicitly provided. 081 * 082 * @return the row type, or null 083 */ 084 Class getRowType(); 085}