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.checkerframework.checker.nullness.qual.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
029@ElementTypesAreNonnullByDefault
030public final class Atomics {
031  private Atomics() {}
032
033  /**
034   * Creates an {@code AtomicReference} instance with no initial value.
035   *
036   * @return a new {@code AtomicReference} with no initial value
037   */
038  public static <V> AtomicReference<@Nullable V> newReference() {
039    return new AtomicReference<>();
040  }
041
042  /**
043   * Creates an {@code AtomicReference} instance with the given initial value.
044   *
045   * @param initialValue the initial value
046   * @return a new {@code AtomicReference} with the given initial value
047   */
048  public static <V extends @Nullable Object> AtomicReference<V> newReference(
049      @ParametricNullness V initialValue) {
050    return new AtomicReference<>(initialValue);
051  }
052
053  /**
054   * Creates an {@code AtomicReferenceArray} instance of given length.
055   *
056   * @param length the length of the array
057   * @return a new {@code AtomicReferenceArray} with the given length
058   */
059  public static <E> AtomicReferenceArray<@Nullable E> newReferenceArray(int length) {
060    return new AtomicReferenceArray<>(length);
061  }
062
063  /**
064   * Creates an {@code AtomicReferenceArray} instance with the same length as, and all elements
065   * copied from, the given array.
066   *
067   * @param array the array to copy elements from
068   * @return a new {@code AtomicReferenceArray} copied from the given array
069   */
070  public static <E extends @Nullable Object> AtomicReferenceArray<E> newReferenceArray(E[] array) {
071    return new AtomicReferenceArray<>(array);
072  }
073}