View Javadoc

1   /*
2    * $Id: CollectionMessageSequence.java 22272 2011-06-27 16:17:16Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.routing.outbound;
12  
13  import java.util.Collection;
14  import java.util.Iterator;
15  
16  import org.apache.commons.lang.Validate;
17  import org.mule.routing.AbstractMessageSequence;
18  import org.mule.routing.MessageSequence;
19  
20  /**
21   * A {@link MessageSequence} that retrieves elements from a {@link Collection}. Its
22   * estimated size is initially the size of the collection, and decreases when
23   * elements are consumed using {@link #next()}
24   * 
25   * @author flbulgarelli
26   * @param <T>
27   */
28  public final class CollectionMessageSequence<T> extends AbstractMessageSequence<T>
29  {
30      private final Iterator<T> iter;
31      private int remaining;
32  
33      public CollectionMessageSequence(Collection<T> collection)
34      {
35          Validate.notNull(collection);
36          this.iter = collection.iterator();
37          this.remaining = collection.size();
38      }
39  
40      public int size()
41      {
42          return remaining;
43      }
44  
45      public boolean hasNext()
46      {
47          return iter.hasNext();
48      }
49  
50      public T next()
51      {
52          T next = iter.next();
53          remaining--;
54          return next;
55      }
56  
57  }