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
040import java.lang.annotation.Documented;
041import java.lang.annotation.ElementType;
042import java.lang.annotation.Retention;
043import java.lang.annotation.RetentionPolicy;
044import java.lang.annotation.Target;
045
046
047
048/**
049 * This annotation type may be used to indicate the level of thread safety for a
050 * class or method.  Any class or interface which does not include the
051 * {@code ThreadSafety} annotation should be assumed to be not threadsafe unless
052 * otherwise specified in the documentation for that class or interface.
053 * <BR><BR>
054 * If the {@code ThreadSafety} annotation is applied to a method, then it will
055 * override the class-level annotation for the scope of that method.  That is,
056 * if a class is declared to be {@code ThreadSafetyLevel.MOSTLY_NOT_THREADSAFE}
057 * but a method within that class is declared to be
058 * {@code ThreadSafetyLevel.METHOD_THREADSAFE}, then that method may be invoked
059 * concurrently by multiple threads against the same instance.  If a class is
060 * declared to be {@code ThreadSafetyLevel.MOSTLY_THREADSAFE} but a method
061 * within that class is declared to be
062 * {@code ThreadSafetyLevel.METHOD_NOT_THREADSAFE}, then that method must not be
063 * invoked on an instance while any other thread is attempting to access the
064 * same instance.  Methods within a class may only be annotated with either the
065 * {@code ThreadSafetyLevel.METHOD_THREADSAFE} or
066 * {@code ThreadSafetyLevel.METHOD_NOT_THREADSAFE} level, and only if the class
067 * is annotated with one of the {@code ThreadSafetyLevel.MOSTLY_THREADSAFE},
068 * {@code ThreadSafetyLevel.MOSTLY_NOT_THREADSAFE}, or
069 * {@code ThreadSafetyLevel.INTERFACE_NOT_THREADSAFE} level.  Classes annotated
070 * with either the {@code ThreadSafetyLevel.COMPLETELY_THREADSAFE} or
071 * {@code ThreadSafetyLevel.NOT_THREADSAFE} levels must not provide alternate
072 * method-level {@code ThreadSafety} annotations.
073 * <BR><BR>
074 * Note that there are some caveats regarding thread safety and immutability of
075 * elements in the LDAP SDK that are true regardless of the stated thread safety
076 * level:
077 * <UL>
078 *   <LI>
079 *     If an array is provided as an argument to a constructor or a method, then
080 *     that array must not be referenced or altered by the caller at any time
081 *     after that point unless it is clearly noted that it is acceptable to do
082 *     so.
083 *     <BR><BR>
084 *   </LI>
085 *
086 *   <LI>
087 *     If an array is returned by a method, then the contents of that array must
088 *     not be altered unless it is clearly noted that it is acceptable to do so.
089 *     <BR><BR>
090 *   </LI>
091 *
092 *   <LI>
093 *     If a method is intended to alter the state of an argument (e.g.,
094 *     appending to a {@code StringBuilder} or {@code ByteBuffer} or
095 *     {@code ByteStringBuffer}, reading from a {@code Reader} or an
096 *     {@code InputStream}, or writing to a {@code Writer} or
097 *     {@code OutputStream}), then that object provided as an argument must not
098 *     be accessed by any other thread while that method is active unless it is
099 *     clearly noted that it is acceptable to do so.
100 *     <BR><BR>
101 *   </LI>
102 *
103 *   <LI>
104 *     Unless otherwise noted, public static methods may be assumed to be
105 *     threadsafe independent of the thread safety level for the class that
106 *     contains them.
107 *     <BR><BR>
108 *   </LI>
109 * </UL>
110 */
111@Documented()
112@Retention(RetentionPolicy.RUNTIME)
113@Target({ ElementType.TYPE, ElementType.METHOD })
114public @interface ThreadSafety
115{
116  /**
117   * The thread safety level for the associated class, interface, enum, or
118   * method.
119   *
120   * @return  The thread safety level for the associated class, interface, enum,
121   *          or method.
122   */
123  @NotNull()
124  ThreadSafetyLevel level();
125}