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; 023 024/** 025 * @since 9.3 026 */ 027public class LogOffsetImpl implements LogOffset { 028 protected final LogPartition partition; 029 030 protected final long offset; 031 032 public LogOffsetImpl(LogPartition partition, long offset) { 033 this.partition = partition; 034 this.offset = offset; 035 } 036 037 public LogOffsetImpl(String 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 String toString() { 054 return String.format("%s:+%d", partition, offset); 055 } 056 057 @Override 058 public boolean equals(Object o) { 059 if (this == o) 060 return true; 061 if (o == null || getClass() != o.getClass()) 062 return false; 063 064 LogOffsetImpl offsetImpl = (LogOffsetImpl) o; 065 066 return partition.equals(offsetImpl.partition) && offset == offsetImpl.offset; 067 } 068 069 @Override 070 public int hashCode() { 071 int result = partition != null ? partition.hashCode() : 0; 072 result = 31 * result + (int) (offset ^ (offset >>> 32)); 073 return result; 074 } 075 076 @SuppressWarnings("NullableProblems") 077 @Override 078 public int compareTo(LogOffset o) { 079 if (this == o) 080 return 0; 081 if (o == null || getClass() != o.getClass()) { 082 throw new IllegalArgumentException("Cannot compare offsets with different classes"); 083 } 084 LogOffsetImpl offsetImpl = (LogOffsetImpl) o; 085 if (partition.equals(offsetImpl.partition)) { 086 throw new IllegalArgumentException("Cannot compare offsets from different partitions"); 087 } 088 return Long.compare(offset, offsetImpl.offset); 089 } 090}