jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1394011 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: api/Blob.java plugins/memory/AbstractBlob.java plugins/memory/BlobImpl.java
Date Thu, 04 Oct 2012 12:34:41 GMT
Author: mduerig
Date: Thu Oct  4 12:34:41 2012
New Revision: 1394011

URL: http://svn.apache.org/viewvc?rev=1394011&view=rev
Log:
OAK-350: Unify PropertyState and CoreValue
- Introduce abstract base class for Blob implementations

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
  (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BlobImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java?rev=1394011&r1=1394010&r2=1394011&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java Thu
Oct  4 12:34:41 2012
@@ -4,10 +4,14 @@ import java.io.InputStream;
 
 import javax.annotation.Nonnull;
 
+/**
+ * Immutable representation of a binary value of finite length.
+ */
 public interface Blob {
 
     /**
-     * Returns a new stream for this value object.
+     * Returns a new stream for this value object. Multiple calls to this
+     * methods return equal instances: {@code getNewStream().equals(getNewStream())}.
      * @return a new stream for this value based on an internal conversion.
      */
     @Nonnull
@@ -16,7 +20,7 @@ public interface Blob {
     /**
      * Returns the length of this blob.
      *
-     * @return the length of this bloc.
+     * @return the length of this blob.
      */
     long length();
 }

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java?rev=1394011&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
Thu Oct  4 12:34:41 2012
@@ -0,0 +1,59 @@
+package org.apache.jackrabbit.oak.plugins.memory;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import com.google.common.base.Optional;
+import org.apache.jackrabbit.oak.api.Blob;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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 final Logger log = LoggerFactory.getLogger(AbstractBlob.class);
+
+    private Optional<Integer> hashCode = Optional.absent();
+
+    /**
+     * This hash code implementation returns the hash code of the underlying stream
+     * @return
+     */
+    @Override
+    public int hashCode() {
+        // Blobs are immutable so we can safely cache the hash
+        if (!hashCode.isPresent()) {
+            InputStream s = getNewStream();
+            try {
+                hashCode = Optional.of(s.hashCode());
+            }
+            finally {
+                close(s);
+            }
+        }
+        return hashCode.get();
+    }
+
+    /**
+     * To {@code Blob} instances are considered equal iff they have the same hash code
+     * are equal.
+     * @param other
+     * @return
+     */
+    @Override
+    public boolean equals(Object other) {
+        return other == this || other instanceof Blob && hashCode() == other.hashCode();
+    }
+
+    private static void close(InputStream s) {
+        try {
+            s.close();
+        }
+        catch (IOException e) {
+            log.warn("Error while closing stream", e);
+        }
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BlobImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BlobImpl.java?rev=1394011&r1=1394010&r2=1394011&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BlobImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BlobImpl.java
Thu Oct  4 12:34:41 2012
@@ -2,10 +2,9 @@ package org.apache.jackrabbit.oak.plugin
 
 import java.io.InputStream;
 
-import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.CoreValue;
 
-public class BlobImpl implements Blob {
+public class BlobImpl extends AbstractBlob {
     private final CoreValue value;
 
     public BlobImpl(CoreValue value) {



Mime
View raw message