    001/*
    002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. 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;
    022
    023import java.util.Collection;
    024import java.util.Iterator;
    025import java.util.Set;
    026
    027import cascading.util.Util;
    028
    029/**
    030 * Helper utilities for {@link CompositeTap} instances.
    031 */
    032public class CompositeTaps
    033  {
    034  private CompositeTaps()
    035    {
    036    }
    037
    038  public static Collection<? extends Tap> unwindNarrow( Class<? extends Tap> type, Tap tap )
    039    {
    040    return Util.narrowIdentitySet( type, unwind( tap ) );
    041    }
    042
    043  public static Collection<Tap> unwind( Tap tap )
    044    {
    045    Set<Tap> taps = Util.createIdentitySet();
    046
    047    addLeaves( tap, taps );
    048
    049    return taps;
    050    }
    051
    052  private static void addLeaves( Tap tap, Set<Tap> taps )
    053    {
    054    if( tap instanceof CompositeTap )
    055      {
    056      Iterator<? extends Tap> childTaps = ( (CompositeTap<? extends Tap>) tap ).getChildTaps();
    057
    058      while( childTaps.hasNext() )
    059        addLeaves( childTaps.next(), taps );
    060      }
    061    else
    062      {
    063      taps.add( tap );
    064      }
    065    }
    066  }