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

com.kolibrifx.plovercrest.server.streams.ReaderPosition Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
 */

package com.kolibrifx.plovercrest.server.streams;

import com.kolibrifx.common.Safety;

public final class ReaderPosition {
    public enum PositionType {
        TIMESTAMP,
        INDEX,
    }

    private final PositionType positionType;
    private final long timestampOrIndex;
    private final int skipCount;

    private ReaderPosition(final PositionType positionType, final long timestampOrIndex, final int skipCount) {
        this.positionType = positionType;
        this.timestampOrIndex = timestampOrIndex;
        this.skipCount = skipCount;
        Safety.argumentNotNull("positionType", positionType);
        Safety.argumentNonNegative("timestampOrIndex", timestampOrIndex);
        Safety.argumentNonNegative("skipCount", skipCount);
    }

    public long getTimestampOrIndex() {
        return timestampOrIndex;
    }

    public int getSkipCount() {
        return skipCount;
    }

    public PositionType getPositionType() {
        return positionType;
    }

    @Override
    public String toString() {
        return "ReaderPosition [positionType=" + positionType + ", timestampOrIndex=" + timestampOrIndex
                + ", skipCount=" + skipCount + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + positionType.hashCode();
        result = prime * result + skipCount;
        result = prime * result + (int) (timestampOrIndex ^ (timestampOrIndex >>> 32));
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ReaderPosition other = (ReaderPosition) obj;
        if (positionType != other.positionType) {
            return false;
        }
        if (skipCount != other.skipCount) {
            return false;
        }
        if (timestampOrIndex != other.timestampOrIndex) {
            return false;
        }
        return true;
    }

    public static ReaderPosition createIndexPosition(final long index) {
        return new ReaderPosition(PositionType.INDEX, index, 0);
    }

    public static ReaderPosition createTimestampPosition(final long timestamp, final int skipCount) {
        return new ReaderPosition(PositionType.TIMESTAMP, timestamp, skipCount);
    }

    /**
     * This function is useful during deserialization, most user code should call either
     * {@link #createIndexPosition(long)} or {@link #createTimestampPosition(long, int)} instead.
     */
    public static ReaderPosition create(final PositionType type, final long timestampOrIndex, final int skipCount) {
        return new ReaderPosition(type, timestampOrIndex, skipCount);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy