    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.flow.hadoop.util;
    022
    023import java.util.Collection;
    024import java.util.Iterator;
    025
    026import cascading.tuple.Tuple;
    027
    028/**
    029 *
    030 */
    031public class LazyCollection implements Collection<Tuple>, ResettableCollection<Iterator<Tuple>>
    032  {
    033  Iterator<Tuple> iterator;
    034  Collection<Tuple> parent;
    035
    036  public static Collection<Tuple> getParent( LazyCollection collection )
    037    {
    038    return collection.parent;
    039    }
    040
    041  public LazyCollection( Collection<Tuple> parent )
    042    {
    043    this.parent = parent;
    044    }
    045
    046  @Override
    047  public int size()
    048    {
    049    return parent.size();
    050    }
    051
    052  @Override
    053  public boolean isEmpty()
    054    {
    055    return !iterator.hasNext() && parent.isEmpty();
    056    }
    057
    058  @Override
    059  public boolean contains( Object o )
    060    {
    061    return parent.contains( o );
    062    }
    063
    064  @Override
    065  public Iterator<Tuple> iterator()
    066    {
    067    if( !iterator.hasNext() )
    068      return parent.iterator();
    069
    070    return new Iterator<Tuple>()
    071    {
    072    @Override
    073    public boolean hasNext()
    074      {
    075      return iterator.hasNext();
    076      }
    077
    078    @Override
    079    public Tuple next()
    080      {
    081      Tuple next = iterator.next();
    082
    083      parent.add( next );
    084
    085      return next;
    086      }
    087
    088    @Override
    089    public void remove()
    090      {
    091      iterator.remove();
    092      }
    093    };
    094    }
    095
    096  @Override
    097  public Object[] toArray()
    098    {
    099    return parent.toArray();
    100    }
    101
    102  @Override
    103  public <T> T[] toArray( T[] a )
    104    {
    105    return parent.toArray( a );
    106    }
    107
    108  @Override
    109  public boolean add( Tuple objects )
    110    {
    111    return parent.add( objects );
    112    }
    113
    114  @Override
    115  public boolean remove( Object o )
    116    {
    117    return parent.remove( o );
    118    }
    119
    120  @Override
    121  public boolean containsAll( Collection<?> c )
    122    {
    123    return parent.containsAll( c );
    124    }
    125
    126  @Override
    127  public boolean addAll( Collection<? extends Tuple> c )
    128    {
    129    return parent.addAll( c );
    130    }
    131
    132  @Override
    133  public boolean removeAll( Collection<?> c )
    134    {
    135    return parent.removeAll( c );
    136    }
    137
    138  @Override
    139  public boolean retainAll( Collection<?> c )
    140    {
    141    return parent.retainAll( c );
    142    }
    143
    144  @Override
    145  public void clear()
    146    {
    147    parent.clear();
    148    }
    149
    150  @Override
    151  public boolean equals( Object o )
    152    {
    153    return parent.equals( o );
    154    }
    155
    156  @Override
    157  public int hashCode()
    158    {
    159    return parent.hashCode();
    160    }
    161
    162  @Override
    163  public void reset( Iterator<Tuple> iterator )
    164    {
    165    this.iterator = iterator;
    166    }
    167  }