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 javax.annotation.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<V> newReference() { 038 return new AtomicReference<V>(); 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> AtomicReference<V> newReference(@Nullable V initialValue) { 048 return new AtomicReference<V>(initialValue); 049 } 050 051 /** 052 * Creates an {@code AtomicReferenceArray} instance of given length. 053 * 054 * @param length the length of the array 055 * @return a new {@code AtomicReferenceArray} with the given length 056 */ 057 public static <E> AtomicReferenceArray<E> newReferenceArray(int length) { 058 return new AtomicReferenceArray<E>(length); 059 } 060 061 /** 062 * Creates an {@code AtomicReferenceArray} instance with the same length as, and all elements 063 * copied from, the given array. 064 * 065 * @param array the array to copy elements from 066 * @return a new {@code AtomicReferenceArray} copied from the given array 067 */ 068 public static <E> AtomicReferenceArray<E> newReferenceArray(E[] array) { 069 return new AtomicReferenceArray<E>(array); 070 } 071}