001/*
002 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
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 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.tuple;
022
023import java.util.Iterator;
024import java.util.LinkedList;
025
026/**
027 * TupleEntryCollector is a convenience class for managing a list of tuples. More specifically it can simultaneously
028 * append and modify in place elements of the list through the use of a ListIterator.
029 */
030public class TupleListCollector extends TupleEntryCollector implements Iterable<Tuple>
031  {
032  /** Field tuples */
033  private final LinkedList<Tuple> tuples = new LinkedList<Tuple>();
034  /** Field copyTupleOnCollect */
035  private boolean copyTupleOnCollect = false;
036
037  /**
038   * Constructor TupleEntryCollector creates a new TupleEntryCollector instance.
039   *
040   * @param fields of type Fields
041   * @param tuple  of type Tuple...
042   */
043  public TupleListCollector( Fields fields, Tuple... tuple )
044    {
045    super( fields );
046
047    collect( tuple );
048    }
049
050  /**
051   * Constructor TupleListCollector creates a new TupleListCollector instance.
052   * <p/>
053   * Set copyTupleOnCollect to {@code true} if a new Tuple instance should be stored in the
054   * underlying list.
055   *
056   * @param fields             of type Fields
057   * @param copyTupleOnCollect of type boolean
058   */
059  public TupleListCollector( Fields fields, boolean copyTupleOnCollect )
060    {
061    super( fields );
062    this.copyTupleOnCollect = copyTupleOnCollect;
063    }
064
065  /**
066   * Method collect adds every given Tuple instance. It tests for and ignores empty Tuples.
067   *
068   * @param tuples of type Tuple
069   */
070  private void collect( Tuple... tuples )
071    {
072    for( Tuple tuple : tuples )
073      add( tuple );
074    }
075
076  protected void collect( TupleEntry tupleEntry )
077    {
078    if( copyTupleOnCollect )
079      tuples.add( tupleEntry.getTupleCopy() );
080    else
081      tuples.add( tupleEntry.getTuple() );
082    }
083
084  /**
085   * Method isEmpty returns true if this collection is empty.
086   *
087   * @return the empty (type boolean) of this TupleCollector object.
088   */
089  public boolean isEmpty()
090    {
091    return tuples.isEmpty();
092    }
093
094  /** Method clear clears all Tuple instances from this instance. */
095  public void clear()
096    {
097    tuples.clear();
098    }
099
100  /**
101   * Returns the size of this collection.
102   *
103   * @return int
104   */
105  public int size()
106    {
107    return tuples.size();
108    }
109
110  /**
111   * Method iterator returns an iterator for this collection.
112   *
113   * @return Iterator<Tuple>
114   */
115  public Iterator<Tuple> iterator()
116    {
117    return tuples.iterator();
118    }
119
120  /**
121   * Method entryIterator return a TupleEntry iterator for this collection.
122   * </p>
123   * Note the same TupleEntry will be returned on each next() call.
124   *
125   * @return Iterator<TupleEntry>
126   */
127  public Iterator<TupleEntry> entryIterator()
128    {
129    return new TupleEntryChainIterator( tupleEntry.getFields(), tuples.iterator() );
130    }
131  }