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

io.deephaven.engine.table.impl.sources.immutable.ImmutableConstantNanosBasedTimeSource Maven / Gradle / Ivy

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

import io.deephaven.base.verify.Require;
import io.deephaven.chunk.LongChunk;
import io.deephaven.chunk.WritableChunk;
import io.deephaven.chunk.WritableObjectChunk;
import io.deephaven.chunk.attributes.Values;
import io.deephaven.engine.rowset.RowSequence;
import io.deephaven.engine.rowset.chunkattributes.RowKeys;
import io.deephaven.engine.table.ColumnSource;
import io.deephaven.engine.table.impl.AbstractColumnSource;
import io.deephaven.engine.table.impl.sources.*;
import io.deephaven.engine.table.impl.util.ShiftData;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public abstract class ImmutableConstantNanosBasedTimeSource extends AbstractColumnSource
        implements ShiftData.ShiftCallback, InMemoryColumnSource, RowKeyAgnosticChunkSource,
        ConvertibleTimeSource {

    protected final ImmutableConstantLongSource nanoSource;

    // region constructor
    public ImmutableConstantNanosBasedTimeSource(
            @NotNull final Class type,
            final ImmutableConstantLongSource nanoSource) {
        super(type);
        this.nanoSource = nanoSource;
    }
    // endregion constructor

    // region Getters & Setters
    protected abstract TIME_TYPE makeValue(final long nanos);

    protected abstract long toNanos(final TIME_TYPE value);

    @Override
    public TIME_TYPE get(long rowKey) {
        return makeValue(getLong(rowKey));
    }

    @Override
    public TIME_TYPE getPrev(long rowKey) {
        return makeValue(getPrevLong(rowKey));
    }

    @Override
    public long getLong(long rowKey) {
        return nanoSource.getLong(rowKey);
    }

    @Override
    public long getPrevLong(long rowKey) {
        return nanoSource.getPrevLong(rowKey);
    }

    @Override
    public final void shift(final long start, final long end, final long offset) {}
    // endregion

    // region Chunking
    @Override
    public final void fillChunk(
            @NotNull final FillContext context,
            @NotNull final WritableChunk destination,
            @NotNull final RowSequence rowSequence) {
        final int size = rowSequence.intSize();
        final TIME_TYPE value = get(0);
        destination.setSize(size);
        destination.asWritableObjectChunk().fillWithValue(0, size, value);
    }

    @Override
    public final void fillPrevChunk(
            @NotNull final FillContext context,
            @NotNull final WritableChunk destination,
            @NotNull final RowSequence rowSequence) {
        fillChunk(context, destination, rowSequence);
    }

    @Override
    public void fillChunkUnordered(
            @NotNull FillContext context,
            @NotNull WritableChunk dest,
            @NotNull LongChunk keys) {
        final WritableObjectChunk destChunk = dest.asWritableObjectChunk();
        final TIME_TYPE value = get(0);
        for (int ii = 0; ii < keys.size(); ++ii) {
            destChunk.set(ii, keys.get(ii) == RowSequence.NULL_ROW_KEY ? null : value);
        }
        destChunk.setSize(keys.size());
    }

    @Override
    public void fillPrevChunkUnordered(
            @NotNull FillContext context,
            @NotNull WritableChunk dest,
            @NotNull LongChunk keys) {
        fillChunkUnordered(context, dest, keys);
    }

    @Override
    public boolean providesFillUnordered() {
        return true;
    }
    // endregion Chunking


    // region Reinterpretation
    @Override
    public  boolean allowsReinterpret(
            @NotNull final Class alternateDataType) {
        return alternateDataType == long.class || alternateDataType == Instant.class;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected  ColumnSource doReinterpret(
            @NotNull Class alternateDataType) {
        if (alternateDataType == this.getType()) {
            return (ColumnSource) this;
        } else if (alternateDataType == long.class || alternateDataType == Long.class) {
            return (ColumnSource) toEpochNano();
        } else if (alternateDataType == Instant.class) {
            return (ColumnSource) toInstant();
        }

        throw new IllegalArgumentException(
                "Cannot reinterpret `" + getType().getName() + "` to `" + alternateDataType.getName() + "`");
    }

    @Override
    public boolean supportsTimeConversion() {
        return true;
    }

    @Override
    public ColumnSource toZonedDateTime(@NotNull final ZoneId zone) {
        return new ImmutableConstantZonedDateTimeSource(Require.neqNull(zone, "zone"), nanoSource);
    }

    @Override
    public ColumnSource toLocalDate(@NotNull final ZoneId zone) {
        return new LongAsLocalDateColumnSource(nanoSource, zone);
    }

    @Override
    public ColumnSource toLocalTime(@NotNull final ZoneId zone) {
        return new LongAsLocalTimeColumnSource(nanoSource, zone);
    }

    @Override
    public ColumnSource toInstant() {
        return new ImmutableConstantInstantSource(nanoSource);
    }

    @Override
    public ColumnSource toEpochNano() {
        return nanoSource;
    }
    // endregion Reinterpretation
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy