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.record;
017
018import java.util.List;
019
020public interface Schema {
021
022    /**
023     * @return the type of this schema.
024     */
025    Type getType();
026
027    /**
028     * @return the nested element schema for arrays.
029     */
030    Schema getElementSchema();
031
032    /**
033     * @return the entries for records.
034     */
035    List<Entry> getEntries();
036
037    enum Type {
038        RECORD,
039        ARRAY,
040        STRING,
041        BYTES,
042        INT,
043        LONG,
044        FLOAT,
045        DOUBLE,
046        BOOLEAN,
047        DATETIME
048    }
049
050    interface Entry {
051
052        /**
053         * @return The name of this entry.
054         */
055        String getName();
056
057        /**
058         * @return The raw name of this entry.
059         */
060        String getRawName();
061
062        /**
063         * @return the raw name of this entry if exists, else return name.
064         */
065        String getOriginalFieldName();
066
067        /**
068         * @return Type of the entry, this determine which other fields are populated.
069         */
070        Type getType();
071
072        /**
073         * @return Is this entry nullable or always valued.
074         */
075        boolean isNullable();
076
077        /**
078         * @param <T> the default value type.
079         * @return Default value for this entry.
080         */
081        <T> T getDefaultValue();
082
083        /**
084         * @return For type == record, the element type.
085         */
086        Schema getElementSchema();
087
088        /**
089         * @return Allows to associate to this field a comment - for doc purposes, no use in the runtime.
090         */
091        String getComment();
092
093        // Map<String, Object> metadata <-- DON'T DO THAT, ENSURE ANY META IS TYPED!
094
095        /**
096         * Plain builder matching {@link Entry} structure.
097         */
098        interface Builder {
099
100            Builder withName(String name);
101
102            Builder withRawName(String rawName);
103
104            Builder withType(Type type);
105
106            Builder withNullable(boolean nullable);
107
108            <T> Builder withDefaultValue(T value);
109
110            Builder withElementSchema(Schema schema);
111
112            Builder withComment(String comment);
113
114            Entry build();
115        }
116    }
117
118    /**
119     * Allows to build a schema.
120     */
121    interface Builder {
122
123        /**
124         * @param type schema type.
125         * @return this builder.
126         */
127        Builder withType(Type type);
128
129        /**
130         * @param entry element for either an array or record type.
131         * @return this builder.
132         */
133        Builder withEntry(Entry entry);
134
135        /**
136         * @param schema nested element schema.
137         * @return this builder.
138         */
139        Builder withElementSchema(Schema schema);
140
141        /**
142         * @return the described schema.
143         */
144        Schema build();
145    }
146}