Class TupleCopier

java.lang.Object
com.streambase.sb.TupleCopier

public class TupleCopier extends Object
A utility class which copies the fields from one tuple to another. TupleCopier uses the given schemas to determine which fields to copy from one Tuple to another. The strict flag on the ctor determines if an Exception is thrown if the fields in the destination schema do not match the fields in the source schema. Fields are matched by name and type. With the strict flag set to false the TupleCopier will only copy the fields between the source and destination that match field name and type. TupleCopier is more efficient than using Tuple's getField(), setField() methods to copy portions of a tuple. TupleCopier's constructor sets up the TupleCopier so that repeated calls to copyTuple() are more efficient. If a full copy is desired Tuple.clone() is probably a more efficient option. An example usage:


     ...
     TupleCopier input2OutputCopier = new TupleCopier(inputSchema, outputSchema, true);
     
     while (true) {
        outputTuple.clear();
        input2OutputCopier.copyTuple(getInputTuple(), outputTuple);
        sendOutput(port, outputTuple);
     }
     
 

Since:
6.4.5
See Also:
  • Constructor Details

    • TupleCopier

      public TupleCopier(Schema sourceSchema, Schema destSchema, boolean strict) throws TupleException
      Create the TupleCopier. Do some up front error checking and setting up Schema maps. This constructor will create a TupleCopier with #CopyByName semantics.
      Parameters:
      sourceSchema - The schema of the source Tuple
      destSchema - The schema of the destination tuple
      strict - set to true will throw exceptions if schema fields do not match by name and type. Set to false and the TupleCopier will copy only fields that match by name and type.
      Throws:
      TupleException - with strict on if the fields do not match by name and type
    • TupleCopier

      public TupleCopier(Schema sourceSchema, Schema destSchema, EnumSet<TupleCopier.Options> options) throws TupleException
      Create the TupleCopier. Do some up front error checking and setting up Schema maps.
      Parameters:
      sourceSchema - The schema of the source Tuple
      destSchema - The schema of the destination tuple
      options - options from TupleCopier.Options
      Throws:
      TupleException - with strict on if the fields do not match by name and type
      Since:
      7.0
  • Method Details

    • copyTuple

      public void copyTuple(Tuple sourceTuple, Tuple destTuple) throws TupleException
      Copy the fields from the sourceTuple to the destTuple. It is assumed that the destination Tuple is either new or has been cleared.
      Parameters:
      sourceTuple - the source tuple
      destTuple - the cleared destination tuple
      Throws:
      TupleException - on field copy errors
    • copyList

      List<?> copyList(List<?> sourceList) throws TupleException
      Copy the given list
      Parameters:
      sourceList - the list to copy
      Returns:
      a new list
      Throws:
      TupleException - on error
    • createCopier

      static com.streambase.sb.DataTypeCopier createCopier(CompleteDataType sourceType, CompleteDataType destType, EnumSet<TupleCopier.Options> options) throws TupleException
      Create a DataTypeCopier
      Parameters:
      sourceType - source data type
      destType - destination data type
      options - copy options
      Returns:
      appropriate DataTypeCopier
      Throws:
      TupleException - on error
    • copyField

      static void copyField(Tuple sourceTuple, Schema.Field sourceField, Tuple destTuple, Schema.Field destField, com.streambase.sb.DataTypeCopier fieldCopier) throws TupleException
      Copy the field from the source tuple to the destination tuple
      Parameters:
      sourceTuple - source tuple
      sourceField - source file
      destTuple - destination tuple
      destField - destination field
      fieldCopier - field copier
      Throws:
      TupleException - on error
    • copyList

      protected static List<?> copyList(com.streambase.sb.DataTypeCopier fieldCopier, CompleteDataType destType, List<?> sourceList) throws TupleException
      a common method to do the list coping
      Parameters:
      fieldCopier - the field copier
      destType - the dest type
      sourceList - the source list
      Returns:
      a copy of the list or null if the sourceList was null
      Throws:
      TupleException - on error