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.ecm.platform.importer.mqueues.mqueues;
020
021/**
022 * @since 9.1
023 */
024public class CQOffset implements Offset {
025    private final long offset;
026    private final int queue;
027
028    public CQOffset(int queue, long offset) {
029        this.queue = queue;
030        this.offset = offset;
031    }
032
033    public long getOffset() {
034        return offset;
035    }
036
037    public int getQueue() {
038        return queue;
039    }
040
041    @Override
042    public String toString() {
043        return String.format("CQOffset(%d, %d)", queue, offset);
044    }
045
046    @Override
047    public boolean equals(Object o) {
048        if (this == o) return true;
049        if (o == null || getClass() != o.getClass()) return false;
050
051        CQOffset cqOffset = (CQOffset) o;
052
053        if (queue != cqOffset.queue) return false;
054        return offset == cqOffset.offset;
055    }
056
057    @Override
058    public int hashCode() {
059        int result = (int) (offset ^ (offset >>> 32));
060        result = 31 * result + queue;
061        return result;
062    }
063
064    @Override
065    public int compareTo(Offset o) {
066        if (this == o) return 0;
067        if (o == null || getClass() != o.getClass()) {
068            throw new IllegalArgumentException("Can not compare offsets with different classes");
069        }
070        CQOffset cqOffset = (CQOffset) o;
071        if (queue != cqOffset.queue) {
072            throw new IllegalArgumentException("Can not compare offsets from different queues");
073        }
074        return (int) (offset - cqOffset.offset);
075    }
076}