Coverage report

  %line %branch
net.sf.infrared.base.model.AggregateOperationTree
83% 
96% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  * 
 16  
  *
 17  
  *
 18  
  * Original Author:  binil.thoms (Tavant Technologies)
 19  
  * Contributor(s):   prashant.nair, subin.p
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.base.model;
 23  
 
 24  
 import java.io.Serializable;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 
 28  
 import net.sf.infrared.base.util.LoggingFactory;
 29  
 import net.sf.infrared.base.util.Merger;
 30  
 import net.sf.infrared.base.util.Tree;
 31  
 import net.sf.infrared.base.util.TreeNode;
 32  
 
 33  
 import org.apache.log4j.Logger;
 34  
 
 35  
 /**
 36  
  * Represents the aggregate of a set of operations in tree form.
 37  
  * 
 38  
  * <p>
 39  
  * This holds a Tree which has a dummy node as the root. The children of this
 40  
  * dummy node are TreeNodes representing AggregateExecutionTimes.
 41  
  * 
 42  
  * <p>
 43  
  * Initially an AggregateOperationTree starts out empty, and more data is added
 44  
  * to it by calls to the the merge(Tree) and merge(AggregateOperationTree)
 45  
  * methods.
 46  
  * 
 47  
  * @author binil.thomas
 48  
  * @author prashant.nair
 49  
  * @author subin.p
 50  
  */
 51  
 public class AggregateOperationTree implements Serializable {
 52  
 
 53  6
     private static final Logger log = LoggingFactory.getLogger(AggregateOperationTree.class);
 54  
 
 55  
     private Tree aggregateTree;
 56  
     
 57  14
     private Merger opMerger = new OperationTreeMerger();
 58  
     
 59  14
     private Merger aggMerger = new AggregateOperationTreeMerger();
 60  
 
 61  
     /**
 62  
      * Creates an empty AggregateOperationTree
 63  
      */
 64  14
     public AggregateOperationTree() {
 65  14
         aggregateTree = new Tree();
 66  14
         aggregateTree.setRoot(TreeNode.createTreeNode("dummy root"));
 67  14
     }
 68  
     
 69  
     /**
 70  
      * Merges an execution tree onto this AggregateOperationTree.
 71  
      * 
 72  
      * @param anOperation the tree of ExecutionTimer objects, which represents the
 73  
      *                    executions in one thread
 74  
      */
 75  
     public synchronized void merge(Tree anOperation) {
 76  13
         aggregateTree.getRoot().mergeAsChild(anOperation.getRoot(),  opMerger);
 77  13
         if (log.isDebugEnabled()) {
 78  0
             log.debug("Merged operation tree with root " + anOperation.getRoot().getValue());
 79  
         }
 80  13
     }
 81  
 
 82  
     /**
 83  
      * Merges another AggregateOperationTree onto this AggregateOperationTree. The
 84  
      * passed in AggregateOperationTree is left unchanged.
 85  
      * 
 86  
      * @param anOperation the tree of ExecutionTimer objects, which represents the
 87  
      *                    executions in one thread
 88  
      */
 89  
     public synchronized void merge(AggregateOperationTree otherTree) {
 90  
         // The root of an AggregateOperationTree is always a dummy node (this is to accomodate the
 91  
         // fact that there could be multiple entry points in an application).
 92  
         // Therefore, we can always safely ignore the dummy node and just merge the children
 93  1
         List othersChildren = otherTree.getAggregateTree().getRoot().getChildren();
 94  1
         TreeNode rootOfThis = getAggregateTree().getRoot(); // this is the dummy node we have
 95  1
         for (Iterator iter = othersChildren.iterator(); iter.hasNext();) {
 96  1
             TreeNode othersChild = (TreeNode) iter.next();
 97  1
             rootOfThis.mergeAsChild(othersChild, aggMerger);
 98  
         }
 99  1
         if (log.isDebugEnabled()) {
 100  0
             log.debug(this + " - Merged AggregateOperationTree " + otherTree);
 101  
         }
 102  1
     }
 103  
     
 104  
     public String toString(){
 105  1
         return aggregateTree.getRoot().toString();
 106  
     }
 107  
         
 108  
     public Tree getAggregateTree() {
 109  9
         return aggregateTree;
 110  
     }
 111  
 
 112  
     public void setAggregateTree(Tree aggregateTree) {
 113  0
         this.aggregateTree = aggregateTree;
 114  0
     }    
 115  
 }
 116  
 
 117  
 abstract class TimeMerger implements Merger {
 118  
     protected ExecutionTimer getExecutionTimer(TreeNode nodeWithATimer) {
 119  
         return (ExecutionTimer) nodeWithATimer.getValue();
 120  
     }
 121  
     
 122  
     protected AggregateExecutionTime getAggregateExecutionTime(TreeNode nodeWithAnAggregate) {
 123  
         return (AggregateExecutionTime) nodeWithAnAggregate.getValue();
 124  
     }
 125  
 }
 126  
 
 127  
 class OperationTreeMerger extends TimeMerger {
 128  
     public TreeNode createNewNode(TreeNode from) {
 129  
         ExecutionTimer et = getExecutionTimer(from);
 130  
         AggregateExecutionTime aet = new AggregateExecutionTime(et.getContext());
 131  
         aet.merge(et);
 132  
         return TreeNode.createTreeNode(aet);
 133  
     }
 134  
 
 135  
     public void mergeValue(TreeNode mergeOnto, TreeNode toMerge) {
 136  
         AggregateExecutionTime aet = getAggregateExecutionTime(mergeOnto);
 137  
         ExecutionTimer et = getExecutionTimer(toMerge);
 138  
         aet.merge(et);
 139  
     }
 140  
 
 141  
     public boolean isMatching(TreeNode original, TreeNode toMerge) {
 142  
         AggregateExecutionTime aet = getAggregateExecutionTime(original);
 143  
         ExecutionTimer et = getExecutionTimer(toMerge);
 144  
         
 145  
         return aet.getContext().equals( et.getContext() );
 146  
     }         
 147  
 }
 148  
 
 149  
 class AggregateOperationTreeMerger extends TimeMerger {
 150  
     public TreeNode createNewNode(TreeNode from) {
 151  
         AggregateExecutionTime aet = getAggregateExecutionTime(from);    
 152  
         return TreeNode.createTreeNode(aet);
 153  
     }
 154  
 
 155  
     public void mergeValue(TreeNode mergeOnto, TreeNode toMerge) {
 156  
         AggregateExecutionTime aet1 = getAggregateExecutionTime(mergeOnto);
 157  
         AggregateExecutionTime aet2 = getAggregateExecutionTime(toMerge);
 158  
         aet1.merge(aet2);
 159  
     }             
 160  
 
 161  
     public boolean isMatching(TreeNode original, TreeNode toMerge) {
 162  
         AggregateExecutionTime aet1 = getAggregateExecutionTime(original);
 163  
         AggregateExecutionTime aet2 = getAggregateExecutionTime(toMerge);
 164  
         
 165  
         return aet1.getContext().equals( aet2.getContext() );
 166  
     }
 167  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.