1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.util;
16  
17  /**
18   * Factory that creates {@link ReentryGuard} instances according to a requested type.
19   *
20   * <p>This class centralizes creation of the built-in guard implementations.
21   * Consumers can use the factory to obtain either a per-thread guard or a no-op
22   * guard depending on their needs.</p>
23   *
24   * @since 1.5.21
25   */
26  public class ReentryGuardFactory {
27  
28      /**
29       * Types of guards that can be produced by this factory.
30       *
31       * THREAD_LOCAL - returns a {@link ReentryGuard.ReentryGuardImpl} backed by a ThreadLocal.
32       * NOP - returns a {@link ReentryGuard.NOPRentryGuard} which never locks.
33       */
34      public enum GuardType {
35          THREAD_LOCAL,
36          NOP
37      }
38  
39  
40      /**
41       * Create a {@link ReentryGuard} for the given {@link GuardType}.
42       *
43       * <p>Returns a fresh instance of the requested guard implementation. The
44       * factory does not cache instances; callers may obtain separate instances
45       * as required.</p>
46       *
47       * <p>Thread-safety: this method is stateless and may be called concurrently
48       * from multiple threads.</p>
49       *
50       * @param guardType the type of guard to create; must not be {@code null}
51       * @return a new {@link ReentryGuard} instance implementing the requested semantics
52       * @throws NullPointerException if {@code guardType} is {@code null}
53       * @throws IllegalArgumentException if an unknown guard type is provided
54       * @since 1.5.21
55       */
56      public static ReentryGuard makeGuard(GuardType guardType) {
57          switch (guardType) {
58              case THREAD_LOCAL:
59                  return new ReentryGuard.ReentryGuardImpl();
60              case NOP:
61                  return new ReentryGuard.NOPRentryGuard();
62              default:
63                  throw new IllegalArgumentException("Unknown GuardType: " + guardType);
64          }
65      }
66  }