EMMA Coverage Report (generated Sun Aug 17 11:20:34 PDT 2014)
[all classes][net.digitaltsunami.tmeter.action]

COVERAGE SUMMARY FOR SOURCE FILE [TimerStatsAction.java]

nameclass, %method, %block, %line, %
TimerStatsAction.java100% (1/1)100% (7/7)95%  (98/103)95%  (21.8/23)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TimerStatsAction100% (1/1)100% (7/7)95%  (98/103)95%  (21.8/23)
processTimer (Timer): void 100% (1/1)85%  (29/34)86%  (6.8/8)
TimerStatsAction (): void 100% (1/1)100% (8/8)100% (3/3)
getAllTimerStatistics (): Collection 100% (1/1)100% (7/7)100% (1/1)
getAllTimerStatisticsSnapshot (): Collection 100% (1/1)100% (31/31)100% (6/6)
getTimerStatistics (String): TimerBasicStatistics 100% (1/1)100% (6/6)100% (1/1)
getTimerStatisticsSnapshot (String): TimerBasicStatistics 100% (1/1)100% (13/13)100% (2/2)
reset (): void 100% (1/1)100% (4/4)100% (2/2)

1/* __copyright_begin__
2   Copyright 2011 Dan Hagberg
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__copyright_end__ */
16/**
17 * 
18 */
19package net.digitaltsunami.tmeter.action;
20 
21import java.util.Collection;
22import java.util.HashSet;
23import java.util.Set;
24import java.util.concurrent.ConcurrentHashMap;
25 
26import net.digitaltsunami.tmeter.Timer;
27import net.digitaltsunami.tmeter.TimerBasicStatistics;
28 
29/**
30 * An implementation of {@link TimerAction} that provides basic statistics for all
31 * timers processed by this action.  Basic statistics may be retrieved for a specific
32 * task or the entire set of tasks. 
33 * @author dhagberg 
34 * 
35 */
36public class TimerStatsAction extends TimerAction {
37 
38    private final ConcurrentHashMap<String, TimerBasicStatistics> statsByTask;
39 
40    public TimerStatsAction() {
41        super();
42        statsByTask = new ConcurrentHashMap<String, TimerBasicStatistics>();
43    }
44 
45    /*
46     * (non-Javadoc)
47     * 
48     * @see net.digitaltsunami.tmeter.TimeAction#timerCompleted(net.digitaltsunami.tmeter.Timer)
49     */
50    @Override
51    protected void processTimer(Timer timer) {
52        TimerBasicStatistics stat = statsByTask.get(timer.getTaskName());
53        if (stat == null) {
54            // Task not found in list. Create a new entry
55            stat = new TimerBasicStatistics(timer.getTaskName());
56            // Place it in the map
57            TimerBasicStatistics currentStat = statsByTask.putIfAbsent(timer.getTaskName(),
58                        stat);
59            // If the same entry was not returned, another thread created
60            // during setup. Use the existing entry.
61            if (currentStat != null && currentStat != stat) {
62                stat = currentStat;
63            }
64        }
65        stat.addTimer(timer);
66    }
67    
68    /**
69     * Clears all accumulated statistics.
70     * 
71     * @see net.digitaltsunami.tmeter.TimeAction#reset()
72     */
73    @Override
74    protected void reset() {
75        statsByTask.clear();
76    }
77 
78    /**
79     * Return a live instance of {@link TimerBasicStatistics} for the provided
80     * task name. Internal values will be modified as this action processes
81     * timers.
82     * 
83     * @param taskName
84     * @return Live instance of {@link TimerBasicStatistics} or null if that
85     *         task has not yet been processed by this action.
86     */
87    public TimerBasicStatistics getTimerStatistics(String taskName) {
88        return statsByTask.get(taskName);
89    }
90 
91    /**
92     * Return a snapshot instance of {@link TimerBasicStatistics} for the
93     * provided task name. Internal values will be not be modified as this
94     * action processes timers.
95     * 
96     * @param taskName
97     * @return Snapshot of {@link TimerBasicStatistics} for task name or null if
98     *         that task has not yet been processed by this action.
99     */
100    public TimerBasicStatistics getTimerStatisticsSnapshot(String taskName) {
101        TimerBasicStatistics stat = statsByTask.get(taskName);
102        return stat == null ? null : stat.getSnapshot();
103    }
104    
105    /**
106     * Return a collection of live instances of {@link TimerBasicStatistics} for
107     * all tasks processed up to the point of invoking this method. Internal
108     * values will be modified as this action processes timers.
109     * 
110     * @return Live instance of {@link TimerBasicStatistics}
111     */
112    public Collection<TimerBasicStatistics> getAllTimerStatistics() {
113        return new HashSet<TimerBasicStatistics>(statsByTask.values());
114 
115    }
116 
117    /**
118     * Return a collection of snapshot instances of {@link TimerBasicStatistics}
119     * for all tasks processed up to the point of invoking this method. Internal
120     * values will be modified as this action processes timers.
121     * 
122     * @return Snapshot instance of {@link TimerBasicStatistics}
123     */
124    public Collection<TimerBasicStatistics> getAllTimerStatisticsSnapshot() {
125        // Create a working copy to avoid concurrent modification exception while building copy.
126        Set<TimerBasicStatistics> workingCopy = new HashSet<TimerBasicStatistics>(statsByTask.values());
127        Set<TimerBasicStatistics> snapshotSet = new HashSet<TimerBasicStatistics>(workingCopy.size());
128        for(TimerBasicStatistics stat : workingCopy) {
129            snapshotSet.add(stat.getSnapshot());
130        }
131        return snapshotSet;
132    }
133 
134}

[all classes][net.digitaltsunami.tmeter.action]
EMMA 2.1.5320 (stable) (C) Vladimir Roubtsov