View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns;
22  
23  import java.util.Collection;
24  import java.util.Iterator;
25  
26  import org.apache.commons.collections.iterators.EmptyIterator;
27  
28  /***
29   * An iterator that goes over an iterator containing <code>IDashboardReportContainer</code>
30   * objects, and explodes them to expose an iterator over <code>IDashboardReport</code> objects.
31   * Does not implement <code>Iterator</code> because it doesn't support remove, and doesn't return
32   * <code>Object</code>.
33   * 
34   * @author Jeremy Kraybill
35   * @author Jeremy Thomerson
36   * @version $Id$
37   */
38  public class ReportContainerIterator {
39  
40  	/*** The wrapped list iterator, must be all IDashboardReportContainer objects. */
41  	private final Iterator myIterator;
42  
43  	/*** The current iterator over the current IDashboardReportContainer. */
44  	private Iterator currentIterator = EmptyIterator.INSTANCE;
45  
46  	/***
47  	 * Basic constructor.
48  	 * 
49  	 * @param col a collection of <code>IDashboardReportContainer</code> objects.
50  	 */
51  	public ReportContainerIterator(Collection col) {
52  		if (col == null) {
53  			throw new IllegalArgumentException("Wrapped collection must not be null.");
54  		}
55  		myIterator = col.iterator();
56  	}
57  
58  	/***
59  	 * Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.) 
60  	 * 
61  	 * @return true if the iterator has more elements.
62  	 */
63  	public boolean hasNext() {
64  		moveToNextActive();
65  		return currentIterator.hasNext();
66  	}
67  
68  	/***
69  	 * Returns the next <code>IDashboardReport</code> in the iteration.
70  	 *  
71  	 * @return the next element in the iteration. 
72  	 */
73  	public IDashboardReport next() {
74  		moveToNextActive();
75  		return (IDashboardReport) currentIterator.next();
76  	}
77  	
78  	/***
79  	 * Ensures our current iterator has elements to offer, or that we are at the end of the line.
80  	 */
81  	private void moveToNextActive() {
82  		while (!currentIterator.hasNext() && myIterator.hasNext()) {
83  			// move on to next container.
84              Object obj = myIterator.next();
85              currentIterator = ((IDashboardReportContainer) obj).getReports().iterator();
86  		}
87  	}
88  
89  }