001/** 002 * Copyright (C) 2006-2021 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.api.service.record; 017 018import java.util.function.BiConsumer; 019import java.util.function.BiFunction; 020import java.util.stream.Collector; 021 022import org.talend.sdk.component.api.record.Record; 023import org.talend.sdk.component.api.record.Schema; 024 025/** 026 * Helping class to build records. 027 */ 028public interface RecordService { 029 030 /** 031 * Simple mapper of record to pojo. 032 * 033 * @param data record to map to pojo. 034 * @param expected : class expected. 035 * @param <T> the pojo type (optional). 036 * @return a pojo representing the data record. 037 */ 038 <T> T toObject(final Record data, Class<T> expected); 039 040 /** 041 * Simple mapper of data to record. 042 * 043 * @param data pojo to map to record. 044 * @param <T> the pojo type (optional). 045 * @return a record representing the pojo. 046 */ 047 <T> Record toRecord(final T data); 048 049 /** 050 * Forward an entry from the source record if it exists. 051 * 052 * @param source the source record to read data from. 053 * @param builder the current builder to fill. 054 * @param sourceColumn the column name. 055 * @param entry the entry to use for the output column. 056 * @return true if the entry was forwarded. 057 */ 058 boolean forwardEntry(Record source, Record.Builder builder, String sourceColumn, Schema.Entry entry); 059 060 /** 061 * Method providing a collector enabling to create a record from another one in a custom fashion. 062 * 063 * @param schema the schema of the record being built. 064 * @param fallbackRecord the source record used when the custom handler does not handle current entry. 065 * @param customHandler a processor of entry enabling to inject custom data in the record being built. 066 * @param beforeFinish a callback before the record is actually built, it enables to add data if not already there. 067 * @return a collector enabling to build a record. 068 */ 069 Collector<Schema.Entry, Record.Builder, Record> toRecord(Schema schema, Record fallbackRecord, 070 BiFunction<Schema.Entry, Record.Builder, Boolean> customHandler, 071 BiConsumer<Record.Builder, Boolean> beforeFinish); 072 073 /** 074 * Shortcut to build a record using {@link RecordService#toRecord(Schema, Record, BiFunction, BiConsumer)}. 075 * 076 * @param schema the schema of the record being built. 077 * @param fallbackRecord the source record used when the custom handler does not handle current entry. 078 * @param customHandler a processor of entry enabling to inject custom data in the record being built. 079 * @param beforeFinish a callback before the record is actually built, it enables to add data if not already there. 080 * @return a collector enabling to build a record. 081 */ 082 Record create(Schema schema, Record fallbackRecord, BiFunction<Schema.Entry, Record.Builder, Boolean> customHandler, 083 BiConsumer<Record.Builder, Boolean> beforeFinish); 084 085 /** 086 * Visit a record with a custom visitor. 087 * 088 * @param visitor the visitor to use to browse the record. 089 * @param record record to visit. 090 * @param <T> the visitor returned type. 091 * @return the visitor value. 092 */ 093 <T> T visit(RecordVisitor<T> visitor, Record record); 094}