001/*
002 * ============================================================================
003 * Copyright © 2002-2026 by Thomas Thrien.
004 * All Rights Reserved.
005 * ============================================================================
006 *
007 * Licensed to the public under the agreements of the GNU Lesser General Public
008 * License, version 3.0 (the "License"). You may obtain a copy of the License at
009 *
010 *      http://www.gnu.org/licenses/lgpl.html
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015 * License for the specific language governing permissions and limitations
016 * under the License.
017 */
018
019package org.tquadrat.foundation.perflog;
020
021import static java.util.Arrays.asList;
022import static org.apiguardian.api.API.Status.STABLE;
023import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
024import static org.tquadrat.foundation.perflog.PerfLogUtils.createPerformanceSectionName;
025
026import java.util.Optional;
027
028import org.apiguardian.api.API;
029import org.tquadrat.foundation.annotation.ClassVersion;
030import org.tquadrat.foundation.perflog.internal.PerfLogManagerImpl;
031
032/**
033 *  <p>{@summary This interface describes the Performance Logging and
034 *  Monitoring Manager that is the frontend to the
035 *  {@linkplain PerfLogMBean Performance Logging and Monitoring MBean}
036 *  that does the work behind the scenes.}</p>
037 *  <p>Instances of {@code PerfLogManager} are thread-safe, so they can be
038 *  created as {@code static}:</p>
039 *  {@include ${javadoc}/sample1a.txt:SOURCE}
040 *  <p>or as attributes as well.</p>
041 *  <p>Because {@code PerfLogManager} implements
042 *  {@link AutoCloseable}
043 *  it can be used with {@code try-with-resources}, too:</p>
044 *  {@include ${javadoc}/sample1c.txt:SOURCE}
045 *  <p>If an instance is abandoned, it will perform an automatic
046 *  clean-up.</p>
047 *
048 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
049 *  @version $Id: PerfLogManager.java 1246 2026-05-16 14:07:00Z tquadrat $
050 *  @since 0.25.0
051 *
052 *  @UMLGraph.link
053 */
054@ClassVersion( sourceVersion = "$Id: PerfLogManager.java 1246 2026-05-16 14:07:00Z tquadrat $" )
055@API( status = STABLE, since = "0.25.0" )
056public sealed interface PerfLogManager extends AutoCloseable
057    permits PerfLogManagerImpl
058{
059        /*---------*\
060    ====** Methods **==========================================================
061        \*---------*/
062    /**
063     *  {@inheritDoc}
064     *  <p>After {@code close()} is called, calls to other methods of this
065     *  instance may result in an
066     *  {@link IllegalStateException}
067     *  to be thrown.</p>
068     */
069    @Override
070    public void close();
071
072    /**
073     *  <p>{@summary Creates a performance tracker for the
074     *  {@link PerformanceSection}
075     *  with the given name.}</p>
076     *  <p>The return value will be empty if the performance section is
077     *  currently ignored.</p>
078     *  <p>If there is currently no performance section defined with the given
079     *  name, one will be created with default values.</p>
080     *
081     *  @param  name    The name of the performance section.
082     *  @return An instance of
083     *      {@link Optional}
084     *      that holds the new tracker.
085     *  @throws IllegalStateException
086     *      {@link #close()}
087     *      was already called on this instance.
088     */
089    public default Optional<PerformanceTracker> createPerformanceTracker( final String name ) throws IllegalStateException
090    {
091        final var retValue = createPerformanceTracker( createPerformanceSectionName( name ) );
092
093        //---* Done *----------------------------------------------------------
094        return retValue;
095    }   //  createPerformanceTracker()
096
097    /**
098     *  <p>{@summary Creates a performance tracker for the
099     *  {@link PerformanceSection}
100     *  with the given name.}</p>
101     *  <p>The return value will be empty if the performance section is
102     *  currently ignored.</p>
103     *  <p>If there is currently no performance section defined with the given
104     *  name, one will be created with default values.</p>
105     *
106     *  @param  name    The name of the performance section.
107     *  @return An instance of
108     *      {@link Optional}
109     *      that holds the new tracker.
110     *  @throws IllegalStateException
111     *      {@link #close()}
112     *      was already called on this instance.
113     */
114    public Optional<PerformanceTracker> createPerformanceTracker( final PerformanceSectionName name ) throws IllegalStateException;
115
116    /**
117     *  <p>{@summary Returns the performance section specified by the given
118     *  name.}</p>
119     *  <p>Delegates to
120     *  {@link PerfLogMBean#getPerformanceSection(PerformanceSectionName)}</p>
121     *
122     *  @param  name    The name of the performance section.
123     *  @return An instance of
124     *      {@link Optional}
125     *      that holds the retrieved performance section.
126     *  @throws IllegalStateException
127     *      {@link #close()}
128     *      was already called on this instance.
129     */
130    public Optional<PerformanceSection> getPerformanceSection( final PerformanceSectionName name ) throws IllegalStateException;
131
132    /**
133     *  <p>{@summary Loads the
134     *  {@linkplain PerformanceSection Performance Section definitions}.}</p>
135     *  <p>Because these definitions are stored globally in the
136     *  {@linkplain PerfLogMBean Performance Logging and Monitorin MBean},
137     *  they need to be loaded only once. But overwriting them does not harm,
138     *  other than performance.</p>
139     *  <p>Each instance of {@code PerfLogManager} shares the same instances
140     *  of
141     *  {@link PerformanceSection}.</p>
142     *
143     *  @param  definitions The definitions for the performance sections.
144     *  @throws IllegalStateException
145     *      {@link #close()}
146     *      was already called on this instance.
147     */
148    @SuppressWarnings( "NewMethodNamingConvention" )
149    public default void loadPerformanceSectionDefinitions( final PerformanceSection... definitions ) throws IllegalStateException
150    {
151        loadPerformanceSectionDefinitions( asList( requireNonNullArgument( definitions, "definitions" ) ) );
152    }   //  loadPerformanceSectionDefinitions()
153
154    /**
155     *  <p>{@summary Loads the
156     *  {@linkplain PerformanceSection Performance Section definitions}.}</p>
157     *  <p>Because these definitions are stored globally in the
158     *  {@linkplain PerfLogMBean Performance Logging and Monitorin MBean},
159     *  they need to be loaded only once. But overwriting them does not harm,
160     *  other than performance.</p>
161     *  <p>Each instance of {@code PerfLogManager} shares the same instances
162     *  of
163     *  {@link PerformanceSection}.</p>
164     *
165     *  @param  definitions The definitions for the performance sections.
166     *  @throws IllegalStateException
167     *      {@link #close()}
168     *      was already called on this instance.
169     */
170    @SuppressWarnings( "NewMethodNamingConvention" )
171    public void loadPerformanceSectionDefinitions( final Iterable<PerformanceSection> definitions ) throws IllegalStateException;
172}
173//  interface PerfLogManager
174
175/*
176 *  End of File
177 */