001/**
002 * Copyright (C) 2006-2022 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.io.Serializable;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.talend.sdk.component.api.record.Record;
023import org.talend.sdk.component.api.record.Schema;
024
025/**
026 * Entry point to create records (through builders).
027 */
028public interface RecordBuilderFactory extends Serializable {
029
030    /**
031     * Enables to build a record from another one. It is typically useful to add a column and passthrough others.
032     *
033     * @param schema the schema if the output record.
034     * @param record the record to take as input.
035     * @return a new builder initialized with the input record for all matching entries (by name).
036     */
037    Record.Builder newRecordBuilder(Schema schema, Record record);
038
039    /**
040     * @param schema the schema of the record to be built
041     * @return a builder to create a new record and enforce the built record to respect
042     * a static schema. If the entries don't match the schema the build call will fail.
043     */
044    Record.Builder newRecordBuilder(Schema schema);
045
046    /**
047     * @return a builder to create a new record.
048     */
049    Record.Builder newRecordBuilder();
050
051    /**
052     * @param type the schema type.
053     * @return a builder to create a schema.
054     */
055    Schema.Builder newSchemaBuilder(Schema.Type type);
056
057    /**
058     * Build a schema from another one. Typically useful to add a column and letting others passthrough.
059     * 
060     * @param schema the input schema.
061     * @return a new schema builder intialized with the input schema.
062     */
063    Schema.Builder newSchemaBuilder(Schema schema);
064
065    /**
066     * @return a builder to create a schema entry.
067     */
068    Schema.Entry.Builder newEntryBuilder();
069
070    /**
071     * Build a schema.entry from another one. Useful to duplicate a column with some changes.
072     * 
073     * @param model : model of entry to copy.
074     * @return entry builder with model parameters.
075     */
076    default Schema.Entry.Builder newEntryBuilder(Schema.Entry model) {
077        final Map<String, String> props = new HashMap<>();
078        final Map<String, String> modelProps = model.getProps();
079        if (modelProps != null) {
080            props.putAll(modelProps);
081        }
082        return this
083                .newEntryBuilder()
084                .withType(model.getType())
085                .withNullable(model.isNullable())
086                .withName(model.getName())
087                .withElementSchema(model.getElementSchema())
088                .withDefaultValue(model.getDefaultValue())
089                .withComment(model.getComment())
090                .withProps(props);
091    }
092}