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 org.talend.sdk.component.runtime.base.Lifecycle; 019import org.talend.sdk.component.runtime.output.InputFactory; 020import org.talend.sdk.component.runtime.output.OutputFactory; 021import org.talend.sdk.component.runtime.output.Processor; 022 023import lombok.RequiredArgsConstructor; 024 025@RequiredArgsConstructor 026public class AutoChunkProcessor implements Lifecycle { 027 028 protected final int chunkSize; 029 030 protected final Processor processor; 031 032 protected int processedItemCount = 0; 033 034 public void onElement(final InputFactory ins, final OutputFactory outs) { 035 if (processedItemCount == 0) { 036 processor.beforeGroup(); 037 } 038 try { 039 processor.onNext(ins, outs); 040 processedItemCount++; 041 } finally { 042 if (processedItemCount == chunkSize) { 043 processor.afterGroup(outs); 044 processedItemCount = 0; 045 } 046 } 047 } 048 049 public void flush(final OutputFactory outs) { 050 if (processedItemCount > 0) { 051 processor.afterGroup(outs); 052 processedItemCount = 0; 053 } 054 } 055 056 @Override 057 public void stop() { 058 processor.stop(); 059 } 060 061 @Override 062 public String plugin() { 063 return processor.plugin(); 064 } 065 066 @Override 067 public String rootName() { 068 return processor.rootName(); 069 } 070 071 @Override 072 public String name() { 073 return processor.name(); 074 } 075 076 @Override 077 public void start() { 078 processor.start(); 079 } 080}