PerformanceLogContext.java

  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. package org.eclipse.jgit.logging;

  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.List;

  14. /**
  15.  * Singleton that collects performance logs.
  16.  *
  17.  * @since 5.10
  18.  */
  19. public class PerformanceLogContext {
  20.     /** Singleton instance that stores the statistics. */
  21.     private static final PerformanceLogContext INSTANCE = new PerformanceLogContext();

  22.     /** List that stores events as performance logs. */
  23.     private static final ThreadLocal<List<PerformanceLogRecord>> eventRecords = ThreadLocal
  24.             .withInitial(ArrayList::new);

  25.     private PerformanceLogContext() {
  26.     }

  27.     /**
  28.      * Get the instance of the context.
  29.      *
  30.      * @return instance of performance log context.
  31.      */
  32.     public static PerformanceLogContext getInstance() {
  33.         return INSTANCE;
  34.     }

  35.     /**
  36.      * Get the unmodifiable list of events as performance records.
  37.      *
  38.      * @return unmodifiable list of events as performance logs.
  39.      */
  40.     public List<PerformanceLogRecord> getEventRecords() {
  41.         return Collections.unmodifiableList(eventRecords.get());
  42.     }

  43.     /**
  44.      * Adds a performance log record to the current list of events.
  45.      *
  46.      * @param record
  47.      *            performance log record that is going to be added.
  48.      */
  49.     public void addEvent(PerformanceLogRecord record) {
  50.         eventRecords.get().add(record);
  51.     }

  52.     /**
  53.      * Removes all of the existing records from the current list of events.
  54.      */
  55.     public void cleanEvents() {
  56.         eventRecords.remove();
  57.     }
  58. }