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