001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.GwtIncompatible;
018import com.google.errorprone.annotations.InlineMe;
019import java.util.concurrent.atomic.AtomicReference;
020import java.util.concurrent.atomic.AtomicReferenceArray;
021import org.jspecify.annotations.Nullable;
022
023/**
024 * Static utility methods pertaining to classes in the {@code java.util.concurrent.atomic} package.
025 *
026 * @author Kurt Alfred Kluever
027 * @since 10.0
028 */
029@GwtIncompatible
030public final class Atomics {
031  private Atomics() {}
032
033  /**
034   * Creates an {@code AtomicReference} instance with no initial value.
035   *
036   * <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
037   * use the {@code AtomicReference} {@linkplain AtomicReference#AtomicReference() constructor}
038   * directly, taking advantage of <a
039   * href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
040   * syntax</a>.
041   *
042   * @return a new {@code AtomicReference} with no initial value
043   */
044  @InlineMe(
045      replacement = "new AtomicReference<>()",
046      imports = "java.util.concurrent.atomic.AtomicReference")
047  public static <V> AtomicReference<@Nullable V> newReference() {
048    return new AtomicReference<>();
049  }
050
051  /**
052   * Creates an {@code AtomicReference} instance with the given initial value.
053   *
054   * <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
055   * use the {@code AtomicReference} {@linkplain AtomicReference#AtomicReference(Object)
056   * constructor} directly, taking advantage of <a
057   * href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
058   * syntax</a>.
059   *
060   * @param initialValue the initial value
061   * @return a new {@code AtomicReference} with the given initial value
062   */
063  @InlineMe(
064      replacement = "new AtomicReference<>(initialValue)",
065      imports = "java.util.concurrent.atomic.AtomicReference")
066  public static <V extends @Nullable Object> AtomicReference<V> newReference(
067      @ParametricNullness V initialValue) {
068    return new AtomicReference<>(initialValue);
069  }
070
071  /**
072   * Creates an {@code AtomicReferenceArray} instance of given length.
073   *
074   * <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
075   * use the {@code AtomicReferenceArray} {@linkplain AtomicReferenceArray#AtomicReferenceArray(int)
076   * constructor} directly, taking advantage of <a
077   * href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
078   * syntax</a>.
079   *
080   * @param length the length of the array
081   * @return a new {@code AtomicReferenceArray} with the given length
082   */
083  @InlineMe(
084      replacement = "new AtomicReferenceArray<>(length)",
085      imports = "java.util.concurrent.atomic.AtomicReferenceArray")
086  public static <E> AtomicReferenceArray<@Nullable E> newReferenceArray(int length) {
087    return new AtomicReferenceArray<>(length);
088  }
089
090  /**
091   * Creates an {@code AtomicReferenceArray} instance with the same length as, and all elements
092   * copied from, the given array.
093   *
094   * <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
095   * use the {@code AtomicReferenceArray} {@linkplain
096   * AtomicReferenceArray#AtomicReferenceArray(Object[]) constructor} directly, taking advantage of
097   * <a
098   * href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
099   * syntax</a>.
100   *
101   * @param array the array to copy elements from
102   * @return a new {@code AtomicReferenceArray} copied from the given array
103   */
104  @InlineMe(
105      replacement = "new AtomicReferenceArray<>(array)",
106      imports = "java.util.concurrent.atomic.AtomicReferenceArray")
107  public static <E extends @Nullable Object> AtomicReferenceArray<E> newReferenceArray(E[] array) {
108    return new AtomicReferenceArray<>(array);
109  }
110}