001/**
002 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com
003 * <p>
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 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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.time.ZonedDateTime;
019import java.util.Collection;
020import java.util.Date;
021
022public interface Record {
023
024    /**
025     * @return the schema of this record.
026     */
027    Schema getSchema();
028
029    /**
030     * Access a record field value.
031     *
032     * @param expectedType the expected type for the column.
033     * @param name the name of the column.
034     * @param <T> the type of expectedType.
035     * @return the column value.
036     */
037    <T> T get(Class<T> expectedType, String name);
038
039    default String getString(final String name) {
040        return get(String.class, name);
041    }
042
043    default int getInt(final String name) {
044        return get(Integer.class, name);
045    }
046
047    default long getLong(final String name) {
048        return get(Long.class, name);
049    }
050
051    default double getDouble(final String name) {
052        return get(Double.class, name);
053    }
054
055    default float getFloat(final String name) {
056        return get(Float.class, name);
057    }
058
059    default boolean getBoolean(final String name) {
060        return get(Boolean.class, name);
061    }
062
063    default byte[] getBytes(final String name) {
064        return get(byte[].class, name);
065    }
066
067    default Record getRecord(final String name) {
068        return get(Record.class, name);
069    }
070
071    default <T> Collection<T> getArray(final Class<T> type, final String name) {
072        return get(Collection.class, name);
073    }
074
075    default ZonedDateTime getDateTime(final String name) {
076        return get(ZonedDateTime.class, name);
077    }
078
079    /**
080     * Allows to create a record with a fluent API. This is the unique recommended way to create a record.
081     */
082    interface Builder {
083
084        Record build();
085
086        Builder withString(String name, String value);
087
088        Builder withString(Schema.Entry entry, String value);
089
090        Builder withBytes(String name, byte[] value);
091
092        Builder withBytes(Schema.Entry entry, byte[] value);
093
094        Builder withDateTime(String name, Date value);
095
096        Builder withDateTime(Schema.Entry entry, Date value);
097
098        Builder withDateTime(String name, ZonedDateTime value);
099
100        Builder withDateTime(Schema.Entry entry, ZonedDateTime value);
101
102        Builder withTimestamp(String name, long value);
103
104        Builder withTimestamp(Schema.Entry entry, long value);
105
106        Builder withInt(String name, int value);
107
108        Builder withInt(Schema.Entry entry, int value);
109
110        Builder withLong(String name, long value);
111
112        Builder withLong(Schema.Entry entry, long value);
113
114        Builder withFloat(String name, float value);
115
116        Builder withFloat(Schema.Entry entry, float value);
117
118        Builder withDouble(String name, double value);
119
120        Builder withDouble(Schema.Entry entry, double value);
121
122        Builder withBoolean(String name, boolean value);
123
124        Builder withBoolean(Schema.Entry entry, boolean value);
125
126        Builder withRecord(Schema.Entry entry, Record value);
127
128        <T> Builder withArray(Schema.Entry entry, Collection<T> values);
129    }
130}