pnever      2003/09/03 06:52:10

  Modified:    src/share/org/apache/slide/structure ObjectNode.java
  Log:
  Made children computed
  
  Revision  Changes    Path
  1.12      +126 -108  
jakarta-slide/src/share/org/apache/slide/structure/ObjectNode.java
  
  Index: ObjectNode.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/structure/ObjectNode.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ObjectNode.java   27 Aug 2003 13:22:18 -0000      1.11
  +++ ObjectNode.java   3 Sep 2003 13:52:10 -0000       1.12
  @@ -65,6 +65,7 @@
   
   import java.io.Serializable;
   import java.util.Enumeration;
  +import java.util.StringTokenizer;
   import java.util.Vector;
   import org.apache.slide.common.ObjectValidationFailedException;
   import org.apache.slide.util.Messages;
  @@ -80,54 +81,11 @@
   public abstract class ObjectNode
       implements Serializable, Cloneable {
       
  -    
  -    // ----------------------------------------------------------- Constructors
  -    
  -    
  -    /**
  -     * Default constructor.
  -     */
  -    public ObjectNode() {
  -    }
  -    
  -    /**
  -     * Default constructor.
  -     */
  -    public ObjectNode(String uri) {
  -        this.uri = uri;
  -    }
  -    
  -    /**
  -     * Default constructor.
  -     */
  -    public ObjectNode(String uri, Vector children, Vector links) {
  -        this.uri = uri;
  -        this.children = children;
  -        this.links = links;
  -        this.bindings = new BindingList();
  -        this.parentBindings = new ParentBindingList();
  -    }
  -    
  -    /**
  -     * Contructor to be used by stores supporting binding.
  -     */
  -    public ObjectNode(String uuri, Vector bindings, Vector parentBindings, Vector 
links) {
  -        this.uuri = uuri;
  -        this.bindings = new BindingList(bindings);
  -        this.parentBindings = new ParentBindingList(parentBindings);
  -        this.links = links;
  -        computeChildren();
  -    }
  -    
  -    
  -    
  -    // ----------------------------------------------------- Instance Variables
       /**
        * Uniform ressource identifier (URI) of the object.
        */
       protected String uri;
       
  -    
       /**
        * Unique URI of the object. Enables clients to determine
        * whether two bindings are to the same resource.
  @@ -135,27 +93,14 @@
        * If binding is enabled, several object URIs may represent the same resource,
        * while the UURI must be really unique.
        */
  -    protected String uuri;
  -    
  -    /**
  -     * Vector of children's Uris. Before modifying this vector you must check
  -     * wheter [EMAIL PROTECTED] #childrenShared} is true. In this case clone the 
vector
  -     * and set the shared state to false.
  -     */
  -    private Vector children = new Vector();
  -    
  -    /**
  -     * If true then the [EMAIL PROTECTED] #children} vector is shared between 
multiple
  -     * ObjectNode-instances and thus must not be modified.
  -     */
  -    private boolean childrenShared;
  +    private String uuri;
       
       /**
        * Vector of inbound links' Uris. Before modifying this vector you must check
        * wheter [EMAIL PROTECTED] #linksShared} is true. In this case clone the vector
        * and set the shared state to false.
        */
  -    private Vector links = new Vector();
  +    private Vector links;
       
       /**
        * If true then the [EMAIL PROTECTED] #links} vector is shared between multiple
  @@ -168,8 +113,8 @@
        * whether [EMAIL PROTECTED] #bindingsShared} is true. In this case clone the 
vector
        * and set the shared state to false.
        */
  -    private BindingList bindings = new BindingList();
  -    private ParentBindingList parentBindings = new ParentBindingList();
  +    private BindingList bindings;
  +    private ParentBindingList parentBindings;
       
       /**
        * If true then the [EMAIL PROTECTED] #bindings} vector is shared between 
multiple
  @@ -178,7 +123,46 @@
       private boolean bindingsShared;
       
       
  -    // ------------------------------------------------------------- Properties
  +    /**
  +     * Default constructor.
  +     */
  +    public ObjectNode() {
  +        this.links = new Vector();
  +        this.bindings = new BindingList();
  +        this.parentBindings = new ParentBindingList();
  +    }
  +    
  +    /**
  +     * Default constructor.
  +     */
  +    public ObjectNode(String uri) {
  +        this();
  +        this.uri = uri;
  +    }
  +    
  +    /**
  +     * Default constructor.
  +     * NOTE: this constructor should not be used if binding is enabled for the
  +     *       store associated to the specified URI.
  +     */
  +    public ObjectNode(String uri, Vector children, Vector links) {
  +        this( uri );
  +        this.links = links;
  +        addChildren(children);
  +    }
  +    
  +    /**
  +     * Contructor to be used by stores supporting binding.
  +     */
  +    public ObjectNode(String uuri, Vector bindings, Vector parentBindings, Vector 
links) {
  +        this();
  +        this.uuri = uuri;
  +        this.bindings = new BindingList(bindings);
  +        this.parentBindings = new ParentBindingList(parentBindings);
  +        this.links = links;
  +        computeChildren();
  +    }
  +    
       /**
        * Get object's unique resource identifier.
        *
  @@ -231,7 +215,7 @@
        * @return Enumeration Children's uris
        */
       public Vector getChildren() {
  -        return (Vector)children.clone();
  +        return computeChildren();
       }
       
       /**
  @@ -240,7 +224,7 @@
        * @return Enumeration Children's uris
        */
       public Enumeration enumerateChildren() {
  -        return children.elements();
  +        return computeChildren().elements();
       }
       
       /**
  @@ -284,7 +268,7 @@
        * false otherwise
        */
       public boolean hasChild(String uri) {
  -        return children.contains(uri);
  +        return computeChildren().contains(uri);
       }
       
       /**
  @@ -297,7 +281,7 @@
       public boolean hasChild(ObjectNode child) {
           boolean result = false;
           if (child != null) {
  -            result = children.contains(child.getUri());
  +            result = computeChildren().contains(child.getUri());
           }
           return result;
       }
  @@ -366,7 +350,7 @@
        * @return boolean true if this object has children, false otherwise
        */
       public boolean hasChildren() {
  -        return !(children.isEmpty());
  +        return !(computeChildren().isEmpty());
       }
       
       /**
  @@ -407,13 +391,9 @@
           
           try {
               // init the shared fields to let clone() copy them
  -            this.childrenShared=true;
               this.linksShared=true;
               this.bindingsShared=true;
               result = (ObjectNode) super.clone();
  -            /* Old code removed because of lazy cloning
  -             result.children = (Vector) this.children.clone();
  -             result.links = (Vector) this.links.clone();*/
           } catch(CloneNotSupportedException e) {
               e.printStackTrace();
           }
  @@ -431,10 +411,8 @@
           
           try {
               result = (ObjectNode) super.clone();
  -            result.childrenShared=false;
               result.linksShared=false;
               result.bindingsShared=false;
  -            result.children = new Vector();
               result.links = new Vector();
               result.bindings = new BindingList();
               result.parentBindings = new ParentBindingList();
  @@ -462,11 +440,6 @@
                   (expectedUri, Messages.message
                        (ObjectNode.class.getName() + ".incorrectUri"));
           
  -        if (children == null)
  -            throw new ObjectValidationFailedException
  -                (uri, Messages.message
  -                     (ObjectNode.class.getName() + ".nullChildrenVector"));
  -        
           if (bindings == null || parentBindings == null)
               throw new ObjectValidationFailedException
                   (uri, Messages.message
  @@ -493,29 +466,19 @@
        * @param    child               an ObjectNode
        */
       public void addBinding( String bindingName, ObjectNode source ) {
  -        String childUri = (this.uri.endsWith("/")
  -                               ? this.uri+bindingName
  -                               : this.uri+"/"+bindingName);
  -        
  -        if (!hasChild(childUri)) {
  -            if(childrenShared) {
  -                // Lazy cloning on first write access
  -                children=(Vector)children.clone();
  -                childrenShared=false;
  -            }
  -            children.addElement(childUri);
  +        if (!hasBinding(bindingName)) {
               if(bindingsShared) {
                   // Lazy cloning on first write access
                   bindings=(BindingList)bindings.clone();
                   parentBindings=(ParentBindingList)parentBindings.clone();
                   bindingsShared=false;
               }
  -            Binding oldBinding = bindings.put(bindingName, source);
  +            bindings.put(bindingName, source);
               source.addParentBinding(bindingName, this);
  -            if (oldBinding != null) {
  -                throw new IllegalStateException(
  -                    "Existing binding "+bindingName+" at "+this.uri+" has to be 
removed first");
  -            }
  +        }
  +        else {
  +            throw new IllegalStateException(
  +                "Existing binding "+bindingName+" at "+this.uri+" has to be removed 
first");
           }
       }
       
  @@ -529,13 +492,6 @@
               return;
           }
           
  -        if(childrenShared) {
  -            // Lazy cloning on first write access
  -            children=(Vector)children.clone();
  -            childrenShared=false;
  -        }
  -        children.removeElement( child.getUri() );
  -        
           if(bindingsShared) {
               // Lazy cloning on first write access
               bindings=(BindingList)bindings.clone();
  @@ -559,16 +515,35 @@
       }
       
       String lastUriSegment( String uri ) {
  -        return uri.substring(uri.lastIndexOf('/') + 1);
  +        return new UriPath(uri).lastSegment();
       }
       
  -    private void computeChildren() {
  -        this.children = new Vector();
  +    private Vector computeChildren() {
  +        Vector children = new Vector();
           Enumeration e = bindings.elements();
           while (e.hasMoreElements()) {
               Binding b = (Binding)e.nextElement();
               children.add( uri+"/"+b.getName() );
           }
  +        return children;
  +    }
  +    
  +    /**
  +     * Method computeBindings
  +     * NOTE: should not be used if binding is enabled for the
  +     *       store associated to this object node.
  +     */
  +    private void addChildren( Vector children ) {
  +        Enumeration ch = children.elements();
  +        while (ch.hasMoreElements()) {
  +            String c = (String)ch.nextElement();
  +            ObjectNode s = new SubjectNode(c);
  +            s.setUuri( s.getUri() );
  +            addBinding( lastUriSegment(c), s );
  +        }
  +        ObjectNode p = new SubjectNode( new UriPath(uri).parent().toString() );
  +        p.setUuri( p.getUri() );
  +        addParentBinding( lastUriSegment(), p );
       }
       
       public void addParentBinding( String bindingName, ObjectNode parent ) {
  @@ -726,12 +701,12 @@
           }
           
           private Binding put(String bindingName, ObjectNode source) {
  -            int i = container.indexOf( new Binding(bindingName, null) );
               Binding result = null;
               String uuri = "";
               if (source != null && source.getUuri() != null) {
                   uuri = source.getUuri();
               }
  +            int i = container.indexOf( new Binding(bindingName, null) );
               if (i >= 0) {
                   result = (Binding)container.get(i);
                   container.set( i, new Binding(bindingName, uuri) );
  @@ -756,6 +731,49 @@
           
           public int size() {
               return container.size();
  +        }
  +    }
  +    
  +    public static class UriPath {
  +        private String[] tokens;
  +        
  +        private UriPath() {
  +        }
  +        
  +        public UriPath(String uri) {
  +            StringTokenizer t = new StringTokenizer( uri, "/" );
  +            this.tokens = new String[t.countTokens()];
  +            for (int i = 0; t.hasMoreTokens(); i++) {
  +                tokens[i] = t.nextToken();
  +            }
  +        }
  +        
  +        public String lastSegment() {
  +            return tokens.length > 0
  +                ? tokens[ tokens.length - 1 ]
  +                : null;
  +        }
  +        
  +        public UriPath parent() {
  +            UriPath result = new UriPath();
  +            result.tokens = new String[this.tokens.length - 1];
  +            for (int i = 0; i < result.tokens.length; i++) {
  +                result.tokens[i] = this.tokens[i];
  +            }
  +            return result;
  +        }
  +        
  +        public String toString() {
  +            if (tokens.length > 0) {
  +                StringBuffer b = new StringBuffer();
  +                for (int i = 0; i < tokens.length; i++) {
  +                    b.append("/").append(tokens[i]);
  +                }
  +                return b.toString();
  +            }
  +            else {
  +                return "/";
  +            }
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to