001/** 002 * Copyright (C) 2006-2025 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 java.util.Optional; 019import java.util.function.Consumer; 020 021import org.talend.sdk.component.runtime.input.CheckpointState; 022import org.talend.sdk.component.runtime.input.Input; 023 024import lombok.RequiredArgsConstructor; 025 026@RequiredArgsConstructor 027public final class ChainedInput implements Input { 028 029 private final ChainedMapper parent; 030 031 private Input delegate = null; 032 033 private Optional<Consumer<CheckpointState>> checkpointStateConsumer = Optional.empty(); 034 035 @Override 036 public Object next() { 037 while (true) { 038 if (delegate == null) { 039 this.delegate = nextDelegate(); 040 if (delegate == null) { 041 return null; 042 } 043 } 044 045 final Object next = delegate.next(); 046 if (next != null) { 047 return next; 048 } 049 delegate.stop(); 050 delegate = null; 051 } 052 } 053 054 @Override 055 public String plugin() { 056 return parent.plugin(); 057 } 058 059 @Override 060 public String rootName() { 061 return parent.rootName(); 062 } 063 064 @Override 065 public String name() { 066 return parent.name(); 067 } 068 069 @Override 070 public void start() { 071 // no-op 072 } 073 074 @Override 075 public void start(final Consumer<CheckpointState> checkpoint) { 076 this.checkpointStateConsumer = Optional.of(checkpoint); 077 } 078 079 @Override 080 public CheckpointState getCheckpoint() { 081 return delegate.getCheckpoint(); 082 } 083 084 @Override 085 public boolean isCheckpointReady() { 086 return delegate.isCheckpointReady(); 087 } 088 089 @Override 090 public void stop() { 091 if (delegate != null) { 092 delegate.stop(); 093 } 094 } 095 096 private Input nextDelegate() { 097 Input localDelegate = parent.getIterator().hasNext() ? parent.getIterator().next().create() : null; 098 if (localDelegate != null) { 099 if (checkpointStateConsumer.isPresent()) { 100 localDelegate.start(checkpointStateConsumer.get()); 101 } else { 102 localDelegate.start(); 103 } 104 } 105 return localDelegate; 106 } 107}