
/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.kv;

import com.google.common.base.Preconditions;

import java.util.Arrays;
import java.util.Map;

import org.jsimpledb.util.ByteUtil;

/**
 * A key/value pair.
 *
 * Note: the internal byte arrays are not copied; therefore, values passed to the constructor
 * or returned from the accessor methods must not be modified if instances are to remain immutable.
 */
public class KVPair {

    private final byte[] key;
    private final byte[] value;

    /**
     * Constructor. The given arrays are copied.
     *
     * @param key key
     * @param value value
     * @throws IllegalArgumentException if {@code key} or {@code value} is null
     */
    public KVPair(byte[] key, byte[] value) {
        Preconditions.checkArgument(key != null, "null key");
        Preconditions.checkArgument(value != null, "null value");
        this.key = key;
        this.value = value;
    }

    /**
     * Constructor. The given key and value arrays are copied.
     *
     * @param entry map entry
     * @throws IllegalArgumentException if {@code entry} or its key or value is null
     */
    public KVPair(Map.Entry<byte[], byte[]> entry) {
        Preconditions.checkArgument(entry != null, "null entry");
        this.key = entry.getKey();
        this.value = entry.getValue();
        Preconditions.checkArgument(this.key != null, "null key");
        Preconditions.checkArgument(this.value != null, "null value");
    }

    /**
     * Get the key.
     *
     * @return the key
     */
    public byte[] getKey() {
        return this.key;
    }

    /**
     * Get the value.
     *
     * @return the value
     */
    public byte[] getValue() {
        return this.value;
    }

    @Override
    public String toString() {
        return "{" + ByteUtil.toString(this.key) + "," + ByteUtil.toString(this.value) + "}";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj == null || obj.getClass() != this.getClass())
            return false;
        final KVPair that = (KVPair)obj;
        return Arrays.equals(this.key, that.key) && Arrays.equals(this.value, that.value);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(this.key) ^ Arrays.hashCode(this.value);
    }
}


@@ -248,16 +248,23 @@
 air.%0A *%0A
+ * %3Cp%3E%0A
  * Note:
@@ -449,16 +449,57 @@
 utable.%0A
+ * To ensure safety, use %7B@link #clone%7D.%0A
  */%0Apubl
@@ -514,16 +514,37 @@
  KVPair 
+implements Cloneable 
 %7B%0A%0A    p
@@ -542,37 +542,41 @@
  %7B%0A%0A    private 
+/*
 final
+*/
  byte%5B%5D key;%0A   
@@ -584,21 +584,25 @@
 private 
+/*
 final
+*/
  byte%5B%5D 
@@ -1835,32 +1835,556 @@
 s.value;%0A    %7D%0A%0A
+// Cloneable%0A%0A    /**%0A     * Deep-clone this instance. Copys this instance as well as the key and value %7B@code byte%5B%5D%7D arrays.%0A     *%0A     * @return cloned instance%0A     */%0A    @Override%0A    public KVPair clone() %7B%0A        final KVPair clone;%0A        try %7B%0A            clone = (KVPair)super.clone();%0A        %7D catch (CloneNotSupportedException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A        clone.key = clone.key.clone();%0A        clone.value = clone.value.clone();%0A        return clone;%0A    %7D%0A%0A// Object%0A%0A
     @Override%0A  

Make KVPair implement a deep Cloneable.