/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.auron.flink.arrow.writers;

import org.apache.arrow.vector.IntVector;
import org.apache.flink.table.data.ArrayData;
import org.apache.flink.table.data.RowData;

/**
 * {@link ArrowFieldWriter} for 32-bit integers ({@link IntVector}).
 *
 * <p>Use {@link #forRow(IntVector)} when writing from {@link RowData} and {@link
 * #forArray(IntVector)} when writing from {@link ArrayData}.
 *
 * @param <T> the input data type
 */
public abstract class IntWriter<T> extends ArrowFieldWriter<T> {

    /** Creates an IntWriter that reads from {@link RowData}. */
    public static IntWriter<RowData> forRow(IntVector intVector) {
        return new IntWriterForRow(intVector);
    }

    /** Creates an IntWriter that reads from {@link ArrayData}. */
    public static IntWriter<ArrayData> forArray(IntVector intVector) {
        return new IntWriterForArray(intVector);
    }

    private IntWriter(IntVector intVector) {
        super(intVector);
    }

    abstract boolean isNullAt(T in, int ordinal);

    abstract int readInt(T in, int ordinal);

    @Override
    public void doWrite(T in, int ordinal) {
        IntVector vector = (IntVector) getValueVector();
        if (isNullAt(in, ordinal)) {
            vector.setNull(getCount());
        } else {
            vector.setSafe(getCount(), readInt(in, ordinal));
        }
    }

    // ---- Inner classes for RowData and ArrayData ----

    public static final class IntWriterForRow extends IntWriter<RowData> {
        private IntWriterForRow(IntVector intVector) {
            super(intVector);
        }

        @Override
        boolean isNullAt(RowData in, int ordinal) {
            return in.isNullAt(ordinal);
        }

        @Override
        int readInt(RowData in, int ordinal) {
            return in.getInt(ordinal);
        }
    }

    public static final class IntWriterForArray extends IntWriter<ArrayData> {
        private IntWriterForArray(IntVector intVector) {
            super(intVector);
        }

        @Override
        boolean isNullAt(ArrayData in, int ordinal) {
            return in.isNullAt(ordinal);
        }

        @Override
        int readInt(ArrayData in, int ordinal) {
            return in.getInt(ordinal);
        }
    }
}
