/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013-2014 Jeff Nelson, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.io;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;

import java.nio.ByteBuffer;
import java.util.Arrays;

import javax.annotation.concurrent.Immutable;

import org.cinchapi.concourse.annotate.DoNotInvoke;
import org.cinchapi.concourse.util.ByteBuffers;

import com.google.common.hash.Hashing;

/**
 * A {@link Composite} is a single Byteable object that wraps multiple other
 * Byteable objects.
 * 
 * @author jnelson
 */
@Immutable
public final class Composite implements Byteable {

    /**
     * Return a Composite for the list of {@code byteables}.
     * 
     * @param byteables
     * @return the Composite
     */
    public static Composite create(Byteable... byteables) {
        return new Composite(byteables);
    }

    /**
     * Create a Composite for the list of {@code byteables} with support for
     * caching. Cached Composites are not guaranteed to perfectly match up with
     * the list of byteables (because hash collisions can occur) so it is only
     * advisable to use this method of creation when precision is not a
     * requirement.
     * 
     * @param byteables
     * @return the Composite
     */
    public static Composite createCached(Byteable... byteables) {
        int hashCode = Arrays.hashCode(byteables);
        Composite composite = CACHE.get(hashCode);
        if(composite == null) {
            composite = create(byteables);
            CACHE.put(hashCode, composite);
        }
        return composite;
    }

    /**
     * Return the Token encoded in {@code bytes} so long as those
     * bytes adhere to the format specified by the {@link #getBytes()} method.
     * This method assumes that all the bytes in the {@code bytes} belong to the
     * Token. In general, it is necessary to get the appropriate
     * Value slice from the parent ByteBuffer using
     * {@link ByteBuffers#slice(ByteBuffer, int, int)}.
     * 
     * @param bytes
     * @return the Token
     */
    public static Composite fromByteBuffer(ByteBuffer bytes) {
        return Byteables.read(bytes, Composite.class);
    }

    /**
     * A cache of Composite. Each composite is associated with the cumulative
     * hashcode of all the things that went into the composite.
     */
    private final static TIntObjectMap<Composite> CACHE = new TIntObjectHashMap<Composite>();

    private final ByteBuffer bytes;

    /**
     * Construct an instance that represents an existing Token from
     * a ByteBuffer. This constructor is public so as to comply with the
     * {@link Byteable} interface. Calling this constructor directly is not
     * recommend. Use {@link #fromByteBuffer(ByteBuffer)} instead to take
     * advantage of reference caching.
     * 
     * @param bytes
     */
    @DoNotInvoke
    public Composite(ByteBuffer bytes) {
        this.bytes = bytes;
    }

    /**
     * Construct a new instance.
     * 
     * @param byteables
     */
    private Composite(Byteable... byteables) {
        int size = 0;
        for (Byteable byteable : byteables) {
            size += byteable.size();
        }
        bytes = ByteBuffer.allocate(size);
        for (Byteable byteable : byteables) {
            byteable.copyTo(bytes);
        }
        bytes.rewind();
    }

    @Override
    public void copyTo(ByteBuffer buffer) {
        ByteBuffers.copyAndRewindSource(bytes, buffer);
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Composite) {
            Composite other = (Composite) obj;
            return getBytes().equals(other.getBytes());
        }
        return false;
    }

    @Override
    public ByteBuffer getBytes() {
        return ByteBuffers.asReadOnlyBuffer(bytes);
    }

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

    @Override
    public int size() {
        return bytes.capacity();
    }

    @Override
    public String toString() {
        return Hashing.sha1().hashBytes(ByteBuffers.toByteArray(getBytes()))
                .toString();
    }

}
@@ -1230,109 +1230,52 @@
 ort 
-gnu.trove.map.TIntObjectMap;%0Aimport gnu.trove.map.hash.TIntObjectHashMap;%0A%0Aimport java.nio.ByteBuffer
+java.nio.ByteBuffer;%0Aimport java.util.Arrays
 ;%0Aim
@@ -1289,22 +1289,19 @@
 va.util.
-Arrays
+Map
 ;%0A%0Aimpor
@@ -1443,16 +1443,55 @@
 ffers;%0A%0A
+import com.google.common.collect.Maps;%0A
 import c
@@ -3475,22 +3475,21 @@
 tic 
-TIntObjectMap%3C
+Map%3CInteger, 
 Comp
@@ -3507,40 +3507,23 @@
 E = 
-new TIntObjectHashMap%3CComposite%3E
+Maps.newHashMap
 ();%0A

use built in hashmap for composite cache