All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deephaven.engine.table.impl.sources.BooleanAsByteColumnSource Maven / Gradle / Ivy

There is a newer version: 0.37.1
Show newest version
/**
 * Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
 */
package io.deephaven.engine.table.impl.sources;

import io.deephaven.engine.table.SharedContext;
import io.deephaven.engine.rowset.RowSequence;
import io.deephaven.engine.table.ColumnSource;
import io.deephaven.engine.table.impl.AbstractColumnSource;
import io.deephaven.engine.table.impl.MutableColumnSourceGetDefaults;
import io.deephaven.util.BooleanUtils;
import io.deephaven.chunk.*;
import io.deephaven.chunk.attributes.Values;
import org.jetbrains.annotations.NotNull;

/**
 * Reinterpret result {@link ColumnSource} implementations that translates {@link Boolean} to {@code byte} values.
 */
public class BooleanAsByteColumnSource extends AbstractColumnSource implements MutableColumnSourceGetDefaults.ForByte {

    private final ColumnSource alternateColumnSource;

    public BooleanAsByteColumnSource(@NotNull final ColumnSource alternateColumnSource) {
        super(byte.class);
        this.alternateColumnSource = alternateColumnSource;
    }

    @Override
    public byte getByte(final long rowKey) {
        return BooleanUtils.booleanAsByte(alternateColumnSource.get(rowKey));
    }

    @Override
    public byte getPrevByte(final long rowKey) {
        return BooleanUtils.booleanAsByte(alternateColumnSource.getPrev(rowKey));
    }

    @Override
    public boolean isImmutable() {
        return alternateColumnSource.isImmutable();
    }

    @Override
    public  boolean allowsReinterpret(@NotNull final Class alternateDataType) {
        return alternateDataType == Boolean.class;
    }

    @Override
    public  ColumnSource doReinterpret(@NotNull final Class alternateDataType) throws IllegalArgumentException {
        //noinspection unchecked
        return (ColumnSource) alternateColumnSource;
    }

    private class UnboxedFillContext implements FillContext {
        final GetContext alternateGetContext;

        private UnboxedFillContext(final int chunkCapacity, final SharedContext sharedContext) {
            alternateGetContext = alternateColumnSource.makeGetContext(chunkCapacity, sharedContext);
        }

        @Override
        public void close() {
            alternateGetContext.close();
        }
    }

    @Override
    public FillContext makeFillContext(final int chunkCapacity, final SharedContext sharedContext) {
        return new UnboxedFillContext(chunkCapacity, sharedContext);
    }

    @Override
    public void fillChunk(@NotNull final FillContext context, @NotNull final WritableChunk destination, @NotNull final RowSequence rowSequence) {
        final UnboxedFillContext unboxedFillContext = (UnboxedFillContext) context;
        final ObjectChunk booleanObjectChunk = alternateColumnSource.getChunk(unboxedFillContext.alternateGetContext, rowSequence).asObjectChunk();
        convertToByte(destination, booleanObjectChunk);
    }

    @Override
    public void fillPrevChunk(@NotNull final FillContext context, @NotNull final WritableChunk destination, @NotNull final RowSequence rowSequence) {
        final UnboxedFillContext unboxedFillContext = (UnboxedFillContext) context;
        final ObjectChunk booleanObjectChunk = alternateColumnSource.getPrevChunk(unboxedFillContext.alternateGetContext, rowSequence).asObjectChunk();
        convertToByte(destination, booleanObjectChunk);
    }

    private static void convertToByte(@NotNull final WritableChunk destination, @NotNull final ObjectChunk booleanObjectChunk) {
        final WritableByteChunk byteDestination = destination.asWritableByteChunk();
        for (int ii = 0; ii < booleanObjectChunk.size(); ++ii) {
            byteDestination.set(ii, BooleanUtils.booleanAsByte(booleanObjectChunk.get(ii)));
        }
        byteDestination.setSize(booleanObjectChunk.size());
    }

    @Override
    public boolean isStateless() {
        return alternateColumnSource.isStateless();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy