001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.util; 029 030import java.util.ArrayList; 031import java.util.List; 032 033/** 034 * A utility class for partitioning a list of items (each of which has a given size) into one or more batches 035 * of consecutive items such that each batch except the last one consists of just enough items to make the total 036 * sum of item sizes in the batch greater or equal than a given parameter.<p> 037 * 038 * @param <T> the type of items in the list to be partitioned 039 * 040 * @since 8.0.0 041 * 042 */ 043public class CmsListSplitter<T extends I_CmsHasSize> { 044 045 /** The minimum size of the batches (except for the last batch). */ 046 private int m_batchSize; 047 048 /** The current position in the list of items. */ 049 private int m_itemIndex; 050 051 /** The list of items to split. */ 052 private List<T> m_items; 053 054 /** 055 * Creates a new instance of a list splitter.<p> 056 * 057 * @param items the list of items to split 058 * @param batchSize the minimum size of the batches (except for the last batch) 059 */ 060 public CmsListSplitter(List<T> items, int batchSize) { 061 062 m_batchSize = batchSize; 063 m_items = items; 064 m_itemIndex = 0; 065 } 066 067 /** 068 * Gets the next batch of items.<p> 069 * 070 * This will fail if there are no more items. 071 * 072 * @return the next batch of items 073 */ 074 public List<T> getMore() { 075 076 assert m_itemIndex < m_items.size(); 077 List<T> result = new ArrayList<T>(); 078 int totalSize = 0; 079 while ((m_itemIndex < m_items.size()) && (totalSize < m_batchSize)) { 080 T item = m_items.get(m_itemIndex); 081 result.add(item); 082 m_itemIndex += 1; 083 totalSize += item.getSize(); 084 } 085 return result; 086 } 087 088 /** 089 * Returns true if there are more items left.<p> 090 * 091 * @return true if there are more items left 092 */ 093 public boolean hasMore() { 094 095 return m_itemIndex < m_items.size(); 096 } 097}