[Performance] Optimize PositionImpl toString, compareTo and hashCode
minimize object allocations
*/
package org.apache.bookkeeper.mledger.impl;
-import static com.google.common.base.Preconditions.checkNotNull;
-import com.google.common.base.Objects;
-import com.google.common.collect.ComparisonChain;
import org.apache.bookkeeper.mledger.Position;
import org.apache.bookkeeper.mledger.proto.MLDataFormats.NestedPositionInfo;
import org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo;
@@ -102,20 +99,27 @@ public class PositionImpl implements Position, Comparable<PositionImpl> {
*/
@Override
public String toString() {
- return String.format("%d:%d", ledgerId, entryId);
+ return ledgerId + ":" + entryId;
}
@Override
- public int compareTo(PositionImpl other) {
- checkNotNull(other);
+ public int compareTo(PositionImpl that) {
+ if (this.ledgerId != that.ledgerId) {
+ return (this.ledgerId < that.ledgerId ? -1 : 1);
+ }
+
+ if (this.entryId != that.entryId) {
+ return (this.entryId < that.entryId ? -1 : 1);
+ }
- return ComparisonChain.start().compare(this.ledgerId, other.ledgerId).compare(this.entryId, other.entryId)
- .result();
+ return 0;
}
@Override
public int hashCode() {
- return Objects.hashCode(ledgerId, entryId);
+ int result = (int) (ledgerId ^ (ledgerId >>> 32));
+ result = 31 * result + (int) (entryId ^ (entryId >>> 32));
+ return result;
}
@Override
