001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (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.cmis; 029 030import java.math.BigInteger; 031import java.util.Iterator; 032import java.util.List; 033 034/** 035 * Helper class to ease implementation of CMIS service methods which support paging.<p> 036 * 037 * This class works as an iterator for a given list, and limits the number of iterations based on skip/max parameters 038 * which are usually passed to the service methods.<p> 039 * 040 * @param <A> the content type of the list 041 */ 042public class CmsObjectListLimiter<A> implements Iterable<A>, Iterator<A> { 043 044 /** The list for which this object acts as an iterator. */ 045 private List<A> m_baseList; 046 047 /** The maximum number of objects which can still be returned. */ 048 private int m_max; 049 050 /** The index of the next object to return. */ 051 private int m_next; 052 053 /** 054 * Creates a new instance.<p> 055 * 056 * @param baseList the list over which we want to iterate 057 * @param maxItems the maximum number of items 058 * @param skipCount the number of items to skip 059 */ 060 public CmsObjectListLimiter(List<A> baseList, BigInteger maxItems, BigInteger skipCount) { 061 062 // skip and max 063 m_baseList = baseList; 064 m_next = (skipCount == null ? 0 : skipCount.intValue()); 065 if (m_next < 0) { 066 m_next = 0; 067 } 068 069 m_max = (maxItems == null ? Integer.MAX_VALUE : maxItems.intValue()); 070 if (m_max < 0) { 071 m_max = Integer.MAX_VALUE; 072 } 073 074 } 075 076 /** 077 * Checks if there are more items left in the base list which were not returned.<p> 078 * 079 * @return true if there are more items left in the base list which were not returned 080 */ 081 public boolean hasMore() { 082 083 return m_next < m_baseList.size(); 084 } 085 086 /** 087 * @see java.util.Iterator#hasNext() 088 */ 089 public boolean hasNext() { 090 091 return (m_next < m_baseList.size()) && (m_max > 0); 092 } 093 094 /** 095 * @see java.lang.Iterable#iterator() 096 */ 097 public Iterator<A> iterator() { 098 099 return this; 100 } 101 102 /** 103 * @see java.util.Iterator#next() 104 */ 105 public A next() { 106 107 A result = m_baseList.get(m_next); 108 m_next += 1; 109 m_max -= 1; 110 return result; 111 } 112 113 /** 114 * @see java.util.Iterator#remove() 115 */ 116 public void remove() { 117 118 throw new UnsupportedOperationException(); 119 } 120}