AbstractRuntime.java
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.core.runtime;
13
14import java.util.Random;
15
16import org.jacoco.core.data.ExecutionDataStore;
17import org.jacoco.core.data.IExecutionDataVisitor;
18import org.jacoco.core.data.ISessionInfoVisitor;
19import org.jacoco.core.data.SessionInfo;
20
21/**
22 * Base {@link IRuntime} implementation.
23 *
24 * @author Marc R. Hoffmann
25 * @version 0.4.1.20101007204400
26 */
27public abstract class AbstractRuntime implements IRuntime {
28
29 /** store for execution data */
30 protected final ExecutionDataStore store;
31
32 /** access for this runtime instance */
33 protected final ExecutionDataAccess access;
34
35 private long startTimeStamp;
36
37 private String sessionId;
38
39 /**
40 * Creates a new runtime.
41 */
42 protected AbstractRuntime() {
43 store = new ExecutionDataStore();
44 access = new ExecutionDataAccess(store);
45 sessionId = generateSessionId();
46 }
47
48 /**
49 * Subclasses need to call this method in their {@link #startup()}
50 * implementation to record the timestamp of session startup.
51 */
52 protected final void setStartTimeStamp() {
53 startTimeStamp = System.currentTimeMillis();
54 }
55
56 public void setSessionId(final String id) {
57 sessionId = id;
58 }
59
60 public String getSessionId() {
61 return sessionId;
62 }
63
64 public final void collect(final IExecutionDataVisitor executionDataVisitor,
65 final ISessionInfoVisitor sessionInfoVisitor, final boolean reset) {
66 synchronized (store) {
67 if (sessionInfoVisitor != null) {
68 final SessionInfo info = new SessionInfo(sessionId,
69 startTimeStamp, System.currentTimeMillis());
70 sessionInfoVisitor.visitSessionInfo(info);
71 }
72 store.accept(executionDataVisitor);
73 if (reset) {
74 reset();
75 }
76 }
77 }
78
79 public final void reset() {
80 synchronized (store) {
81 store.reset();
82 setStartTimeStamp();
83 }
84 }
85
86 private String generateSessionId() {
87 return Integer.toHexString(new Random().nextInt());
88 }
89
90}