Coverage report

  %line %branch
net.sf.infrared.base.model.LayerTimeRepository
94% 
91% 

 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.thomas (Tavant Technologies)
 19  
  * Contributor(s):   -;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.base.model;
 23  
 
 24  
 import java.io.Serializable;
 25  
 import java.util.Collection;
 26  
 import java.util.HashMap;
 27  
 import java.util.HashSet;
 28  
 import java.util.Map;
 29  
 import java.util.Set;
 30  
 import java.util.regex.Matcher;
 31  
 import java.util.regex.Pattern;
 32  
 
 33  
 /**
 34  
  * Holds statistics of various layers, the time spend in those layers and the aggregate time of
 35  
  * each execution in those layers.
 36  
  * 
 37  
  * @author binil.thomas
 38  
  */
 39  9
 public class LayerTimeRepository implements Serializable, Cloneable {
 40  
     // map of String hierarchical layer name -> LayerTime objects
 41  9
     private Map hierarchicalLayerTimes = new HashMap();
 42  
 
 43  
     // map of hierarchical layer name -> [Map of ExecutionContext -> AggregateExecutionTime objects]
 44  
     // This identifies aggregate timings of executions that happenend within a hierarchical layer
 45  9
     private Map hierarchicalAggExecTimes = new HashMap();
 46  
         
 47  
     /**
 48  
      * Gets the hierarchical layers that executed in this application
 49  
      */
 50  
     public String[] getHierarchicalLayers() {
 51  17
         Set ret = new HashSet();
 52  17
         ret.addAll(hierarchicalLayerTimes.keySet());
 53  17
         ret.addAll(hierarchicalAggExecTimes.keySet());
 54  17
         return (String[]) ret.toArray(new String[ret.size()]);
 55  
     }
 56  
     
 57  
     /**
 58  
      * Gets the absolute layers that executed in this application
 59  
      */
 60  
     public String[] getAbsoluteLayers() {
 61  
         // @TODO cache this calculation somewhere
 62  3
         String[] hLayers = getHierarchicalLayers();
 63  3
         Set aLayers = new HashSet();        
 64  8
         for (int i = 0; i < hLayers.length; i++) {
 65  5
             String hLayer = hLayers[i];
 66  5
             aLayers.addAll(parseAbsoluteLayers(hLayer));
 67  
         }
 68  
         
 69  3
         return (String[]) aLayers.toArray(new String[aLayers.size()]);
 70  
     }
 71  
     
 72  
     /**
 73  
      * Gets the time spend in an hierarchical layer
 74  
      */
 75  
     public long getTimeInHierarchicalLayer(String hLayer) {
 76  10
         return getLayerTimeFromMap(hierarchicalLayerTimes, hLayer);
 77  
     }
 78  
 
 79  
     public long getTimeInAbsoluteLayer(String aLayer) {
 80  5
         String dotPrefixedLayer = "." + aLayer;
 81  5
         long time = 0;
 82  5
         String[] hLayers = getHierarchicalLayers();
 83  14
         for (int i = 0; i < hLayers.length; i++) {
 84  9
             String hLayer = hLayers[i];
 85  9
             if (hLayer.equals(aLayer) || (hLayer.endsWith(dotPrefixedLayer))) {
 86  5
                 time += getTimeInHierarchicalLayer(hLayer);
 87  
             }
 88  
         }
 89  5
         return time;
 90  
     }
 91  
     
 92  
     /**
 93  
      * Gets the aggregate of executions in that happenend in a given layer
 94  
      */
 95  
     public AggregateExecutionTime[] getExecutionsInHierarchicalLayer(String hLayer) {
 96  16
         return getExecutionsFromMap(hierarchicalAggExecTimes, hLayer);
 97  
     }
 98  
     
 99  
     public AggregateExecutionTime[] getExecutionsInAbsoluteLayer(String aLayer) {
 100  6
         Map ctxToAggExecs = new HashMap();
 101  
         
 102  6
         String[] hLayers = getHierarchicalLayers();
 103  16
         for (int i = 0; i < hLayers.length; i++) {
 104  10
             String hLayer = hLayers[i];
 105  10
             AggregateExecutionTime[] aets = getExecutionsInHierarchicalLayer(hLayer);
 106  32
             for (int j = 0; j < aets.length; j++) {
 107  22
                 AggregateExecutionTime aet = aets[j];
 108  22
                 ExecutionContext ctx = aet.getContext();   
 109  22
                 if (! ctx.getLayer().equals(aLayer)) {
 110  11
                     continue;
 111  
                 }
 112  11
                 AggregateExecutionTime merged = (AggregateExecutionTime) ctxToAggExecs.get(ctx);
 113  11
                 if (merged == null) {
 114  10
                     merged = new AggregateExecutionTime(ctx);
 115  10
                     ctxToAggExecs.put(ctx, merged);
 116  
                 }
 117  11
                 merged.merge(aet);
 118  
             }
 119  
         }
 120  
         
 121  6
         return (AggregateExecutionTime[]) ctxToAggExecs.values().toArray(new AggregateExecutionTime[0]);
 122  
     }
 123  
     
 124  
     /**
 125  
      * Record that the specified time was spend in the specified hierarchical layer
 126  
      */
 127  
     public void mergeHierarchicalLayerTime(String layerName, long time) {        
 128  3
         getAggregateHierarchicalLayerTime(layerName).addToTime(time);        
 129  3
     }
 130  
     
 131  
     /**
 132  
      * Record that some time was spend in a layer, where the later name and time are
 133  
      * given as a LayerTime object
 134  
      */
 135  
     public void mergeHierarchicalLayerTime(LayerTime lt) {
 136  3
         mergeHierarchicalLayerTime(lt.getLayer(), lt.getTime());
 137  3
     }
 138  
     
 139  
     /**
 140  
      * Record that a specified list of executions happenend in the specified hierarchical layer.
 141  
      * Each execution is denoted as an ExecutionTimer object
 142  
      */
 143  
     public void mergeExecutionTimes(String hLayer, ExecutionTimer[] times) {
 144  8
         for (int i = 0; i < times.length; i++) {
 145  5
             ExecutionTimer et = times[i];
 146  5
             getAggregateExecutionTime(hLayer, et.getContext()).merge(et); // hierarchical
 147  
         }
 148  3
     }
 149  
     
 150  
     /**
 151  
      * Record that a specified list of executions happenend in the specified hierarchical layer
 152  
      * Each type of execution is denoted as an AggregateExecutionTime object
 153  
      */
 154  
     public void mergeExecutionTimes(String layerName, AggregateExecutionTime[] times) {
 155  0
         for (int i = 0; i < times.length; i++) {
 156  0
             AggregateExecutionTime aet = times[i];
 157  0
             getAggregateExecutionTime(layerName, aet.getContext()).merge(aet); // hierarchical            
 158  
         }
 159  0
     }
 160  
     
 161  
     LayerTime getAggregateHierarchicalLayerTime(String layer) {
 162  3
         LayerTime lt = (LayerTime) hierarchicalLayerTimes.get(layer);
 163  3
         if (lt == null) {
 164  2
             lt = new LayerTime(layer);
 165  2
             hierarchicalLayerTimes.put(layer, lt);
 166  
         }
 167  3
         return lt;
 168  
     }
 169  
     
 170  
     AggregateExecutionTime getAggregateExecutionTime(String hLayer, ExecutionContext ctx) {
 171  5
         Map m = (Map) hierarchicalAggExecTimes.get(hLayer);
 172  5
         if (m == null) {
 173  2
             m = new HashMap();
 174  2
             hierarchicalAggExecTimes.put(hLayer, m);
 175  2
             AggregateExecutionTime aet = new AggregateExecutionTime(ctx);
 176  2
             m.put(ctx, aet);
 177  2
             return aet;
 178  
         }
 179  
         
 180  3
         AggregateExecutionTime aet = (AggregateExecutionTime) m.get(ctx);
 181  3
         if (aet == null) {
 182  3
             aet = new AggregateExecutionTime(ctx);
 183  3
             m.put(ctx, aet);
 184  
         }
 185  3
         return aet;
 186  
     }    
 187  
     
 188  
     long getLayerTimeFromMap(Map map, String l) {
 189  10
         LayerTime lt = (LayerTime) map.get(l);
 190  10
         if (lt != null) {
 191  10
             return lt.getTime();
 192  
         } else {
 193  0
             return 0;
 194  
         }
 195  
     }
 196  
     
 197  
     AggregateExecutionTime[] getExecutionsFromMap(Map map, String l) {
 198  16
         Map innerMap = (Map) map.get(l);
 199  16
         if (innerMap == null) {
 200  1
             return new AggregateExecutionTime[0];
 201  
         }
 202  15
         return (AggregateExecutionTime[]) innerMap.values().toArray(new AggregateExecutionTime[0]);
 203  
     }
 204  
     
 205  
     Collection parseAbsoluteLayers(String hLayer) {
 206  8
         Set absLayers = new HashSet();
 207  
 
 208  8
         Pattern p = Pattern.compile("[\\w]+[\\.]");
 209  8
         Matcher m = p.matcher(hLayer);
 210  8
         int i = 0;
 211  13
         while (m.find(i)) {
 212  5
             i = m.end();
 213  5
             String aLayer = m.group();
 214  5
             aLayer = aLayer.substring(0, aLayer.length() - 1);
 215  5
             absLayers.add(aLayer);
 216  
         }
 217  8
         absLayers.add(hLayer.substring(i));
 218  8
         return absLayers;
 219  
     }
 220  
 }

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