001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2009-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util;
037
038
039
040/**
041 * This enumeration defines a set of thread safety levels that may be used to
042 * indicate whether the associated code is safe to be accessed concurrently
043 * by multiple threads.
044 */
045public enum ThreadSafetyLevel
046{
047  /**
048   * The associated code is completely threadsafe and may be accessed
049   * concurrently by any number of threads, subject to the constraints described
050   * in the {@link ThreadSafety} documentation.
051   */
052  COMPLETELY_THREADSAFE,
053
054
055
056  /**
057   * The associated code is mostly threadsafe, but there may be some methods
058   * which are not safe to be invoked when multiple threads are accessing an
059   * instance concurrently.  The class-level documentation for a class including
060   * this thread safety level should include comments indicating which methods
061   * are not threadsafe, and those methods should also be marked with their own
062   * {@code ThreadSafety} annotations using the {@link #METHOD_NOT_THREADSAFE}
063   * level.
064   */
065  MOSTLY_THREADSAFE,
066
067
068
069  /**
070   * The associated code is mostly not threadsafe, but there may be some methods
071   * which are safe to be invoked concurrently by multiple threads.  The
072   * class-level documentation for a class including this thread safety level
073   * should include comments indicating which methods are threadsafe, and those
074   * methods should also be marked with their own {@code ThreadSafety}
075   * annotations using the {@link #METHOD_THREADSAFE} level.
076   */
077  MOSTLY_NOT_THREADSAFE,
078
079
080
081  /**
082   * The associated code is not threadsafe.  Unless otherwise noted, multiple
083   * threads may not attempt to invoke methods on the same instance of objects
084   * of this type without external synchronization.
085   */
086  NOT_THREADSAFE,
087
088
089
090  /**
091   * Methods declared in the associated interface or abstract class must be
092   * threadsafe in classes which implement that interface or extend that
093   * abstract class.  No guarantees will be made about the thread safety of
094   * other methods contained in that class which are not declared in the parent
095   * interface or superclass.
096   */
097  INTERFACE_THREADSAFE,
098
099
100
101  /**
102   * Methods declared in the associated interface or abstract class are not
103   * required to be threadsafe and classes which call them must not rely on the
104   * ability to concurrently invoke those methods on the same object instance
105   * without any external synchronization.
106   */
107  INTERFACE_NOT_THREADSAFE,
108
109
110
111  /**
112   * The associated method may be considered threadsafe and may be invoked
113   * concurrently by multiple threads, subject to the constraints described in
114   * the {@link ThreadSafety} documentation, and in any additional notes
115   * contained in the method-level javadoc.
116   */
117  METHOD_THREADSAFE,
118
119
120
121  /**
122   * The associated method may not be considered threadsafe and should not be
123   * invoked concurrently by multiple threads.
124   */
125  METHOD_NOT_THREADSAFE;
126
127
128
129  /**
130   * Retrieves the thread safety level with the specified name.
131   *
132   * @param  name  The name of the thread safety level to retrieve.  It must not
133   *               be {@code null}.
134   *
135   * @return  The requested thread safety level, or {@code null} if no such
136   *          level is defined.
137   */
138  @Nullable()
139  public static ThreadSafetyLevel forName(@NotNull final String name)
140  {
141    switch (StaticUtils.toLowerCase(name))
142    {
143      case "completelythreadsafe":
144      case "completely-threadsafe":
145      case "completely_threadsafe":
146        return COMPLETELY_THREADSAFE;
147      case "mostlythreadsafe":
148      case "mostly-threadsafe":
149      case "mostly_threadsafe":
150        return MOSTLY_THREADSAFE;
151      case "mostlynotthreadsafe":
152      case "mostly-not-threadsafe":
153      case "mostly_not_threadsafe":
154        return MOSTLY_NOT_THREADSAFE;
155      case "notthreadsafe":
156      case "not-threadsafe":
157      case "not_threadsafe":
158        return NOT_THREADSAFE;
159      case "interfacethreadsafe":
160      case "interface-threadsafe":
161      case "interface_threadsafe":
162        return INTERFACE_THREADSAFE;
163      case "interfacenotthreadsafe":
164      case "interface-not-threadsafe":
165      case "interface_not_threadsafe":
166        return INTERFACE_NOT_THREADSAFE;
167      case "methodthreadsafe":
168      case "method-threadsafe":
169      case "method_threadsafe":
170        return METHOD_THREADSAFE;
171      case "methodnotthreadsafe":
172      case "method-not-threadsafe":
173      case "method_not_threadsafe":
174        return METHOD_NOT_THREADSAFE;
175      default:
176        return null;
177    }
178  }
179}