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.math.BigDecimal;
019import java.time.ZonedDateTime;
020import java.util.Collection;
021import java.util.Optional;
022import java.util.OptionalDouble;
023import java.util.OptionalInt;
024import java.util.OptionalLong;
025import java.util.function.BinaryOperator;
026import java.util.function.Supplier;
027
028import org.talend.sdk.component.api.record.Record;
029import org.talend.sdk.component.api.record.Schema;
030
031/**
032 * Visitor enabling to browse a record. All methods are adapters - implementing a no-op by default.
033 * 
034 * @param <T> the returned type by the visitor if it owns a state.
035 */
036public interface RecordVisitor<T> extends Supplier<T>, BinaryOperator<T> {
037
038    /**
039     * This is called to get the value extracted from this visitor.
040     * It is also an exit callback for a record instance.
041     *
042     * @return the outcome value of this visitor.
043     */
044    @Override
045    default T get() {
046        return null;
047    }
048
049    /**
050     * Enables to combine two visitors returned value ({@link RecordVisitor#get()}).
051     *
052     * @param t1 previous value, can be null.
053     * @param t2 current value
054     * @return the merged value of t1 and t2. By default it returns t1.
055     */
056    @Override
057    default T apply(final T t1, final T t2) {
058        return t1;
059    }
060
061    default void onInt(final Schema.Entry entry, final OptionalInt optionalInt) {
062        // no-op
063    }
064
065    default void onLong(final Schema.Entry entry, final OptionalLong optionalLong) {
066        // no-op
067    }
068
069    default void onFloat(final Schema.Entry entry, final OptionalDouble optionalFloat) {
070        // no-op
071    }
072
073    default void onDouble(final Schema.Entry entry, final OptionalDouble optionalDouble) {
074        // no-op
075    }
076
077    default void onBoolean(final Schema.Entry entry, final Optional<Boolean> optionalBoolean) {
078        // no-op
079    }
080
081    default void onString(final Schema.Entry entry, final Optional<String> string) {
082        // no-op
083    }
084
085    default void onObject(final Schema.Entry entry, final Optional<Object> object) {
086        // no-op
087    }
088
089    default void onDatetime(final Schema.Entry entry, final Optional<ZonedDateTime> dateTime) {
090        // no-op
091    }
092
093    default void onDecimal(final Schema.Entry entry, final Optional<BigDecimal> decimal) {
094        // no-op
095    }
096
097    default void onBytes(final Schema.Entry entry, final Optional<byte[]> bytes) {
098        // no-op
099    }
100
101    default RecordVisitor<T> onRecord(final Schema.Entry entry, final Optional<Record> record) {
102        return this;
103    }
104
105    default void onIntArray(final Schema.Entry entry, final Optional<Collection<Integer>> array) {
106        // no-op
107    }
108
109    default void onLongArray(final Schema.Entry entry, final Optional<Collection<Long>> array) {
110        // no-op
111    }
112
113    default void onFloatArray(final Schema.Entry entry, final Optional<Collection<Float>> array) {
114        // no-op
115    }
116
117    default void onDoubleArray(final Schema.Entry entry, final Optional<Collection<Double>> array) {
118        // no-op
119    }
120
121    default void onBooleanArray(final Schema.Entry entry, final Optional<Collection<Boolean>> array) {
122        // no-op
123    }
124
125    default void onStringArray(final Schema.Entry entry, final Optional<Collection<String>> array) {
126        // no-op
127    }
128
129    default void onDatetimeArray(final Schema.Entry entry, final Optional<Collection<ZonedDateTime>> array) {
130        // no-op
131    }
132
133    default void onDecimalArray(final Schema.Entry entry, final Optional<Collection<BigDecimal>> array) {
134        // no-op
135    }
136
137    default void onBytesArray(final Schema.Entry entry, final Optional<Collection<byte[]>> array) {
138        // no-op
139    }
140
141    default RecordVisitor<T> onRecordArray(final Schema.Entry entry, final Optional<Collection<Record>> array) {
142        return this;
143    }
144}