You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2021/03/04 06:30:59 UTC

[incubator-tuweni] branch master updated: Wire up Delegate Bytes equals and hashcode

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new 2edba86  Wire up Delegate Bytes equals and hashcode
     new e98b2e1  Merge pull request #198 from shemnon/delegateEquals
2edba86 is described below

commit 2edba86414b02cdfafe3fc2f7d5e66faf5ba98c3
Author: Danno Ferrin <da...@gmail.com>
AuthorDate: Wed Mar 3 13:35:32 2021 -0700

    Wire up Delegate Bytes equals and hashcode
    
    * wire up equals and hashCode directly to the delegate
    * refactor the delegate classes to reduce code duplication
    
    In Besu this lack of delegation accounts for ~20% of bonsai import time.
    
    Signed-off-by: Danno Ferrin <da...@gmail.com>
---
 .../org/apache/tuweni/bytes/DelegatingBytes.java   |  13 +-
 .../org/apache/tuweni/bytes/DelegatingBytes32.java |  16 +-
 .../org/apache/tuweni/bytes/DelegatingBytes48.java |  16 +-
 ...bleBytes48.java => DelegatingMutableBytes.java} |  27 +--
 .../tuweni/bytes/DelegatingMutableBytes32.java     | 244 +-------------------
 .../tuweni/bytes/DelegatingMutableBytes48.java     | 245 +--------------------
 6 files changed, 34 insertions(+), 527 deletions(-)

diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes.java b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes.java
index e8d9376..3774610 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes.java
@@ -22,7 +22,7 @@ package org.apache.tuweni.bytes;
  */
 public class DelegatingBytes extends AbstractBytes implements Bytes {
 
-  private final Bytes delegate;
+  final Bytes delegate;
 
   protected DelegatingBytes(Bytes delegate) {
     this.delegate = delegate;
@@ -52,4 +52,15 @@ public class DelegatingBytes extends AbstractBytes implements Bytes {
   public MutableBytes mutableCopy() {
     return MutableBytes.wrap(toArray());
   }
+
+  @Override
+  public boolean equals(final Object o) {
+    return delegate.equals(o);
+  }
+
+  @Override
+  public int hashCode() {
+    return delegate.hashCode();
+  }
+
 }
diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes32.java b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes32.java
index 999b8b3..14832cb 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes32.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes32.java
@@ -20,12 +20,10 @@ package org.apache.tuweni.bytes;
  * <p>
  * This class may be used to create more types that represent 32 bytes, but need a different name for business logic.
  */
-public class DelegatingBytes32 extends AbstractBytes implements Bytes32 {
-
-  private final Bytes delegate;
+public class DelegatingBytes32 extends DelegatingBytes implements Bytes32 {
 
   protected DelegatingBytes32(Bytes delegate) {
-    this.delegate = delegate;
+    super(delegate);
   }
 
   @Override
@@ -34,16 +32,6 @@ public class DelegatingBytes32 extends AbstractBytes implements Bytes32 {
   }
 
   @Override
-  public byte get(int i) {
-    return delegate.get(i);
-  }
-
-  @Override
-  public Bytes slice(int index, int length) {
-    return delegate.slice(index, length);
-  }
-
-  @Override
   public Bytes32 copy() {
     return Bytes32.wrap(toArray());
   }
diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes48.java b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes48.java
index 497b4e6..de4b295 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes48.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingBytes48.java
@@ -20,12 +20,10 @@ package org.apache.tuweni.bytes;
  * <p>
  * This class may be used to create more types that represent 48 bytes, but need a different name for business logic.
  */
-public class DelegatingBytes48 extends AbstractBytes implements Bytes48 {
-
-  private final Bytes delegate;
+public class DelegatingBytes48 extends DelegatingBytes implements Bytes48 {
 
   protected DelegatingBytes48(Bytes delegate) {
-    this.delegate = delegate;
+    super(delegate);
   }
 
   @Override
@@ -34,16 +32,6 @@ public class DelegatingBytes48 extends AbstractBytes implements Bytes48 {
   }
 
   @Override
-  public byte get(int i) {
-    return delegate.get(i);
-  }
-
-  @Override
-  public Bytes slice(int index, int length) {
-    return delegate.slice(index, length);
-  }
-
-  @Override
   public Bytes48 copy() {
     return Bytes48.wrap(toArray());
   }
diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes48.java b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes.java
similarity index 90%
copy from bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes48.java
copy to bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes.java
index 0086494..318b6bb 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes48.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes.java
@@ -12,7 +12,6 @@
  */
 package org.apache.tuweni.bytes;
 
-import static com.google.common.base.Preconditions.checkArgument;
 
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
@@ -20,17 +19,19 @@ import java.security.MessageDigest;
 
 import io.vertx.core.buffer.Buffer;
 
-final class DelegatingMutableBytes48 implements MutableBytes48 {
 
-  private final MutableBytes delegate;
+/**
+ * A class that holds and delegates all operations to its inner bytes field.
+ *
+ * <p>
+ * This class may be used to create more types that represent bytes, but need a different name for business logic.
+ */
+public class DelegatingMutableBytes implements MutableBytes {
 
-  private DelegatingMutableBytes48(MutableBytes delegate) {
-    this.delegate = delegate;
-  }
+  final MutableBytes delegate;
 
-  static MutableBytes48 delegateTo(MutableBytes value) {
-    checkArgument(value.size() == SIZE, "Expected %s bytes but got %s", SIZE, value.size());
-    return new DelegatingMutableBytes48(value);
+  protected DelegatingMutableBytes(MutableBytes delegate) {
+    this.delegate = delegate;
   }
 
   @Override
@@ -199,13 +200,13 @@ final class DelegatingMutableBytes48 implements MutableBytes48 {
   }
 
   @Override
-  public Bytes48 copy() {
-    return Bytes48.wrap(delegate.toArray());
+  public Bytes copy() {
+    return Bytes.wrap(delegate.toArray());
   }
 
   @Override
-  public MutableBytes48 mutableCopy() {
-    return MutableBytes48.wrap(delegate.toArray());
+  public MutableBytes mutableCopy() {
+    return MutableBytes.wrap(delegate.toArray());
   }
 
   @Override
diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes32.java b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes32.java
index dbfec11..6a3a560 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes32.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes32.java
@@ -14,18 +14,12 @@ package org.apache.tuweni.bytes;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
-import java.math.BigInteger;
-import java.nio.ByteBuffer;
-import java.security.MessageDigest;
 
-import io.vertx.core.buffer.Buffer;
 
-final class DelegatingMutableBytes32 implements MutableBytes32 {
-
-  private final MutableBytes delegate;
+final class DelegatingMutableBytes32 extends DelegatingMutableBytes implements MutableBytes32 {
 
   private DelegatingMutableBytes32(MutableBytes delegate) {
-    this.delegate = delegate;
+    super(delegate);
   }
 
   static MutableBytes32 delegateTo(MutableBytes value) {
@@ -34,171 +28,6 @@ final class DelegatingMutableBytes32 implements MutableBytes32 {
   }
 
   @Override
-  public void set(int i, byte b) {
-    delegate.set(i, b);
-  }
-
-  @Override
-  public void setInt(int i, int value) {
-    delegate.setInt(i, value);
-  }
-
-  @Override
-  public void setLong(int i, long value) {
-    delegate.setLong(i, value);
-  }
-
-  @Override
-  public MutableBytes increment() {
-    return delegate.increment();
-  }
-
-  @Override
-  public MutableBytes decrement() {
-    return delegate.decrement();
-  }
-
-  @Override
-  public MutableBytes mutableSlice(int i, int length) {
-    return delegate.mutableSlice(i, length);
-  }
-
-  @Override
-  public void fill(byte b) {
-    delegate.fill(b);
-  }
-
-  @Override
-  public void clear() {
-    delegate.clear();
-  }
-
-  @Override
-  public int size() {
-    return delegate.size();
-  }
-
-  @Override
-  public byte get(int i) {
-    return delegate.get(i);
-  }
-
-  @Override
-  public int getInt(int i) {
-    return delegate.getInt(i);
-  }
-
-  @Override
-  public int toInt() {
-    return delegate.toInt();
-  }
-
-  @Override
-  public long getLong(int i) {
-    return delegate.getLong(i);
-  }
-
-  @Override
-  public long toLong() {
-    return delegate.toLong();
-  }
-
-  @Override
-  public BigInteger toBigInteger() {
-    return delegate.toBigInteger();
-  }
-
-  @Override
-  public BigInteger toUnsignedBigInteger() {
-    return delegate.toUnsignedBigInteger();
-  }
-
-  @Override
-  public boolean isZero() {
-    return delegate.isZero();
-  }
-
-  @Override
-  public int numberOfLeadingZeros() {
-    return delegate.numberOfLeadingZeros();
-  }
-
-  @Override
-  public int numberOfLeadingZeroBytes() {
-    return delegate.numberOfLeadingZeroBytes();
-  }
-
-  @Override
-  public boolean hasLeadingZeroByte() {
-    return delegate.hasLeadingZeroByte();
-  }
-
-  @Override
-  public boolean hasLeadingZero() {
-    return delegate.hasLeadingZero();
-  }
-
-  @Override
-  public int bitLength() {
-    return delegate.bitLength();
-  }
-
-  @Override
-  public Bytes and(Bytes other) {
-    return delegate.and(other);
-  }
-
-  @Override
-  public <T extends MutableBytes> T and(Bytes other, T result) {
-    return delegate.and(other, result);
-  }
-
-  @Override
-  public Bytes or(Bytes other) {
-    return delegate.or(other);
-  }
-
-  @Override
-  public <T extends MutableBytes> T or(Bytes other, T result) {
-    return delegate.or(other, result);
-  }
-
-  @Override
-  public Bytes xor(Bytes other) {
-    return delegate.xor(other);
-  }
-
-  @Override
-  public <T extends MutableBytes> T xor(Bytes other, T result) {
-    return delegate.xor(other, result);
-  }
-
-  @Override
-  public <T extends MutableBytes> T not(T result) {
-    return delegate.not(result);
-  }
-
-  @Override
-  public <T extends MutableBytes> T shiftRight(int distance, T result) {
-    return delegate.shiftRight(distance, result);
-  }
-
-  @Override
-  public <T extends MutableBytes> T shiftLeft(int distance, T result) {
-    return delegate.shiftLeft(distance, result);
-  }
-
-  @Override
-  public Bytes slice(int index) {
-    return delegate.slice(index);
-  }
-
-  @Override
-  public Bytes slice(int index, int length) {
-    return delegate.slice(index, length);
-  }
-
-  @Override
   public Bytes32 copy() {
     return Bytes32.wrap(delegate.toArray());
   }
@@ -208,73 +37,4 @@ final class DelegatingMutableBytes32 implements MutableBytes32 {
     return MutableBytes32.wrap(delegate.toArray());
   }
 
-  @Override
-  public void copyTo(MutableBytes destination) {
-    delegate.copyTo(destination);
-  }
-
-  @Override
-  public void copyTo(MutableBytes destination, int destinationOffset) {
-    delegate.copyTo(destination, destinationOffset);
-  }
-
-  @Override
-  public void appendTo(ByteBuffer byteBuffer) {
-    delegate.appendTo(byteBuffer);
-  }
-
-  @Override
-  public void appendTo(Buffer buffer) {
-    delegate.appendTo(buffer);
-  }
-
-  @Override
-  public int commonPrefixLength(Bytes other) {
-    return delegate.commonPrefixLength(other);
-  }
-
-  @Override
-  public Bytes commonPrefix(Bytes other) {
-    return delegate.commonPrefix(other);
-  }
-
-  @Override
-  public void update(MessageDigest digest) {
-    delegate.update(digest);
-  }
-
-  @Override
-  public byte[] toArray() {
-    return delegate.toArray();
-  }
-
-  @Override
-  public byte[] toArrayUnsafe() {
-    return delegate.toArrayUnsafe();
-  }
-
-  @Override
-  public String toString() {
-    return delegate.toString();
-  }
-
-  @Override
-  public String toHexString() {
-    return delegate.toHexString();
-  }
-
-  @Override
-  public String toShortHexString() {
-    return delegate.toShortHexString();
-  }
-
-  @Override
-  public boolean equals(Object obj) {
-    return delegate.equals(obj);
-  }
-
-  @Override
-  public int hashCode() {
-    return delegate.hashCode();
-  }
 }
diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes48.java b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes48.java
index 0086494..831c4fa 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes48.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/DelegatingMutableBytes48.java
@@ -14,18 +14,12 @@ package org.apache.tuweni.bytes;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
-import java.math.BigInteger;
-import java.nio.ByteBuffer;
-import java.security.MessageDigest;
 
-import io.vertx.core.buffer.Buffer;
 
-final class DelegatingMutableBytes48 implements MutableBytes48 {
-
-  private final MutableBytes delegate;
+final class DelegatingMutableBytes48 extends DelegatingMutableBytes implements MutableBytes48 {
 
   private DelegatingMutableBytes48(MutableBytes delegate) {
-    this.delegate = delegate;
+    super(delegate);
   }
 
   static MutableBytes48 delegateTo(MutableBytes value) {
@@ -34,171 +28,6 @@ final class DelegatingMutableBytes48 implements MutableBytes48 {
   }
 
   @Override
-  public void set(int i, byte b) {
-    delegate.set(i, b);
-  }
-
-  @Override
-  public void setInt(int i, int value) {
-    delegate.setInt(i, value);
-  }
-
-  @Override
-  public void setLong(int i, long value) {
-    delegate.setLong(i, value);
-  }
-
-  @Override
-  public MutableBytes increment() {
-    return delegate.increment();
-  }
-
-  @Override
-  public MutableBytes decrement() {
-    return delegate.decrement();
-  }
-
-  @Override
-  public MutableBytes mutableSlice(int i, int length) {
-    return delegate.mutableSlice(i, length);
-  }
-
-  @Override
-  public void fill(byte b) {
-    delegate.fill(b);
-  }
-
-  @Override
-  public void clear() {
-    delegate.clear();
-  }
-
-  @Override
-  public int size() {
-    return delegate.size();
-  }
-
-  @Override
-  public byte get(int i) {
-    return delegate.get(i);
-  }
-
-  @Override
-  public int getInt(int i) {
-    return delegate.getInt(i);
-  }
-
-  @Override
-  public int toInt() {
-    return delegate.toInt();
-  }
-
-  @Override
-  public long getLong(int i) {
-    return delegate.getLong(i);
-  }
-
-  @Override
-  public long toLong() {
-    return delegate.toLong();
-  }
-
-  @Override
-  public BigInteger toBigInteger() {
-    return delegate.toBigInteger();
-  }
-
-  @Override
-  public BigInteger toUnsignedBigInteger() {
-    return delegate.toUnsignedBigInteger();
-  }
-
-  @Override
-  public boolean isZero() {
-    return delegate.isZero();
-  }
-
-  @Override
-  public int numberOfLeadingZeros() {
-    return delegate.numberOfLeadingZeros();
-  }
-
-  @Override
-  public int numberOfLeadingZeroBytes() {
-    return delegate.numberOfLeadingZeroBytes();
-  }
-
-  @Override
-  public boolean hasLeadingZeroByte() {
-    return delegate.hasLeadingZeroByte();
-  }
-
-  @Override
-  public boolean hasLeadingZero() {
-    return delegate.hasLeadingZero();
-  }
-
-  @Override
-  public int bitLength() {
-    return delegate.bitLength();
-  }
-
-  @Override
-  public Bytes and(Bytes other) {
-    return delegate.and(other);
-  }
-
-  @Override
-  public <T extends MutableBytes> T and(Bytes other, T result) {
-    return delegate.and(other, result);
-  }
-
-  @Override
-  public Bytes or(Bytes other) {
-    return delegate.or(other);
-  }
-
-  @Override
-  public <T extends MutableBytes> T or(Bytes other, T result) {
-    return delegate.or(other, result);
-  }
-
-  @Override
-  public Bytes xor(Bytes other) {
-    return delegate.xor(other);
-  }
-
-  @Override
-  public <T extends MutableBytes> T xor(Bytes other, T result) {
-    return delegate.xor(other, result);
-  }
-
-  @Override
-  public <T extends MutableBytes> T not(T result) {
-    return delegate.not(result);
-  }
-
-  @Override
-  public <T extends MutableBytes> T shiftRight(int distance, T result) {
-    return delegate.shiftRight(distance, result);
-  }
-
-  @Override
-  public <T extends MutableBytes> T shiftLeft(int distance, T result) {
-    return delegate.shiftLeft(distance, result);
-  }
-
-  @Override
-  public Bytes slice(int index) {
-    return delegate.slice(index);
-  }
-
-  @Override
-  public Bytes slice(int index, int length) {
-    return delegate.slice(index, length);
-  }
-
-  @Override
   public Bytes48 copy() {
     return Bytes48.wrap(delegate.toArray());
   }
@@ -207,74 +36,4 @@ final class DelegatingMutableBytes48 implements MutableBytes48 {
   public MutableBytes48 mutableCopy() {
     return MutableBytes48.wrap(delegate.toArray());
   }
-
-  @Override
-  public void copyTo(MutableBytes destination) {
-    delegate.copyTo(destination);
-  }
-
-  @Override
-  public void copyTo(MutableBytes destination, int destinationOffset) {
-    delegate.copyTo(destination, destinationOffset);
-  }
-
-  @Override
-  public void appendTo(ByteBuffer byteBuffer) {
-    delegate.appendTo(byteBuffer);
-  }
-
-  @Override
-  public void appendTo(Buffer buffer) {
-    delegate.appendTo(buffer);
-  }
-
-  @Override
-  public int commonPrefixLength(Bytes other) {
-    return delegate.commonPrefixLength(other);
-  }
-
-  @Override
-  public Bytes commonPrefix(Bytes other) {
-    return delegate.commonPrefix(other);
-  }
-
-  @Override
-  public void update(MessageDigest digest) {
-    delegate.update(digest);
-  }
-
-  @Override
-  public byte[] toArray() {
-    return delegate.toArray();
-  }
-
-  @Override
-  public byte[] toArrayUnsafe() {
-    return delegate.toArrayUnsafe();
-  }
-
-  @Override
-  public String toString() {
-    return delegate.toString();
-  }
-
-  @Override
-  public String toHexString() {
-    return delegate.toHexString();
-  }
-
-  @Override
-  public String toShortHexString() {
-    return delegate.toShortHexString();
-  }
-
-  @Override
-  public boolean equals(Object obj) {
-    return delegate.equals(obj);
-  }
-
-  @Override
-  public int hashCode() {
-    return delegate.hashCode();
-  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org