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 java.util.concurrent.atomic.AtomicReference;
019import java.util.concurrent.atomic.AtomicReferenceArray;
020import org.jspecify.annotations.Nullable;
021
022/**
023 * Static utility methods pertaining to classes in the {@code java.util.concurrent.atomic} package.
024 *
025 * @author Kurt Alfred Kluever
026 * @since 10.0
027 */
028@GwtIncompatible
029public final class Atomics {
030  private Atomics() {}
031
032  /**
033   * Creates an {@code AtomicReference} instance with no initial value.
034   *
035   * @return a new {@code AtomicReference} with no initial value
036   */
037  public static <V> AtomicReference<@Nullable V> newReference() {
038    return new AtomicReference<>();
039  }
040
041  /**
042   * Creates an {@code AtomicReference} instance with the given initial value.
043   *
044   * @param initialValue the initial value
045   * @return a new {@code AtomicReference} with the given initial value
046   */
047  public static <V extends @Nullable Object> AtomicReference<V> newReference(
048      @ParametricNullness V initialValue) {
049    return new AtomicReference<>(initialValue);
050  }
051
052  /**
053   * Creates an {@code AtomicReferenceArray} instance of given length.
054   *
055   * @param length the length of the array
056   * @return a new {@code AtomicReferenceArray} with the given length
057   */
058  public static <E> AtomicReferenceArray<@Nullable E> newReferenceArray(int length) {
059    return new AtomicReferenceArray<>(length);
060  }
061
062  /**
063   * Creates an {@code AtomicReferenceArray} instance with the same length as, and all elements
064   * copied from, the given array.
065   *
066   * @param array the array to copy elements from
067   * @return a new {@code AtomicReferenceArray} copied from the given array
068   */
069  public static <E extends @Nullable Object> AtomicReferenceArray<E> newReferenceArray(E[] array) {
070    return new AtomicReferenceArray<>(array);
071  }
072}