Created
November 26, 2019 20:19
-
-
Save ok3141/6d02ea5e575937b99e66ff5eaed63047 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.shamanland.annotations.NonNull; | |
import com.shamanland.annotations.Nullable; | |
public final class ImmutableByteArray { | |
@NonNull | |
private final byte[] bytes; | |
@NonNull | |
public byte[] getBytes() { | |
return bytes.clone(); | |
} | |
public ImmutableByteArray(@NonNull byte[] bytes, int offset, int length) { | |
this.bytes = new byte[length]; | |
System.arraycopy(bytes, offset, this.bytes, 0, length); | |
} | |
public boolean equalsArray(@Nullable byte[] bytes) { | |
if (bytes == null) { | |
return false; | |
} | |
if (this.bytes.length != bytes.length) { | |
return false; | |
} | |
for (int i = 0, n = bytes.length; i < n; i++) { | |
if (this.bytes[i] != bytes[i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} else if (o == null || getClass() != o.getClass()) { | |
return false; | |
} else { | |
return equalsArray(((ImmutableByteArray) o).bytes); | |
} | |
} | |
@Override | |
public int hashCode() { | |
int result = 1; | |
for (byte element : bytes) { | |
result = 31 * result + element; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment