    001/*
    002 * Copyright (c) 2007-2016 Concurrent, 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.tap.partition;
    022
    023import java.util.Iterator;
    024
    025import cascading.tap.TapException;
    026import cascading.tuple.Fields;
    027import cascading.tuple.Tuple;
    028import cascading.tuple.TupleEntry;
    029import cascading.tuple.TupleEntrySchemeIterator;
    030import cascading.tuple.util.TupleViews;
    031
    032/**
    033 *
    034 */
    035public class PartitionTupleEntryIterator implements Iterator<Tuple>
    036  {
    037  private final TupleEntrySchemeIterator childIterator;
    038  private final Tuple base;
    039  private final Tuple view;
    040
    041  public PartitionTupleEntryIterator( Fields sourceFields, Partition partition, String parentIdentifier, String childIdentifier, TupleEntrySchemeIterator schemeIterator )
    042    {
    043    this.childIterator = schemeIterator;
    044
    045    TupleEntry partitionEntry = new TupleEntry( partition.getPartitionFields(), Tuple.size( partition.getPartitionFields().size() ) );
    046
    047    try
    048      {
    049      partition.toTuple( childIdentifier.substring( parentIdentifier.length() + 1 ), partitionEntry );
    050      }
    051    catch( Exception exception )
    052      {
    053      throw new TapException( "unable to parse partition given parent: " + parentIdentifier + " and child: " + childIdentifier );
    054      }
    055
    056    base = TupleViews.createOverride( sourceFields, partitionEntry.getFields() );
    057
    058    TupleViews.reset( base, Tuple.size( sourceFields.size() ), partitionEntry.getTuple() );
    059
    060    view = TupleViews.createOverride( sourceFields, childIterator.getFields() );
    061    }
    062
    063  @Override
    064  public boolean hasNext()
    065    {
    066    return childIterator.hasNext();
    067    }
    068
    069  @Override
    070  public Tuple next()
    071    {
    072    Tuple tuple = childIterator.next().getTuple();
    073    TupleViews.reset( view, base, tuple );
    074
    075    return view;
    076    }
    077
    078  @Override
    079  public void remove()
    080    {
    081    childIterator.remove();
    082    }
    083  }