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 Type of the entry, this determine which other fields are populated.
059         */
060        Type getType();
061
062        /**
063         * @return Is this entry nullable or always valued.
064         */
065        boolean isNullable();
066
067        /**
068         * @param <T> the default value type.
069         * @return Default value for this entry.
070         */
071        <T> T getDefaultValue();
072
073        /**
074         * @return For type == record, the element type.
075         */
076        Schema getElementSchema();
077
078        /**
079         * @return Allows to associate to this field a comment - for doc purposes, no use in the runtime.
080         */
081        String getComment();
082
083        // Map<String, Object> metadata <-- DON'T DO THAT, ENSURE ANY META IS TYPED!
084
085        /**
086         * Plain builder matching {@link Entry} structure.
087         */
088        interface Builder {
089
090            Builder withName(String name);
091
092            Builder withType(Type type);
093
094            Builder withNullable(boolean nullable);
095
096            <T> Builder withDefaultValue(T value);
097
098            Builder withElementSchema(Schema schema);
099
100            Builder withComment(String comment);
101
102            Entry build();
103        }
104    }
105
106    /**
107     * Allows to build a schema.
108     */
109    interface Builder {
110
111        /**
112         * @param type schema type.
113         * @return this builder.
114         */
115        Builder withType(Type type);
116
117        /**
118         * @param entry element for either an array or record type.
119         * @return this builder.
120         */
121        Builder withEntry(Entry entry);
122
123        /**
124         * @param schema nested element schema.
125         * @return this builder.
126         */
127        Builder withElementSchema(Schema schema);
128
129        /**
130         * @return the described schema.
131         */
132        Schema build();
133    }
134}