db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bri...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker/util/graph GraphTest.java
Date Tue, 06 Jul 2004 00:28:56 GMT
brianm      2004/07/05 17:28:56

  Added:       src/java/org/apache/ojb/broker/util/graph BaseVisitor.java
                        CollectionWrapper.java Graph.java GraphVisitor.java
                        ObjectWrapper.java
               src/test/org/apache/ojb/broker/util/graph GraphTest.java
  Log:
  Added a persistent object graph traversal system, required for attach/detach; hopefully
also useful in places where we traverse object graphs already as well.
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/graph/BaseVisitor.java
  
  Index: BaseVisitor.java
  ===================================================================
  /* Copyright 2004-2004 The Apache Software Foundation
   *
   * Licensed 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.ojb.broker.util.graph;
  
  public class BaseVisitor implements GraphVisitor
  {
      public void visitObject(ObjectWrapper obj)
      {
      }
  
      public void visitCollection(CollectionWrapper c)
      {
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/graph/CollectionWrapper.java
  
  Index: CollectionWrapper.java
  ===================================================================
  /* Copyright 2004-2004 The Apache Software Foundation
   *
   * Licensed 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.ojb.broker.util.graph;
  
  import org.apache.ojb.broker.core.proxy.ProxyHelper;
  import org.apache.ojb.broker.core.proxy.CollectionProxy;
  
  import java.util.Collection;
  import java.util.Iterator;
  
  /**
   * Wraps a collection on a persistent class
   *
   * @see GraphVisitor
   */
  public class CollectionWrapper
  {
      private Collection real;
  
      CollectionWrapper(Collection c)
      {
          this.real = c;
      }
  
      /**
       * Visit each of the elements in the wrapped collection
       * @param v
       */
      public void visitElements(GraphVisitor v)
      {
          if (real == null) return;
          for (Iterator iterator = real.iterator(); iterator.hasNext();)
          {
              Object o = iterator.next();
              v.visitObject(new ObjectWrapper(o));
          }
      }
  
      /**
       * Is the wrapped collection a proxy? 
       */
      public boolean isProxy()
      {
          return ProxyHelper.isCollectionProxy(real);
      }
  
      /**
       * Returns true on non-proxied collections, true on proxied collections which
       * have been loaded already, and false on unloaded proxied collections
       */
      public boolean isLoaded()
      {
          if (ProxyHelper.isCollectionProxy(real))
          {
              return ((CollectionProxy)real).isLoaded();
          }
          return true;
      }
  
      /**
       * Provides access to the actual collection in the object graph
       */
      public Collection getRealCollection()
      {
          return this.real;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/graph/Graph.java
  
  Index: Graph.java
  ===================================================================
  /* Copyright 2004-2004 The Apache Software Foundation
   *
   * Licensed 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.ojb.broker.util.graph;
  
  import java.util.Collection;
  import java.util.ArrayList;
  import java.util.Iterator;
  
  /**
   * Represents and object graph and provides means of traversing
   * <p>
   * A graph is not designed to be thread-safe
   */
  public class Graph
  {
      private Collection roots;
  
      /**
       * Represent an object graph where everything in <code>roots</code> is considered
       * a root element in the graph
       */
      public Graph(Collection roots)
      {
          this.roots = roots;
      }
  
      /**
       * Represent an object graph where <code>root</code> is the root element
in the graph
       */
      public Graph(Object root)
      {
          this.roots = new ArrayList(1);
          roots.add(root);
      }
  
      /**
       * Traverse the entire object graph passing each element into the approriate
       * callback defined in the <code>GraphVisitor</code>.
       * <p>
       * Implementors of the visitor are responsible for handling reference loops
       */
      public void each(GraphVisitor v)
      {
          for (Iterator iterator = roots.iterator(); iterator.hasNext();)
          {
              Object o = iterator.next();
              v.visitObject(new ObjectWrapper(o));
          }
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/graph/GraphVisitor.java
  
  Index: GraphVisitor.java
  ===================================================================
  /* Copyright 2004-2004 The Apache Software Foundation
   *
   * Licensed 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.ojb.broker.util.graph;
  
  /**
   * Visitor interface for persistent object graph traversal.
   * Implementors are responsible for handling loops in the graph.
   *
   * @see Graph
   */
  public interface GraphVisitor
  {
      /**
       * Will be called when an instance is being visited in graph traversal
       *
       * @param obj utility class with instance and persistence information about instance
being visited
       * @see ObjectWrapper
       */
      void visitObject(ObjectWrapper obj);
  
      /**
       * Will be called when a collection is being visited in graph traversal
       *
       * @param c utility class with collection and persistence information about collection
being visited
       * @see CollectionWrapper
       */
      void visitCollection(CollectionWrapper c);
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/graph/ObjectWrapper.java
  
  Index: ObjectWrapper.java
  ===================================================================
  /* Copyright 2004-2004 The Apache Software Foundation
   *
   * Licensed 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.ojb.broker.util.graph;
  
  import org.apache.ojb.broker.core.proxy.ProxyHelper;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.CollectionDescriptor;
  import org.apache.ojb.broker.metadata.MetadataManager;
  import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
  
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Vector;
  
  /**
   * Wraps a persistent instance
   */
  public class ObjectWrapper
  {
      private Object real = null;
      private MetadataManager man = MetadataManager.getInstance();
  
      ObjectWrapper(Object obj)
      {
          this.real = obj;
      }
  
      /**
       * Will traverse all collections defined on this instance in OJB metadata
       * via appropriate callbacks on the visitor
       */
      public void visitCollections(GraphVisitor v)
      {
          ClassDescriptor classDescriptor = man.getRepository().getDescriptorFor(real.getClass());
          Vector colDescs = classDescriptor.getCollectionDescriptors();
          for (Iterator iterator = colDescs.iterator(); iterator.hasNext();)
          {
              CollectionDescriptor cd = (CollectionDescriptor) iterator.next();
              PersistentField pf = cd.getPersistentField();
              Collection col = (Collection) pf.get(real);
              v.visitCollection(new CollectionWrapper(col));
          }
      }
  
      /**
       * Will traverse all object references defined on this instance via appropriate
       * callbacks on the visitor
       */
      public void visitReferences(GraphVisitor v)
      {
          ClassDescriptor classDescriptor = man.getRepository().getDescriptorFor(real.getClass());
          Vector refDescs = classDescriptor.getObjectReferenceDescriptors();
          for (Iterator iterator = refDescs.iterator(); iterator.hasNext();)
          {
              ObjectReferenceDescriptor rd = (ObjectReferenceDescriptor) iterator.next();
              v.visitObject(new ObjectWrapper(rd));
          }
      }
  
      /**
       * Returns true if the instance is not a proxy, true if the instance is a proxy
       * and has already been loaded, or false if it is a proxy and is not yet loaded
       */
      public boolean isMaterialized()
      {
          return ProxyHelper.isProxy(real) && ProxyHelper.isMaterialized(real);
      }
  
      /**
       * Is the wrapped instance a proxy?
       */
      public boolean isProxy()
      {
          return ProxyHelper.isProxy(real);
      }
  
      /**
       * Returns the wrapped instance
       */
      public Object getRealObject()
      {
          return real;
      }
  }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/util/graph/GraphTest.java
  
  Index: GraphTest.java
  ===================================================================
  package org.apache.ojb.broker.util.graph;
  
  import junit.framework.TestCase;
  import org.apache.ojb.broker.Person;
  import org.apache.ojb.broker.Project;
  
  import java.util.ArrayList;
  import java.util.Collection;
  
  public class GraphTest extends TestCase
  {
      public void testRootOnly()
      {
          Person person = new Person();
          final boolean calls[] = {false};
  
          Graph graph = new Graph(person);
          graph.each(new BaseVisitor()
          {
              public void visitObject(ObjectWrapper obj)
              {
                  calls[0] = true;
              }
          });
          assertTrue("Root object should have forced this call", calls[0]);
      }
  
      public void testRootWithCollection()
      {
          final Person person = new Person();
          Collection projects = new ArrayList();
          final Project project = new Project();
          Collection persons = new ArrayList();
          persons.add(person);
          project.setPersons(persons);
          projects.add(project);
          person.setProjects(projects);
  
          final boolean calls[] = {false, false, false, false};
  
          Graph graph = new Graph(person);
          graph.each(new BaseVisitor()
          {
              public void visitObject(ObjectWrapper obj)
              {
                  if (obj.getRealObject() == person && !calls[0])
                  {
                      calls[0] = true;
                      obj.visitCollections(this);
                  }
                  else if (obj.getRealObject() == project)
                  {
                      calls[2] = true;
                      obj.visitCollections(this);
                  }
                  else if (obj.getRealObject() == person && calls[0] && !calls[3])
                  {
                      calls[3] = true;
                  }
              }
  
              public void visitCollection(CollectionWrapper c)
              {
                  calls[1] = true;
                  c.visitElements(this);
              }
          });
          assertTrue("Root object should have forced this call", calls[0]);
          assertTrue("Collection not visited", calls[1]);
          assertTrue("Project not visited", calls[2]);
          assertTrue(calls[3]);
  
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message