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