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