001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.internal;
016
017 import java.util.ArrayList;
018 import java.util.Iterator;
019 import java.util.List;
020
021 /**
022 * Provides an implementation that will only iterate
023 * over the objects within the range provided when prepare() is called.
024 *
025 * @version $Id: PagedSource.java 361 2008-11-25 13:05:14Z homburgs $
026 */
027 public class PagedSource<T> implements Iterable<T>
028 {
029 private List<T> _source = new ArrayList<T>();
030
031 private List<T> _pageSource = new ArrayList<T>();
032
033 private Integer _iterableSize;
034
035 public PagedSource(Iterable<T> source)
036 {
037 for (T aSource : source)
038 _source.add(aSource);
039 }
040
041 /**
042 * @return
043 *
044 * @see java.lang.Iterable#iterator()
045 */
046 public Iterator<T> iterator()
047 {
048 return _pageSource.iterator();
049 }
050
051 public int getTotalRowCount()
052 {
053 if (_iterableSize != null)
054 return _iterableSize;
055
056 _iterableSize = 0;
057
058 Iterator<?> it = _source.iterator();
059 while (it.hasNext())
060 {
061 it.next();
062 _iterableSize++;
063 }
064
065 return _iterableSize;
066 }
067
068 public void prepare(int startIndex, int endIndex)
069 {
070 for (int i = startIndex; i <= endIndex; i++)
071 {
072 _pageSource.add(_source.get(i));
073 }
074 }
075
076 }