View Javadoc
1   package com.atlassian.plugin.instrumentation;
2   
3   import com.atlassian.instrumentation.operations.OpTimer;
4   
5   import javax.annotation.Nonnull;
6   import java.io.Closeable;
7   import java.util.Optional;
8   
9   import static com.google.common.base.Preconditions.checkNotNull;
10  
11  /**
12   * Wrapper around an {@link OpTimer} that may be safely used even if that class is not present in the class loader.
13   * <p>
14   * This wrapper implements {@link Closeable}
15   * <p>
16   * Note to maintainers: extreme care must be taken to ensure that <code>OpTimer</code> not accessed at runtime if it is
17   * not present.
18   *
19   * @see PluginSystemInstrumentation
20   * @since 4.1
21   */
22  public class Timer implements Closeable {
23      private final Optional<OpTimer> opTimer;
24  
25      Timer(@Nonnull Optional<OpTimer> opTimer) {
26          this.opTimer = checkNotNull(opTimer);
27      }
28  
29      /**
30       * Retrieve the timer, if instrumentation is present and enabled.
31       */
32      public Optional<OpTimer> getOpTimer() {
33          return opTimer;
34      }
35  
36      /**
37       * End the timer, if instrumentation is present and enabled.
38       */
39      @Override
40      public void close() {
41          if (opTimer.isPresent()) {
42              opTimer.get().end();
43          }
44      }
45  }