1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 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 *******************************************************************************/ 12 package org.jacoco.core.data; 13 14 import static java.lang.Math.max; 15 import static java.lang.Math.min; 16 17 import java.util.ArrayList; 18 import java.util.Collections; 19 import java.util.List; 20 21 /** 22 * Container to collect and merge session {@link SessionInfo} objects. A 23 * instance of this class is not thread safe. 24 */ 25 public class SessionInfoStore implements ISessionInfoVisitor { 26 27 private final List<SessionInfo> infos = new ArrayList<SessionInfo>(); 28 29 /** 30 * Tests whether the store is empty. 31 * 32 * @return <code>true</code> if the store is empty 33 */ isEmpty()34 public boolean isEmpty() { 35 return infos.isEmpty(); 36 } 37 38 /** 39 * Returns all {@link SessionInfo} objects currently contained in the store. 40 * The info objects are ordered by its natural ordering (i.e. by the dump 41 * time stamp). 42 * 43 * @return list of stored {@link SessionInfo} objects 44 */ getInfos()45 public List<SessionInfo> getInfos() { 46 final List<SessionInfo> copy = new ArrayList<SessionInfo>(infos); 47 Collections.sort(copy); 48 return copy; 49 } 50 51 /** 52 * Returns a new session info with the given id that contains a merged 53 * version from all contained version. The start timestamp is the minimum of 54 * all contained sessions, the dump timestamp the maximum of all contained 55 * sessions. If no session is currently contained both timestamps are set to 56 * <code>0</code>. 57 * 58 * @param id 59 * identifier for the merged session info 60 * @return new {@link SessionInfo} object 61 * 62 */ getMerged(final String id)63 public SessionInfo getMerged(final String id) { 64 if (infos.isEmpty()) { 65 return new SessionInfo(id, 0, 0); 66 } 67 long start = Long.MAX_VALUE; 68 long dump = Long.MIN_VALUE; 69 for (final SessionInfo i : infos) { 70 start = min(start, i.getStartTimeStamp()); 71 dump = max(dump, i.getDumpTimeStamp()); 72 } 73 return new SessionInfo(id, start, dump); 74 } 75 76 /** 77 * Writes all contained {@link SessionInfo} objects into the given visitor. 78 * The info objects are emitted in chronological order by dump timestamp. 79 * 80 * @param visitor 81 * visitor to emit {@link SessionInfo} objects to 82 */ accept(final ISessionInfoVisitor visitor)83 public void accept(final ISessionInfoVisitor visitor) { 84 for (final SessionInfo i : getInfos()) { 85 visitor.visitSessionInfo(i); 86 } 87 } 88 89 // === ISessionInfoVisitor === 90 visitSessionInfo(final SessionInfo info)91 public void visitSessionInfo(final SessionInfo info) { 92 infos.add(info); 93 } 94 95 } 96