001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 *  Copyright (C) 1999-2025, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *     or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.core.util;
016
017/**
018 * Factory that creates {@link ReentryGuard} instances according to a requested type.
019 *
020 * <p>This class centralizes creation of the built-in guard implementations.
021 * Consumers can use the factory to obtain either a per-thread guard or a no-op
022 * guard depending on their needs.</p>
023 *
024 * @since 1.5.21
025 */
026public class ReentryGuardFactory {
027
028    /**
029     * Types of guards that can be produced by this factory.
030     *
031     * THREAD_LOCAL - returns a {@link ReentryGuard.ReentryGuardImpl} backed by a ThreadLocal.
032     * NOP - returns a {@link ReentryGuard.NOPRentryGuard} which never locks.
033     */
034    public enum GuardType {
035        THREAD_LOCAL,
036        NOP
037    }
038
039
040    /**
041     * Create a {@link ReentryGuard} for the given {@link GuardType}.
042     *
043     * <p>Returns a fresh instance of the requested guard implementation. The
044     * factory does not cache instances; callers may obtain separate instances
045     * as required.</p>
046     *
047     * <p>Thread-safety: this method is stateless and may be called concurrently
048     * from multiple threads.</p>
049     *
050     * @param guardType the type of guard to create; must not be {@code null}
051     * @return a new {@link ReentryGuard} instance implementing the requested semantics
052     * @throws NullPointerException if {@code guardType} is {@code null}
053     * @throws IllegalArgumentException if an unknown guard type is provided
054     * @since 1.5.21
055     */
056    public static ReentryGuard makeGuard(GuardType guardType) {
057        switch (guardType) {
058            case THREAD_LOCAL:
059                return new ReentryGuard.ReentryGuardImpl();
060            case NOP:
061                return new ReentryGuard.NOPRentryGuard();
062            default:
063                throw new IllegalArgumentException("Unknown GuardType: " + guardType);
064        }
065    }
066}