001/**
002 * Copyright (C) 2006-2020 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     * Forward an entry from the source record if it exists.
032     *
033     * @param source the source record to read data from.
034     * @param builder the current builder to fill.
035     * @param sourceColumn the column name.
036     * @param entry the entry to use for the output column.
037     * @return true if the entry was forwarded.
038     */
039    boolean forwardEntry(Record source, Record.Builder builder, String sourceColumn, Schema.Entry entry);
040
041    /**
042     * Method providing a collector enabling to create a record from another one in a custom fashion.
043     *
044     * @param schema the schema of the record being built.
045     * @param fallbackRecord the source record used when the custom handler does not handle current entry.
046     * @param customHandler a processor of entry enabling to inject custom data in the record being built.
047     * @param beforeFinish a callback before the record is actually built, it enables to add data if not already there.
048     * @return a collector enabling to build a record.
049     */
050    Collector<Schema.Entry, Record.Builder, Record> toRecord(Schema schema, Record fallbackRecord,
051            BiFunction<Schema.Entry, Record.Builder, Boolean> customHandler,
052            BiConsumer<Record.Builder, Boolean> beforeFinish);
053
054    /**
055     * Shortcut to build a record using {@link RecordService#toRecord(Schema, Record, BiFunction, BiConsumer)}.
056     *
057     * @param schema the schema of the record being built.
058     * @param fallbackRecord the source record used when the custom handler does not handle current entry.
059     * @param customHandler a processor of entry enabling to inject custom data in the record being built.
060     * @param beforeFinish a callback before the record is actually built, it enables to add data if not already there.
061     * @return a collector enabling to build a record.
062     */
063    Record create(Schema schema, Record fallbackRecord, BiFunction<Schema.Entry, Record.Builder, Boolean> customHandler,
064            BiConsumer<Record.Builder, Boolean> beforeFinish);
065
066    /**
067     * Visit a record with a custom visitor.
068     *
069     * @param visitor the visitor to use to browse the record.
070     * @param <T> the visitor returned type.
071     * @return the visitor value.
072     */
073    <T> T visit(RecordVisitor<T> visitor, Record record);
074}