001    package org.shiftone.jrat.provider.tree.ui.summary;
002    
003    import org.shiftone.jrat.core.MethodKey;
004    import org.shiftone.jrat.provider.tree.ui.TraceTreeNode;
005    
006    import java.util.*;
007    
008    /**
009     * @author (jeff@shiftone.org) Jeff Drost
010     */
011    public class MethodSummaryModel {
012    
013        private final List methodList = new ArrayList(); // <MethodSummary>
014        private final Map methodMap = new HashMap(); // <MethodKey, MethodSummary>
015        private final long totalMethodDuration;
016    
017        public MethodSummaryModel(TraceTreeNode node) {
018            process(node);
019            totalMethodDuration = calculateTotalMethodDuration();
020        }
021    
022    
023        public long getTotalMethodDuration() {
024            return totalMethodDuration;
025        }
026    
027        private long calculateTotalMethodDuration() {
028            long duration = 0;
029            for (Iterator i = methodList.iterator(); i.hasNext();) {
030                MethodSummary summary = (MethodSummary) i.next();
031                Long d = (Long) summary.getTotalMethodDuration();
032                if (d != null) {
033                    duration += d.longValue();
034                }
035            }
036            return duration;
037        }
038    
039    
040        private void process(TraceTreeNode node) {
041    
042            if (!node.isRootNode()) {
043                MethodKey methodKey = node.getMethodKey();
044                MethodSummary method = getMethod(methodKey);
045                method.addStatistics(node);
046            }
047    
048            for (int i = 0; i < node.getChildCount(); i++) {
049                TraceTreeNode child = node.getChildNodeAt(i);
050                process(child);
051            }
052        }
053    
054    
055        private MethodSummary getMethod(MethodKey methodKey) {
056            MethodSummary summary = (MethodSummary) methodMap.get(methodKey);
057            if (summary == null) {
058                summary = new MethodSummary(methodKey);
059                methodMap.put(methodKey, summary);
060                methodList.add(summary);
061            }
062            return summary;
063        }
064    
065    
066        public List getMethodSummaryList() {
067            return Collections.unmodifiableList(methodList);
068        }
069    
070        public Map getMethodSummaryMap() {
071            return Collections.unmodifiableMap(methodMap);
072        }
073    }