1 /* 2 * Copyright (C) 2010 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.util.concurrent; 16 17 import com.google.common.annotations.GwtIncompatible; 18 import java.util.concurrent.atomic.AtomicReference; 19 import java.util.concurrent.atomic.AtomicReferenceArray; 20 import org.checkerframework.checker.nullness.qual.Nullable; 21 22 /** 23 * Static utility methods pertaining to classes in the {@code java.util.concurrent.atomic} package. 24 * 25 * @author Kurt Alfred Kluever 26 * @since 10.0 27 */ 28 @GwtIncompatible 29 @ElementTypesAreNonnullByDefault 30 public final class Atomics { Atomics()31 private Atomics() {} 32 33 /** 34 * Creates an {@code AtomicReference} instance with no initial value. 35 * 36 * @return a new {@code AtomicReference} with no initial value 37 */ newReference()38 public static <V> AtomicReference<@Nullable V> newReference() { 39 return new AtomicReference<>(); 40 } 41 42 /** 43 * Creates an {@code AtomicReference} instance with the given initial value. 44 * 45 * @param initialValue the initial value 46 * @return a new {@code AtomicReference} with the given initial value 47 */ newReference( @arametricNullness V initialValue)48 public static <V extends @Nullable Object> AtomicReference<V> newReference( 49 @ParametricNullness V initialValue) { 50 return new AtomicReference<>(initialValue); 51 } 52 53 /** 54 * Creates an {@code AtomicReferenceArray} instance of given length. 55 * 56 * @param length the length of the array 57 * @return a new {@code AtomicReferenceArray} with the given length 58 */ newReferenceArray(int length)59 public static <E> AtomicReferenceArray<@Nullable E> newReferenceArray(int length) { 60 return new AtomicReferenceArray<>(length); 61 } 62 63 /** 64 * Creates an {@code AtomicReferenceArray} instance with the same length as, and all elements 65 * copied from, the given array. 66 * 67 * @param array the array to copy elements from 68 * @return a new {@code AtomicReferenceArray} copied from the given array 69 */ newReferenceArray(E[] array)70 public static <E extends @Nullable Object> AtomicReferenceArray<E> newReferenceArray(E[] array) { 71 return new AtomicReferenceArray<>(array); 72 } 73 } 74