1 /* 2 * Copyright 2020 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.perfmark; 18 19 import java.io.Closeable; 20 21 /** 22 * TaskCloseable is a helper class to simplify the closing of PerfMark tasks. It should be used in a 23 * try-with-resources block so that PerfMark tasks are recorded even in the event of exceptions. 24 * 25 * <p>Implementation note: This would normally implement {@code AutoCloseable}, but that is not 26 * available in Java 6. A future version of PerfMark may implement the parent interface instead. 27 * 28 * 29 * @since 0.23.0 30 */ 31 public final class TaskCloseable implements Closeable { 32 33 static final TaskCloseable INSTANCE = new TaskCloseable(); 34 35 /** 36 * Stops the opened task. See {@link PerfMark#traceTask(String)}. 37 */ 38 @Override close()39 public void close() { 40 PerfMark.stopTask(); 41 } 42 TaskCloseable()43 private TaskCloseable() {} 44 } 45