001/**
002 * Copyright (C) 2006-2024 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.runtime.manager.chain;
017
018import org.talend.sdk.component.runtime.input.Input;
019
020import lombok.RequiredArgsConstructor;
021
022@RequiredArgsConstructor
023public final class ChainedInput implements Input {
024
025    private final ChainedMapper parent;
026
027    private Input delegate = null;
028
029    @Override
030    public Object next() {
031        while (true) {
032            if (delegate == null) {
033                delegate = parent.getIterator().hasNext() ? parent.getIterator().next().create() : null;
034                if (delegate == null) {
035                    return null;
036                }
037                delegate.start();
038            }
039            final Object next = delegate.next();
040            if (next != null) {
041                return next;
042            }
043            delegate.stop();
044            delegate = null;
045        }
046    }
047
048    @Override
049    public String plugin() {
050        return parent.plugin();
051    }
052
053    @Override
054    public String rootName() {
055        return parent.rootName();
056    }
057
058    @Override
059    public String name() {
060        return parent.name();
061    }
062
063    @Override
064    public void start() {
065        // no-op
066    }
067
068    @Override
069    public void stop() {
070        if (delegate != null) {
071            delegate.stop();
072        }
073    }
074}