jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r552863 - in /jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common: NodeInfoImpl.java PropertyInfoImpl.java SerializableIdFactory.java
Date Tue, 03 Jul 2007 14:54:20 GMT
Author: angela
Date: Tue Jul  3 07:54:19 2007
New Revision: 552863

URL: http://svn.apache.org/viewvc?view=rev&rev=552863
Log:
minor improvements:
- make usage of commons IdFactory
- add static create methods

Modified:
    jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/NodeInfoImpl.java
    jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/PropertyInfoImpl.java
    jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/SerializableIdFactory.java

Modified: jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/NodeInfoImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/NodeInfoImpl.java?view=diff&rev=552863&r1=552862&r2=552863
==============================================================================
--- jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/NodeInfoImpl.java
(original)
+++ jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/NodeInfoImpl.java
Tue Jul  3 07:54:19 2007
@@ -20,12 +20,14 @@
 import org.apache.jackrabbit.spi.NodeId;
 import org.apache.jackrabbit.spi.PropertyId;
 import org.apache.jackrabbit.spi.IdIterator;
+import org.apache.jackrabbit.spi.ItemId;
 import org.apache.jackrabbit.name.QName;
 import org.apache.jackrabbit.name.Path;
 
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.io.Serializable;
 
 /**
  * <code>NodeInfoImpl</code> implements a serializable <code>NodeInfo</code>
@@ -64,6 +66,39 @@
     private final List propertyIds;
 
     /**
+     * Creates a new serializable <code>NodeInfo</code> for the given
+     * <code>NodeInfo</code>.
+     *
+     * @param nodeInfo
+     */
+    public static NodeInfo createSerializableNodeInfo(NodeInfo nodeInfo, final SerializableIdFactory
idFactory) {
+        if (nodeInfo instanceof Serializable) {
+            return nodeInfo;
+        } else {
+            PropertyId[] refs = nodeInfo.getReferences();
+            PropertyId[] serRefs = new PropertyId[refs.length];
+            for (int i = 0; i < serRefs.length; i++) {
+                serRefs[i] = idFactory.createSerializablePropertyId(refs[i]);
+            }
+            NodeId parentId = null;
+            if (nodeInfo.getParentId() != null) {
+                parentId = idFactory.createSerializableNodeId(nodeInfo.getParentId());
+            }
+            return new NodeInfoImpl(parentId, nodeInfo.getQName(),
+                    nodeInfo.getPath(),
+                    idFactory.createSerializableNodeId(nodeInfo.getId()),
+                    nodeInfo.getIndex(), nodeInfo.getNodetype(),
+                    nodeInfo.getMixins(), serRefs,
+                    new IteratorHelper(nodeInfo.getPropertyIds()) {
+                        public ItemId nextId() {
+                            return idFactory.createSerializablePropertyId(
+                                    (PropertyId) super.nextId());
+                        }
+                    });
+        }
+    }
+
+    /**
      * Creates a new serializable node info for the given <code>node</code>
      * info.
      *
@@ -77,9 +112,9 @@
      * @param references      the references to this node.
      * @param propertyIds     the properties of this node.
      */
-    public NodeInfoImpl(NodeId parentId, QName name, Path path, NodeId id,
-                        int index, QName primaryTypeName, QName[] mixinNames,
-                        PropertyId[] references, IdIterator propertyIds) {
+    private NodeInfoImpl(NodeId parentId, QName name, Path path, NodeId id,
+                         int index, QName primaryTypeName, QName[] mixinNames,
+                         PropertyId[] references, IdIterator propertyIds) {
         super(parentId, name, path, true);
         this.id = id;
         this.index = index;

Modified: jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/PropertyInfoImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/PropertyInfoImpl.java?view=diff&rev=552863&r1=552862&r2=552863
==============================================================================
--- jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/PropertyInfoImpl.java
(original)
+++ jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/PropertyInfoImpl.java
Tue Jul  3 07:54:19 2007
@@ -23,6 +23,8 @@
 import org.apache.jackrabbit.name.QName;
 import org.apache.jackrabbit.name.Path;
 
+import java.io.Serializable;
+
 /**
  * <code>PropertyInfoImpl</code> implements a serializable
  * <code>PropertyInfo</code> based on another property info.
@@ -51,7 +53,24 @@
 
     /**
      * Creates a new serializable property info for the given
-     * <code>property</code>.
+     * <code>PropertyInfo</code>.
+     *
+     * @param propertyInfo
+     */
+    public static PropertyInfo createSerializablePropertyInfo(PropertyInfo propertyInfo,
SerializableIdFactory idFactory) {
+        if (propertyInfo instanceof Serializable) {
+            return propertyInfo;
+        } else {
+            return new PropertyInfoImpl(idFactory.createSerializableNodeId(propertyInfo.getParentId()),
+                propertyInfo.getQName(), propertyInfo.getPath(),
+                idFactory.createSerializablePropertyId(propertyInfo.getId()),
+                propertyInfo.getType(), propertyInfo.isMultiValued(),
+                propertyInfo.getValues());
+        }
+    }
+
+    /**
+     * Creates a new serializable property info for the given parameters.
      *
      * @param parentId      the parent id.
      * @param name          the name of this property.
@@ -61,7 +80,7 @@
      * @param isMultiValued whether this property is multi-valued.
      * @param values        the values.
      */
-    public PropertyInfoImpl(NodeId parentId, QName name, Path path,
+    private PropertyInfoImpl(NodeId parentId, QName name, Path path,
                             PropertyId id, int type, boolean isMultiValued,
                             QValue[] values) {
         super(parentId, name, path, false);

Modified: jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/SerializableIdFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/SerializableIdFactory.java?view=diff&rev=552863&r1=552862&r2=552863
==============================================================================
--- jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/SerializableIdFactory.java
(original)
+++ jackrabbit/trunk/contrib/spi/spi-rmi/src/main/java/org/apache/jackrabbit/spi/rmi/common/SerializableIdFactory.java
Tue Jul  3 07:54:19 2007
@@ -16,218 +16,57 @@
  */
 package org.apache.jackrabbit.spi.rmi.common;
 
-import org.apache.jackrabbit.name.Path;
-import org.apache.jackrabbit.spi.IdFactory;
-import org.apache.jackrabbit.spi.ItemId;
 import org.apache.jackrabbit.spi.PropertyId;
 import org.apache.jackrabbit.spi.NodeId;
-import org.apache.jackrabbit.name.QName;
-import org.apache.jackrabbit.name.MalformedPathException;
+import org.apache.jackrabbit.identifier.AbstractIdFactory;
 
 import java.io.Serializable;
 
 /**
  * <code>SerializableIdFactory</code> implements an id factory with serializable
  * item ids.
- * TODO: copied from spi2dav, move common part to spi-commons.
  */
-public class SerializableIdFactory implements IdFactory {
+public class SerializableIdFactory extends AbstractIdFactory {
 
     private static final SerializableIdFactory INSTANCE = new SerializableIdFactory();
 
-    private SerializableIdFactory() {};
+    private SerializableIdFactory() {}
 
     public static SerializableIdFactory getInstance() {
         return INSTANCE;
     }
 
     /**
-     * {@inheritDoc}
+     * Checks if the passed <code>nodeId</code> is serializable and if it is
not
+     * creates a serializable version for the given <code>nodeId</code>.
+     *
+     * @param nodeId the node id to check.
+     * @return a serializable version of <code>nodeId</code> or the passed
+     *         nodeId itself it is already serializable.
      */
-    public PropertyId createPropertyId(NodeId parentId, QName propertyName) {
-        try {
-            return new PropertyIdImpl(parentId, propertyName);
-        } catch (MalformedPathException e) {
-            throw new IllegalArgumentException(e.getMessage());
+    public NodeId createSerializableNodeId(NodeId nodeId) {
+        if (nodeId instanceof Serializable) {
+            return nodeId;
+        } else {
+            return INSTANCE.createNodeId(nodeId.getUniqueID(), nodeId.getPath());
         }
     }
 
     /**
-     * {@inheritDoc}
+     * Checks if the passed <code>propId</code> is serializable and if it is
not
+     * creates a serializable version for the given <code>propId</code>.
+     *
+     * @param propId the property id to check.
+     * @return a serializable version of <code>propId</code> or the passed
+     *         propId itself it is already serializable.
      */
-    public NodeId createNodeId(NodeId parentId, Path path) {
-        try {
-            return new NodeIdImpl(parentId, path);
-        } catch (MalformedPathException e) {
-            throw new IllegalArgumentException(e.getMessage());
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public NodeId createNodeId(String uniqueID, Path path) {
-        return new NodeIdImpl(uniqueID, path);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public NodeId createNodeId(String uniqueID) {
-        return new NodeIdImpl(uniqueID);
-    }
-
-    //------------------------------------------------------< Inner classes >---
-    private static abstract class ItemIdImpl implements ItemId, Serializable {
-
-        private final String uniqueID;
-        private final Path path;
-
-        private transient int hashCode = 0;
-
-        private ItemIdImpl(String uniqueID, Path path) {
-            if (uniqueID == null && path == null) {
-                throw new IllegalArgumentException("Only uniqueID or relative path might
be null.");
-            }
-            this.uniqueID = uniqueID;
-            this.path = path;
-        }
-
-        private ItemIdImpl(NodeId parentId, QName name) throws MalformedPathException {
-            if (parentId == null || name == null) {
-                throw new IllegalArgumentException("Invalid ItemIdImpl: parentId and name
must not be null.");
-            }
-            this.uniqueID = parentId.getUniqueID();
-            Path parentPath = parentId.getPath();
-            if (parentPath != null) {
-                this.path = Path.create(parentPath, name, true);
-            } else {
-                this.path = Path.create(name, Path.INDEX_UNDEFINED);
-            }
-        }
-
-        public abstract boolean denotesNode();
-
-        public String getUniqueID() {
-            return uniqueID;
-        }
-
-        public Path getPath() {
-            return path;
-        }
-
-        /**
-         * ItemIdImpl objects are equal if the have the same uuid and relative path.
-         *
-         * @param obj
-         * @return
-         */
-        public boolean equals(Object obj) {
-            if (obj == this) {
-                return true;
-            }
-            if (obj instanceof ItemId) {
-                ItemId other = (ItemId) obj;
-                return equals(other);
-            }
-            return false;
-        }
-
-        boolean equals(ItemId other) {
-            return (uniqueID == null ? other.getUniqueID() == null : uniqueID.equals(other.getUniqueID()))
-                && (path == null ? other.getPath() == null : path.equals(other.getPath()));
-        }
-
-        /**
-         * Returns the hash code of the uuid and the path. The computed hash code
-         * is memorized for better performance.
-         *
-         * @return hash code
-         * @see Object#hashCode()
-         */
-        public int hashCode() {
-            // since the ItemIdImpl is immutable, store the computed hash code value
-            if (hashCode == 0) {
-                hashCode = toString().hashCode();
-            }
-            return hashCode;
-        }
-
-        /**
-         * Combination of uuid and relative path
-         *
-         * @return
-         */
-        public String toString() {
-            StringBuffer b = new StringBuffer();
-            if (uniqueID != null) {
-                b.append(uniqueID);
-            }
-            if (path != null) {
-                b.append(path.toString());
-            }
-            return b.toString();
-        }
-    }
-
-    private static class NodeIdImpl extends ItemIdImpl implements NodeId {
-
-        public NodeIdImpl(String uniqueID) {
-            super(uniqueID, null);
-        }
-
-        public NodeIdImpl(String uniqueID, Path path) {
-            super(uniqueID, path);
-        }
-
-        public NodeIdImpl(NodeId parentId, Path path) throws MalformedPathException {
-            super(parentId.getUniqueID(), (parentId.getPath() != null) ? Path.create(parentId.getPath(),
path, true) : path);
-        }
-
-        public boolean denotesNode() {
-            return true;
-        }
-
-        public boolean equals(Object obj) {
-            if (obj == this) {
-                return true;
-            }
-            if (obj instanceof NodeId) {
-                return super.equals((NodeId)obj);
-            }
-            return false;
-        }
-    }
-
-    private static class PropertyIdImpl extends ItemIdImpl implements PropertyId {
-
-        private final NodeId parentId;
-
-        private PropertyIdImpl(NodeId parentId, QName name) throws MalformedPathException
{
-            super(parentId, name);
-            this.parentId = parentId;
-        }
-
-        public boolean denotesNode() {
-            return false;
-        }
-
-        public NodeId getParentId() {
-            return parentId;
-        }
-
-        public QName getQName() {
-            return getPath().getNameElement().getName();
-        }
-
-        public boolean equals(Object obj) {
-            if (obj == this) {
-                return true;
-            }
-            if (obj instanceof PropertyId) {
-                return super.equals((PropertyId)obj);
-            }
-            return false;
+    public PropertyId createSerializablePropertyId(PropertyId propertyId) {
+        if (propertyId instanceof Serializable) {
+            return propertyId;
+        } else {
+            return INSTANCE.createPropertyId(
+                    createSerializableNodeId(propertyId.getParentId()),
+                    propertyId.getQName());
         }
     }
 }



Mime
View raw message