You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2012/03/07 16:49:23 UTC

svn commit: r1297998 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model: AbstractChildNodeEntry.java AbstractNodeState.java AbstractPropertyState.java NodeState.java PropertyState.java

Author: jukka
Date: Wed Mar  7 15:49:22 2012
New Revision: 1297998

URL: http://svn.apache.org/viewvc?rev=1297998&view=rev
Log:
OAK-3: Internal tree model

Define node state equality and add abstract base classes for sharing default functionality

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractChildNodeEntry.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractNodeState.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractPropertyState.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/NodeState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/PropertyState.java

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractChildNodeEntry.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractChildNodeEntry.java?rev=1297998&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractChildNodeEntry.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractChildNodeEntry.java Wed Mar  7 15:49:22 2012
@@ -0,0 +1,65 @@
+/*
+ * 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.model;
+
+/**
+ * Abstract base class for {@link ChildNodeEntry} implementations.
+ * This base class contains default implementations of the
+ * {@link #equals(Object)} and {@link #hashCode()} methods based on
+ * the implemented interface.
+ */
+public abstract class AbstractChildNodeEntry implements ChildNodeEntry {
+
+    /**
+     * Checks whether the given object is equal to this one. Two child node
+     * entries are considered equal if both their names and referenced node
+     * states match. Subclasses may override this method with a more efficient
+     * equality check if one is available.
+     *
+     * @param that target of the comparison
+     * @return <code>true</code> if the objects are equal,
+     *         <code>false</code> otherwise
+     */
+    @Override
+    public boolean equals(Object that) {
+        if (this == that) {
+            return true;
+        } else if (that instanceof ChildNodeEntry) {
+            ChildNodeEntry other = (ChildNodeEntry) that;
+            return getName().equals(other.getName())
+                    && getNode().equals(other.getNode());
+        } else {
+            return false;
+        }
+
+    }
+
+    /**
+     * Returns a hash code that's compatible with how the
+     * {@link #equals(Object)} method is implemented. The current
+     * implementation simply returns the hash code of the child node name
+     * since {@link ChildNodeEntry} instances are not intended for use as
+     * hash keys.
+     *
+     * @return hash code
+     */
+    @Override
+    public int hashCode() {
+        return getName().hashCode();
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractChildNodeEntry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractNodeState.java?rev=1297998&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractNodeState.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractNodeState.java Wed Mar  7 15:49:22 2012
@@ -0,0 +1,132 @@
+/*
+ * 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.model;
+
+/**
+ * Abstract base class for {@link NodeState} implementations.
+ * This base class contains default implementations of the
+ * {@link #equals(Object)} and {@link #hashCode()} methods based on
+ * the implemented interface.
+ * <p>
+ * This class also implements trivial (and potentially very slow) versions of
+ * the {@link #getProperty(String)} and {@link #getPropertyCount()} methods
+ * based on {@link #getProperties()}. The {@link #getChildNode(String)} and
+ * {@link #getChildNodeCount()} methods are similarly implemented based on
+ * {@link #getChildNodeEntries(long, long)}. Subclasses should normally
+ * override these method with a more efficient alternatives.
+ */
+public abstract class AbstractNodeState implements NodeState {
+
+    @Override
+    public PropertyState getProperty(String name) {
+        for (PropertyState property : getProperties()) {
+            if (name.equals(property.getName())) {
+                return property;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    @SuppressWarnings("unused")
+    public long getPropertyCount() {
+        long count = 0;
+        for (PropertyState property : getProperties()) {
+            count++;
+        }
+        return count;
+    }
+
+    @Override
+    public NodeState getChildNode(String name) {
+        for (ChildNodeEntry entry : getChildNodeEntries(0, -1)) {
+            if (name.equals(entry.getName())) {
+                return entry.getNode();
+            }
+        }
+        return null;
+    }
+
+    @Override
+    @SuppressWarnings("unused")
+    public long getChildNodeCount() {
+        long count = 0;
+        for (ChildNodeEntry entry : getChildNodeEntries(0, -1)) {
+            count++;
+        }
+        return count;
+    }
+
+    /**
+     * Checks whether the given object is equal to this one. Two node states
+     * are considered equal if all their properties and child nodes match,
+     * regardless of ordering. Subclasses may override this method with a
+     * more efficient equality check if one is available.
+     *
+     * @param that target of the comparison
+     * @return <code>true</code> if the objects are equal,
+     *         <code>false</code> otherwise
+     */
+    @Override
+    public boolean equals(Object that) {
+        if (this == that) {
+            return true;
+        } else if (that == null || !(that instanceof NodeState)) {
+            return false;
+        }
+
+        NodeState other = (NodeState) that;
+
+        long propertyCount = 0;
+        for (PropertyState property : getProperties()) {
+            if (!property.equals(other.getProperty(property.getName()))) {
+                return false;
+            }
+            propertyCount++;
+        }
+        if (propertyCount != other.getPropertyCount()) {
+            return false;
+        }
+
+        long childNodeCount = 0;
+        for (ChildNodeEntry entry : getChildNodeEntries(0, -1)) {
+            if (!entry.getNode().equals(other.getChildNode(entry.getName()))) {
+                return false;
+            }
+            childNodeCount++;
+        }
+        if (childNodeCount != other.getChildNodeCount()) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Returns a hash code that's compatible with how the
+     * {@link #equals(Object)} method is implemented. The current
+     * implementation simply returns zero for everything since
+     * {@link NodeState} instances are not intended for use as hash keys.
+     *
+     * @return hash code
+     */
+    @Override
+    public int hashCode() {
+        return 0;
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractNodeState.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractPropertyState.java?rev=1297998&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractPropertyState.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractPropertyState.java Wed Mar  7 15:49:22 2012
@@ -0,0 +1,64 @@
+/*
+ * 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.model;
+
+/**
+ * Abstract base class for {@link PropertyState} implementations.
+ * This base class contains default implementations of the
+ * {@link #equals(Object)} and {@link #hashCode()} methods based on
+ * the implemented interface.
+ */
+public abstract class AbstractPropertyState implements PropertyState {
+
+    /**
+     * Checks whether the given object is equal to this one. Two property
+     * states are considered equal if both their names and encoded values
+     * match. Subclasses may override this method with a more efficient
+     * equality check if one is available.
+     *
+     * @param that target of the comparison
+     * @return <code>true</code> if the objects are equal,
+     *         <code>false</code> otherwise
+     */
+    @Override
+    public boolean equals(Object that) {
+        if (this == that) {
+            return true;
+        } else if (that instanceof PropertyState) {
+            PropertyState other = (PropertyState) that;
+            return getName().equals(other.getName())
+                    && getEncodedValue().equals(other.getEncodedValue());
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Returns a hash code that's compatible with how the
+     * {@link #equals(Object)} method is implemented. The current
+     * implementation simply returns the hash code of the property name
+     * since {@link PropertyState} instances are not intended for use as
+     * hash keys.
+     *
+     * @return hash code
+     */
+    @Override
+    public int hashCode() {
+        return getName().hashCode();
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/AbstractPropertyState.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/NodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/NodeState.java?rev=1297998&r1=1297997&r2=1297998&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/NodeState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/NodeState.java Wed Mar  7 15:49:22 2012
@@ -77,6 +77,15 @@ package org.apache.jackrabbit.oak.model;
  * according to the API contract of this interface. A separate higher level
  * interface needs to be used if an implementation can't for example
  * guarantee immutability of exposed content as discussed above.
+ *
+ * <h2>Equality and hash codes</h2>
+ * <p>
+ * Two node states are considered equal if their properties and child nodes
+ * match, regardless of ordering. The {@link Object#equals(Object)} method
+ * needs to be implemented so that it complies with this definition. And
+ * while node states are not meant for use as hash keys, the
+ * {@link Object#hashCode()} method should still be implemented according
+ * to the equality contract.
  */
 public interface NodeState {
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/PropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/PropertyState.java?rev=1297998&r1=1297997&r2=1297998&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/PropertyState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/model/PropertyState.java Wed Mar  7 15:49:22 2012
@@ -17,7 +17,8 @@
 package org.apache.jackrabbit.oak.model;
 
 /**
- * TODO: document
+ * Immutable property state. A property consists of a name and
+ * a JSON-encoded value.
  */
 public interface PropertyState {