package joshng.util.collect;

import joshng.util.ByteSerializable;
import joshng.util.StringUtils;

import java.util.Arrays;

/**
 * User: josh
 * Date: 5/1/14
 * Time: 7:06 PM
 */
public class ByteArrayKey implements ByteSerializable<byte[]> {
  private final byte[] bytes;
  private final int hashCode;

  public ByteArrayKey(byte[] bytes) {
    this.hashCode = Arrays.hashCode(bytes);
    this.bytes = bytes;
  }

  public byte[] bytes() {
    return bytes;
  }

  @Override
  public boolean equals(Object o) {
    return this == o || o != null && o.getClass() == getClass() && isEqual((ByteArrayKey)o);
  }

  private boolean isEqual(ByteArrayKey that) {
    return that.hashCode == hashCode && Arrays.equals(bytes, that.bytes);
  }

  @Override
  public int hashCode() {
    return hashCode;
  }

  @Override
  public String toString() {
    return StringUtils.toHexStringTruncatedWithEllipsis(bytes, 0, 32);
  }

  @Override
  public byte[] getSerializableValue() {
    return bytes;
  }

  public int getLength() {
    return bytes.length;
  }
}

@@ -402,32 +402,154 @@
 s = bytes;%0A  %7D%0A%0A
+  public static ByteArrayKey valueOf(String hex) %7B%0A    return new ByteArrayKey(StringUtils.bytesFromHexString(hex));%0A  %7D%0A%0A
   public byte%5B%5D 

introduce ByteArrayKey.valueOf(hex)