All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.jet.impl.processor.TransformUsingServiceP Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.jet.impl.processor;

import com.hazelcast.jet.Traverser;
import com.hazelcast.jet.core.ProcessorSupplier;
import com.hazelcast.jet.core.ResettableSingletonTraverser;
import com.hazelcast.jet.function.TriFunction;
import com.hazelcast.jet.pipeline.ServiceFactory;

import javax.annotation.Nonnull;

import static com.hazelcast.jet.impl.processor.ProcessorSupplierWithService.supplierWithService;

/**
 * Backing processor for {@link
 * com.hazelcast.jet.pipeline.GeneralStage#mapUsingService}.
 *
 * @param  service type
 * @param  received item type
 * @param  emitted item type
 */
public final class TransformUsingServiceP extends AbstractTransformUsingServiceP {

    private final TriFunction, ? super S, ? super T, ? extends Traverser>
            flatMapFn;

    private Traverser outputTraverser;
    private final ResettableSingletonTraverser singletonTraverser = new ResettableSingletonTraverser<>();

    /**
     * Constructs a processor with the given mapping function.
     */
    private TransformUsingServiceP(
            @Nonnull ServiceFactory serviceFactory,
            @Nonnull C context,
            @Nonnull TriFunction, ? super S, ? super T, ? extends Traverser>
                    flatMapFn
    ) {
        super(serviceFactory, context);
        this.flatMapFn = flatMapFn;
    }


    @Override
    @SuppressWarnings("unchecked")
    protected boolean tryProcess(int ordinal, @Nonnull Object item) {
        if (outputTraverser == null) {
            outputTraverser = flatMapFn.apply(singletonTraverser, service, (T) item);
        }
        if (emitFromTraverser(outputTraverser)) {
            outputTraverser = null;
            return true;
        }
        return false;
    }

    /**
     * The {@link ResettableSingletonTraverser} is passed as a first argument to
     * {@code flatMapFn}, it can be used if needed.
     */
    public static  ProcessorSupplier supplier(
            @Nonnull ServiceFactory serviceFactory,
            @Nonnull TriFunction, ? super S, ? super T, ? extends Traverser>
                    flatMapFn
    ) {
        return supplierWithService(serviceFactory,
                (serviceFn, context) -> new TransformUsingServiceP(serviceFn, context, flatMapFn)
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy