001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.util.concurrent;
018
019import java.util.concurrent.atomic.AtomicReference;
020import java.util.concurrent.atomic.AtomicReferenceArray;
021
022import javax.annotation.Nullable;
023
024/**
025 * Static utility methods pertaining to classes in the
026 * {@code java.util.concurrent.atomic} package.
027 *
028 * @author Kurt Alfred Kluever
029 * @since 10.0
030 */
031public final class Atomics {
032  private Atomics() {}
033
034  /**
035   * Creates an {@code AtomicReference} instance with no initial value.
036   *
037   * @return a new {@code AtomicReference} with no initial value
038   */
039  public static <V> AtomicReference<V> newReference() {
040    return new AtomicReference<V>();
041  }
042
043  /**
044   * Creates an {@code AtomicReference} instance with the given initial value.
045   *
046   * @param initialValue the initial value
047   * @return a new {@code AtomicReference} with the given initial value
048   */
049  public static <V> AtomicReference<V> newReference(@Nullable V initialValue) {
050    return new AtomicReference<V>(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<E> newReferenceArray(int length) {
060    return new AtomicReferenceArray<E>(length);
061  }
062
063  /**
064   * Creates an {@code AtomicReferenceArray} instance with the same length as,
065   * and all elements 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> AtomicReferenceArray<E> newReferenceArray(E[] array) {
071    return new AtomicReferenceArray<E>(array);
072  }
073}