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