1 /** 2 * This file Copyright (c) 2011 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.jcr.iterator; 35 36 import java.util.Arrays; 37 import java.util.List; 38 39 import javax.jcr.Node; 40 import javax.jcr.NodeIterator; 41 42 import org.apache.commons.collections.iterators.IteratorChain; 43 44 45 /** 46 * An implementation of {@link NodeIterator} which will chain several iterators making them behave like one. 47 * 48 * @version $Id$ 49 */ 50 public class ChainedNodeIterator implements NodeIterator { 51 52 private final IteratorChain iterators = new IteratorChain(); 53 private long position = 0; 54 private long size = 0; 55 56 public ChainedNodeIterator(List<NodeIterator> iterators) { 57 for(NodeIterator it : iterators) { 58 this.iterators.addIterator(it); 59 60 // If any of the iterators returns -1 the we must also return -1 61 long s = it.getSize(); 62 if (size != -1) { 63 if (s == -1) { 64 size = -1; 65 } else { 66 size += s; 67 } 68 } 69 } 70 } 71 72 public ChainedNodeIterator(NodeIterator... iterators) { 73 this(Arrays.asList(iterators)); 74 } 75 76 @Override 77 public void skip(long skipNum) { 78 while (skipNum > 0) { 79 next(); 80 skipNum--; 81 } 82 } 83 84 @Override 85 public long getSize() { 86 return size; 87 } 88 89 @Override 90 public long getPosition() { 91 return position; 92 } 93 94 @Override 95 public boolean hasNext() { 96 return iterators.hasNext(); 97 } 98 99 @Override 100 public Object next() { 101 return nextNode(); 102 } 103 104 @Override 105 public void remove() { 106 iterators.remove(); 107 } 108 109 @Override 110 public Node nextNode() { 111 Node node = (Node)iterators.next(); 112 position++; 113 return node; 114 } 115 }