001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.log.internals;
020
021import org.nuxeo.lib.stream.log.LogOffset;
022import org.nuxeo.lib.stream.log.LogPartition;
023import org.nuxeo.lib.stream.log.Name;
024
025/**
026 * @since 9.3
027 */
028public class LogOffsetImpl implements LogOffset {
029    protected final LogPartition partition;
030
031    protected final long offset;
032
033    public LogOffsetImpl(LogPartition partition, long offset) {
034        this(partition.name(), partition.partition(), offset);
035    }
036
037    public LogOffsetImpl(Name name, int partition, long offset) {
038        this.partition = LogPartition.of(name, partition);
039        this.offset = offset;
040    }
041
042    @Override
043    public LogPartition partition() {
044        return partition;
045    }
046
047    @Override
048    public long offset() {
049        return offset;
050    }
051
052    @Override
053    public LogOffset nextOffset() {
054        return new LogOffsetImpl(partition, offset + 1);
055    }
056
057    @Override
058    public String toString() {
059        return String.format("%s:+%d", partition, offset);
060    }
061
062    @Override
063    public boolean equals(Object o) {
064        if (this == o)
065            return true;
066        if (o == null || getClass() != o.getClass())
067            return false;
068
069        LogOffsetImpl offsetImpl = (LogOffsetImpl) o;
070
071        return partition.equals(offsetImpl.partition) && offset == offsetImpl.offset;
072    }
073
074    @Override
075    public int hashCode() {
076        int result = partition != null ? partition.hashCode() : 0;
077        result = 31 * result + (int) (offset ^ (offset >>> 32));
078        return result;
079    }
080
081    @SuppressWarnings("NullableProblems")
082    @Override
083    public int compareTo(LogOffset o) {
084        if (this == o)
085            return 0;
086        if (o == null || getClass() != o.getClass()) {
087            throw new IllegalArgumentException("Cannot compare offsets with different classes");
088        }
089        LogOffsetImpl offsetImpl = (LogOffsetImpl) o;
090        if (partition.equals(offsetImpl.partition)) {
091            throw new IllegalArgumentException("Cannot compare offsets from different partitions");
092        }
093        return Long.compare(offset, offsetImpl.offset);
094    }
095}