/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.jackrabbit.oak.plugins.memory;

import java.io.IOException;
import java.io.InputStream;

import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;

import org.apache.jackrabbit.oak.api.Blob;

/**
 * Abstract base class for {@link Blob} implementations.
 * This base class provides default implementations for
 * {@code hashCode} and {@code equals}.
 */
public abstract class AbstractBlob implements Blob {

    private static InputSupplier<InputStream> supplier(final Blob blob) {
        return new InputSupplier<InputStream>() {
            @Override
            public InputStream getInput() throws IOException {
                return blob.getNewStream();
            }
        };
    }

    public static boolean equal(Blob a, Blob b) {
        try {
            return ByteStreams.equal(supplier(a), supplier(b));
        } catch (IOException e) {
            throw new IllegalStateException("Blob equality check failed", e);
        }
    }

    public static HashCode calculateSha256(Blob blob) {
        if (blob instanceof AbstractBlob) {
            return ((AbstractBlob) blob).getSha256();
        } else {
            try {
                return ByteStreams.hash(supplier(blob), Hashing.sha256());
            } catch (IOException e) {
                throw new IllegalStateException("Hash calculation failed", e);
            }
        }
    }

    private HashCode hashCode; // synchronized access

    protected AbstractBlob(HashCode hashCode) {
        this.hashCode = hashCode;
    }

    protected AbstractBlob() {
        this(null);
    }

    private synchronized HashCode getSha256() {
        // Blobs are immutable so we can safely cache the hash
        if (hashCode == null) {
            hashCode = calculateSha256(this);
        }
        return hashCode;
    }

    /**
     * This hash code implementation returns the hash code of the underlying stream
     * @return
     */
    protected byte[] sha256() {
        return getSha256().asBytes();
    }

    /**
     * To {@code Blob} instances are considered equal iff they have the
     * same SHA-256 hash code  are equal.
     * @param other
     * @return
     */
    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }

        if (other instanceof AbstractBlob) {
            AbstractBlob that = (AbstractBlob) other;
            // optimize the comparison if both this and the other blob
            // already have pre-computed SHA-256 hash codes
            synchronized (this) {
                if (hashCode != null) {
                    synchronized (that) {
                        if (that.hashCode != null) {
                            return hashCode.equals(that.hashCode);
                        }
                    }
                }
            }
        }

        return other instanceof Blob && equal(this, (Blob) other);
    }

    @Override
    public int hashCode() {
        return 0; // see Blob javadoc
    }

    @Override
    public String toString() {
        return getSha256().toString();
    }

}

@@ -1917,16 +1917,22 @@
 eSha256(
+final 
 Blob blo
@@ -1928,32 +1928,57 @@
 al Blob blob) %7B%0A
+        AbstractBlob ab;%0A
         if (blob
@@ -2017,22 +2017,20 @@
         
-return
+ab =
  ((Abstr
@@ -2047,20 +2047,8 @@
 lob)
-.getSha256()
 ;%0A  
@@ -2078,229 +2078,358 @@
     
-try %7B%0A                return ByteStreams.hash(supplier(blob), Hashing.sha256());%0A            %7D catch (IOException e) %7B%0A                throw new IllegalStateException(%22Hash calculation failed%22, e);%0A            %7D%0A        %7D
+ab = new AbstractBlob() %7B%0A                @Override%0A                public long length() %7B%0A                    return blob.length();%0A                %7D%0A                @Override%0A                public InputStream getNewStream() %7B%0A                    return blob.getNewStream();%0A                %7D%0A            %7D;%0A        %7D%0A        return ab.getSha256();
 %0A   
@@ -2793,41 +2793,223 @@
     
-hashCode = calculateSha256(this);
+try %7B%0A                hashCode = ByteStreams.hash(supplier(this), Hashing.sha256());%0A            %7D catch (IOException e) %7B%0A                throw new IllegalStateException(%22Hash calculation failed%22, e);%0A            %7D
 %0A   

Implement the MicroKernel API