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