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