com.hazelcast.jet.pipeline

## Class ContextFactory<C>

- java.lang.Object
- - com.hazelcast.jet.pipeline.ContextFactory<C>

- Type Parameters:
    `C` - the user-defined context object type
  All Implemented Interfaces:
    Serializable

      public final class ContextFactory<C>
      extends Object
      implements Serializable

  A holder of functions needed to create and destroy a context object.

  You can use the context factory from these Pipeline API methods:

  - `GeneralStage.mapUsingContext(com.hazelcast.jet.pipeline.ContextFactory<C>, com.hazelcast.jet.function.DistributedBiFunction<? super C, ? super T, ? extends R>)`
  - `GeneralStage.filterUsingContext(com.hazelcast.jet.pipeline.ContextFactory<C>, com.hazelcast.jet.function.DistributedBiPredicate<? super C, ? super T>)`
  - `GeneralStage.flatMapUsingContext(com.hazelcast.jet.pipeline.ContextFactory<C>, com.hazelcast.jet.function.DistributedBiFunction<? super C, ? super T, ? extends com.hazelcast.jet.Traverser<? extends R>>)`
  - `GeneralStageWithKey.mapUsingContext(com.hazelcast.jet.pipeline.ContextFactory<C>, com.hazelcast.jet.function.DistributedTriFunction<? super C, ? super K, ? super T, ? extends R>)`
  - `GeneralStageWithKey.filterUsingContext(com.hazelcast.jet.pipeline.ContextFactory<C>, com.hazelcast.jet.function.DistributedTriPredicate<? super C, ? super K, ? super T>)`
  - `GeneralStageWithKey.flatMapUsingContext(com.hazelcast.jet.pipeline.ContextFactory<C>, com.hazelcast.jet.function.DistributedTriFunction<? super C, ? super K, ? super T, ? extends com.hazelcast.jet.Traverser<? extends R>>)`
  See Also:
    Serialized Form

- - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `DistributedFunction<JetInstance,? extends C>` | `createFn()` Returns the create-function.
    `DistributedConsumer<? super C>` | `destroyFn()` Returns the destroy-function.
    `boolean` | `isCooperative()` Returns the `isCooperative` flag.
    `boolean` | `isSharedLocally()` Returns the `isSharedLocally` flag.
    `ContextFactory<C>` | `nonCooperative()` Returns a copy of this `ContextFactory` with the isCooperative flag set to `false`.
    `ContextFactory<C>` | `shareLocally()` Returns a copy of this `ContextFactory` with the shareLocally flag set.
    `static <C> ContextFactory<C>` | `withCreateFn(DistributedFunction<JetInstance,? extends C> createContextFn)` Creates a new `ContextFactory` with the given create-function.
    `ContextFactory<C>` | `withDestroyFn(DistributedConsumer<? super C> destroyFn)` Returns a copy of this `ContextFactory` with the destroy-function replaced with the given function.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### withCreateFn

          @Nonnull
          public static <C> ContextFactory<C> withCreateFn(@Nonnull
                                                                    DistributedFunction<JetInstance,? extends C> createContextFn)

      Creates a new `ContextFactory` with the given create-function.
      Type Parameters:
        `C` - the user-defined context object type
      Parameters:
        `createContextFn` - the function to create new context object, given a JetInstance
      Returns:
        a new factory instance

    - #### withDestroyFn

          @Nonnull
          public ContextFactory<C> withDestroyFn(@Nonnull
                                                          DistributedConsumer<? super C> destroyFn)

      Returns a copy of this `ContextFactory` with the destroy-function replaced with the given function.

      The destroy function is called at the end of the job to destroy all created context objects.

      Parameters:
        `destroyFn` - the function to destroy user-defined context
      Returns:
        a copy of this factory with the supplied destroy-function

    - #### nonCooperative

          @Nonnull
          public ContextFactory<C> nonCooperative()

      Returns a copy of this `ContextFactory` with the isCooperative flag set to `false`. The context factory is cooperative by default. Call this method if your transform function doesn't follow the cooperative processor contract.
      Returns:
        a copy of this factory with the `isCooperative` flag set to `false`.

    - #### shareLocally

          @Nonnull
          public ContextFactory<C> shareLocally()

      Returns a copy of this `ContextFactory` with the shareLocally flag set. If the pipeline doesn't have grouping, there will be:
      - one context object per local processor, if flag is disabled
      - one context object per member, if flag is enabled. Make sure the context object is thread-safe in this case.
      If the pipeline has grouping, the context object is never shared and this flag is ignored.
      Returns:
        a copy of this factory with the `isSharedLocally` flag set.

    - #### createFn

          @Nonnull
          public DistributedFunction<JetInstance,? extends C> createFn()

      Returns the create-function.

    - #### destroyFn

          @Nonnull
          public DistributedConsumer<? super C> destroyFn()

      Returns the destroy-function.

    - #### isCooperative

          public boolean isCooperative()

      Returns the `isCooperative` flag.

    - #### isSharedLocally

          public boolean isSharedLocally()

      Returns the `isSharedLocally` flag.

Copyright © 2018. All rights reserved.