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