    001/*
    002 * Copyright (c) 2016-2017 Chris K Wensel. 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.io.IOException;
    024import java.io.UncheckedIOException;
    025import java.util.Objects;
    026import java.util.function.Function;
    027import java.util.function.Supplier;
    028import java.util.function.ToDoubleFunction;
    029import java.util.function.ToIntFunction;
    030import java.util.function.ToLongFunction;
    031import java.util.stream.Stream;
    032
    033import cascading.flow.FlowProcess;
    034import cascading.tap.Tap;
    035import cascading.tuple.type.CoercibleType;
    036
    037/**
    038 * TupleEntryStream provides helper methods to create {@link TupleEntry} {@link Stream} instances from
    039 * {@link Tap} instances.
    040 * <p>
    041 * This class is a convenience class over the methods provided on the Tap class that provide the same
    042 * functionality. This class exists to help overcome any generics compiler warnings.
    043 */
    044public class TupleEntryStream
    045  {
    046  /**
    047   * Method entryStream returns a {@link Stream} of {@link TupleEntry} instances from the given
    048   * {@link Tap} instance.
    049   * <p>
    050   * Also see {@link Tap#entryStream(FlowProcess)}.
    051   *
    052   * @param tap         the Tap to open
    053   * @param flowProcess represents the current platform configuration
    054   * @return a Stream of TupleEntry instances
    055   */
    056  @SuppressWarnings("unchecked")
    057  public static Stream<TupleEntry> entryStream( Tap tap, FlowProcess flowProcess )
    058    {
    059    Objects.requireNonNull( tap );
    060
    061    return tap.entryStream( flowProcess );
    062    }
    063
    064  /**
    065   * Method entryStreamCopy returns a {@link Stream} of {@link TupleEntry} instances from the given
    066   * {@link Tap} instance.
    067   * <p>
    068   * This method returns an TupleEntry instance suitable for caching.
    069   * <p>
    070   * Also see {@link Tap#entryStreamCopy(FlowProcess)}.
    071   *
    072   * @param tap         the Tap to open
    073   * @param flowProcess represents the current platform configuration
    074   * @return a Stream of TupleEntry instances
    075   */
    076  @SuppressWarnings("unchecked")
    077  public static Stream<TupleEntry> entryStreamCopy( Tap tap, FlowProcess flowProcess )
    078    {
    079    Objects.requireNonNull( tap );
    080
    081    return tap.entryStreamCopy( flowProcess );
    082    }
    083
    084  /**
    085   * Method entryStream returns a {@link Stream} of {@link TupleEntry} instances from the given
    086   * {@link Tap} instance.
    087   * <p>
    088   * Also see {@link Tap#entryStream(FlowProcess, Fields)}.
    089   *
    090   * @param tap         the Tap to open
    091   * @param flowProcess represents the current platform configuration
    092   * @param selector    the fields to select from the underlying TupleEntry
    093   * @return a Stream of TupleEntry instances
    094   */
    095  @SuppressWarnings("unchecked")
    096  public static Stream<TupleEntry> entryStream( Tap tap, FlowProcess flowProcess, Fields selector )
    097    {
    098    Objects.requireNonNull( tap );
    099    Objects.requireNonNull( selector );
    100
    101    return tap.entryStream( flowProcess, selector );
    102    }
    103
    104  /**
    105   * Method entryStreamCopy returns a {@link Stream} of {@link TupleEntry} instances from the given
    106   * {@link Tap} instance.
    107   * <p>
    108   * This method returns an TupleEntry instance suitable for caching.
    109   * <p>
    110   * Also see {@link Tap#entryStreamCopy(FlowProcess)}.
    111   *
    112   * @param tap         the Tap to open
    113   * @param flowProcess represents the current platform configuration
    114   * @param selector    the fields to select from the underlying TupleEntry
    115   * @return a Stream of TupleEntry instances
    116   */
    117  @SuppressWarnings("unchecked")
    118  public static Stream<TupleEntry> entryStreamCopy( Tap tap, FlowProcess flowProcess, Fields selector )
    119    {
    120    Objects.requireNonNull( tap );
    121    Objects.requireNonNull( selector );
    122
    123    return tap.entryStreamCopy( flowProcess, selector );
    124    }
    125
    126  /**
    127   * Method fieldToObject returns a {@link Function} that returns the object in the given named field or position.
    128   *
    129   * @param fields the field to select, only first field will be honored
    130   * @param <R>    the type of the object returned
    131   * @return a Function returning the object or null in the selected field
    132   */
    133  @SuppressWarnings("unchecked")
    134  public static <R> Function<TupleEntry, ? extends R> fieldToObject( Fields fields )
    135    {
    136    Objects.requireNonNull( fields );
    137
    138    return value -> (R) value.getObject( fields );
    139    }
    140
    141  /**
    142   * Method fieldToObject returns a {@link Function} that returns the object in the given named field or position
    143   * coerced to the requested {@link CoercibleType} type.
    144   *
    145   * @param fields the field to select, only first field will be honored
    146   * @param <R>    the type of the object returned
    147   * @param type   the CoercibleType to coerce the selected value into
    148   * @return a Function returning the object or null in the selected field
    149   */
    150  @SuppressWarnings("unchecked")
    151  public static <R> Function<TupleEntry, R> fieldToObject( Fields fields, CoercibleType<R> type )
    152    {
    153    Objects.requireNonNull( fields );
    154    Objects.requireNonNull( type );
    155
    156    return value -> (R) value.getObject( fields, type );
    157    }
    158
    159  /**
    160   * Method fieldToObject returns a {@link Function} that returns the object in the given named field or position
    161   * coerced to the requested {@link Class}.
    162   *
    163   * @param fields the field to select, only first field will be honored
    164   * @param <R>    the type of the object returned
    165   * @param type   the Class to coerce the selected value into
    166   * @return a Function returning the object or null in the selected field
    167   */
    168  @SuppressWarnings("unchecked")
    169  public static <R> Function<TupleEntry, R> fieldToObject( Fields fields, Class<R> type )
    170    {
    171    Objects.requireNonNull( fields );
    172    Objects.requireNonNull( type );
    173
    174    return value -> (R) value.getObject( fields, type );
    175    }
    176
    177  /**
    178   * Method fieldToInt returns a {@link Function} that returns the int in the given named field or position.
    179   *
    180   * @param fields the field to select, only first field will be honored
    181   * @return the int value in the selected field
    182   */
    183  public static ToIntFunction<TupleEntry> fieldToInt( Fields fields )
    184    {
    185    Objects.requireNonNull( fields );
    186
    187    return value -> value.getInteger( fields );
    188    }
    189
    190  /**
    191   * Method fieldToInt returns a {@link Function} that returns the int in the given named field or position.
    192   *
    193   * @param name the field to select
    194   * @return the int value in the selected field
    195   */
    196  public static ToIntFunction<TupleEntry> fieldToInt( Comparable name )
    197    {
    198    Objects.requireNonNull( name );
    199
    200    return value -> value.getInteger( name );
    201    }
    202
    203  /**
    204   * Method fieldToLong returns a {@link Function} that returns the long in the given named field or position.
    205   *
    206   * @param fields the field to select, only first field will be honored
    207   * @return the long value in the selected field
    208   */
    209  public static ToLongFunction<TupleEntry> fieldToLong( Fields fields )
    210    {
    211    Objects.requireNonNull( fields );
    212
    213    return value -> value.getLong( fields );
    214    }
    215
    216  /**
    217   * Method fieldToLong returns a {@link Function} that returns the long in the given named field or position.
    218   *
    219   * @param name the field to select
    220   * @return the long value in the selected field
    221   */
    222  public static ToLongFunction<TupleEntry> fieldToLong( Comparable name )
    223    {
    224    Objects.requireNonNull( name );
    225
    226    return value -> value.getLong( name );
    227    }
    228
    229  /**
    230   * Method fieldToDouble returns a {@link Function} that returns the double in the given named field or position.
    231   *
    232   * @param fields the field to select, only first field will be honored
    233   * @return the double value in the selected field
    234   */
    235  public static ToDoubleFunction<TupleEntry> fieldToDouble( Fields fields )
    236    {
    237    Objects.requireNonNull( fields );
    238
    239    return value -> value.getDouble( fields );
    240    }
    241
    242  /**
    243   * Method fieldToDouble returns a {@link Function} that returns the double in the given named field or position.
    244   *
    245   * @param name the field to select
    246   * @return the double value in the selected field
    247   */
    248  public static ToDoubleFunction<TupleEntry> fieldToDouble( Comparable name )
    249    {
    250    Objects.requireNonNull( name );
    251
    252    return value -> value.getDouble( name );
    253    }
    254
    255  /**
    256   * Method writeEntry will add each {@link TupleEntry} instance to the {@link TupleEntryCollector} provided
    257   * by the given {@link Tap} instance.
    258   *
    259   * @param stream      a Stream of TupleEntry instances
    260   * @param into        a Supplier that returns the Tap to sink each entry into
    261   * @param flowProcess represents the current platform configuration
    262   * @return returns the given Tap
    263   */
    264  public static Tap writeEntry( Stream<TupleEntry> stream, Supplier<Tap> into, FlowProcess flowProcess )
    265    {
    266    return writeEntry( stream, into.get(), flowProcess );
    267    }
    268
    269  /**
    270   * Method writeEntry will add each {@link TupleEntry} instance to the {@link TupleEntryCollector} provided
    271   * by the given {@link Tap} instance.
    272   *
    273   * @param stream      a Stream of TupleEntry instances
    274   * @param into        the Tap to sink each entry into
    275   * @param flowProcess represents the current platform configuration
    276   * @return returns the given Tap
    277   */
    278  @SuppressWarnings("unchecked")
    279  public static Tap writeEntry( Stream<TupleEntry> stream, Tap into, FlowProcess flowProcess )
    280    {
    281    Objects.requireNonNull( into );
    282    Objects.requireNonNull( stream );
    283
    284    try
    285      {
    286      TupleEntryCollector collector = into.openForWrite( flowProcess );
    287
    288      stream.forEach( collector::add );
    289
    290      collector.close();
    291      }
    292    catch( IOException exception )
    293      {
    294      throw new UncheckedIOException( exception );
    295      }
    296
    297    return into;
    298    }
    299
    300  /**
    301   * Method writeEntry will add each {@link TupleEntry} instance to the {@link TupleEntryCollector} provided
    302   * by the given {@link Tap} instance.
    303   *
    304   * @param iterable    an Iterable of TupleEntry instances
    305   * @param into        a Supplier that returns the Tap to sink each entry into
    306   * @param flowProcess represents the current platform configuration
    307   * @return returns the given Tap
    308   */
    309  public static Tap writeEntry( Iterable<TupleEntry> iterable, Supplier<Tap> into, FlowProcess flowProcess )
    310    {
    311    return writeEntry( iterable, into.get(), flowProcess );
    312    }
    313
    314  /**
    315   * Method writeEntry will add each {@link TupleEntry} instance to the {@link TupleEntryCollector} provided
    316   * by the given {@link Tap} instance.
    317   *
    318   * @param iterable    an Iterable of TupleEntry instances
    319   * @param into        the Tap to sink each entry into
    320   * @param flowProcess represents the current platform configuration
    321   * @return returns the given Tap
    322   */
    323  @SuppressWarnings("unchecked")
    324  public static Tap writeEntry( Iterable<TupleEntry> iterable, Tap into, FlowProcess flowProcess )
    325    {
    326    Objects.requireNonNull( into );
    327    Objects.requireNonNull( iterable );
    328
    329    try
    330      {
    331      TupleEntryCollector collector = into.openForWrite( flowProcess );
    332
    333      iterable.forEach( collector::add );
    334
    335      collector.close();
    336      }
    337    catch( IOException exception )
    338      {
    339      throw new UncheckedIOException( exception );
    340      }
    341
    342    return into;
    343    }
    344  }