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;
020
021/**
022 * Represent the number of messages between 2 offsets
023 *
024 * @since 9.3
025 */
026public class LogLag {
027    final protected long lowerOffset;
028
029    final protected long upperOffset;
030
031    final protected long lag;
032
033    final protected long upper;
034
035    public LogLag(long lowerOffset, long upperOffset) {
036        this(lowerOffset, upperOffset, upperOffset - lowerOffset, upperOffset);
037    }
038
039    public LogLag(long lowerOffset, long upperOffset, long lag, long upper) {
040        this.lowerOffset = lowerOffset;
041        this.upperOffset = upperOffset;
042        this.upper = upper;
043        this.lag = lag;
044    }
045
046    public static LogLag of(long lowerOffset, long upperOffset) {
047        return new LogLag(lowerOffset, upperOffset);
048    }
049
050    public static LogLag of(long lag) {
051        return new LogLag(0, lag, lag, lag);
052    }
053
054    /**
055     * Returns the number of messages between lower and upper offsets.
056     */
057    public long lag() {
058        return lag;
059    }
060
061    /**
062     * Convert the upperOffset into a number of messages.
063     */
064    public long upper() {
065        return upper;
066    }
067
068    /**
069     * Convert the lowerOffset into a number of messages.
070     */
071    public long lower() {
072        return upper - lag;
073    }
074
075    public long upperOffset() {
076        return upperOffset;
077    }
078
079    public long lowerOffset() {
080        return lowerOffset;
081    }
082
083    @Override
084    public String toString() {
085        return "LogLag{" + "lower=" + lower() + ", upper=" + upper + ", lag=" + lag + ", lowerOffset=" + lowerOffset
086                + ", upperOffset=" + upperOffset + '}';
087    }
088
089    @Override
090    public boolean equals(Object o) {
091        if (this == o)
092            return true;
093        if (o == null || getClass() != o.getClass())
094            return false;
095
096        LogLag lag1 = (LogLag) o;
097
098        return lag == lag1.lag;
099    }
100
101    @Override
102    public int hashCode() {
103        return (int) (lag ^ (lag >>> 32));
104    }
105
106}