View Javadoc
1   /*
2    * Copyright (c) 2020, Google LLC  and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * http://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.logging;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.List;
16  
17  /**
18   * Singleton that collects performance logs.
19   *
20   * @since 5.10
21   */
22  public class PerformanceLogContext {
23  	/** Singleton instance that stores the statistics. */
24  	private static final PerformanceLogContext INSTANCE = new PerformanceLogContext();
25  
26  	/** List that stores events as performance logs. */
27  	private static final ThreadLocal<List<PerformanceLogRecord>> eventRecords = ThreadLocal
28  			.withInitial(ArrayList::new);
29  
30  	private PerformanceLogContext() {
31  	}
32  
33  	/**
34  	 * Get the instance of the context.
35  	 *
36  	 * @return instance of performance log context.
37  	 */
38  	public static PerformanceLogContext getInstance() {
39  		return INSTANCE;
40  	}
41  
42  	/**
43  	 * Get the unmodifiable list of events as performance records.
44  	 *
45  	 * @return unmodifiable list of events as performance logs.
46  	 */
47  	public List<PerformanceLogRecord> getEventRecords() {
48  		return Collections.unmodifiableList(eventRecords.get());
49  	}
50  
51  	/**
52  	 * Adds a performance log record to the current list of events.
53  	 *
54  	 * @param record
55  	 *            performance log record that is going to be added.
56  	 */
57  	public void addEvent(PerformanceLogRecord record) {
58  		eventRecords.get().add(record);
59  	}
60  
61  	/**
62  	 * Removes all of the existing records from the current list of events.
63  	 */
64  	public void cleanEvents() {
65  		eventRecords.remove();
66  	}
67  }