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