    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.concurrent;
    018
    019import java.util.concurrent.atomic.AtomicReference;
    020
    021/**
    022 * A specialized {@link ConcurrentInitializer} implementation which is similar
    023 * to {@link AtomicInitializer}, but ensures that the {@link #initialize()}
    024 * method is called only once.
    025 *
    026 * <p>
    027 * As {@link AtomicInitializer} this class is based on atomic variables, so it
    028 * can create an object under concurrent access without synchronization.
    029 * However, it implements an additional check to guarantee that the
    030 * {@link #initialize()} method which actually creates the object cannot be
    031 * called multiple times.
    032 * </p>
    033 * <p>
    034 * Because of this additional check this implementation is slightly less
    035 * efficient than {@link AtomicInitializer}, but if the object creation in the
    036 * {@code initialize()} method is expensive or if multiple invocations of
    037 * {@code initialize()} are problematic, it is the better alternative.
    038 * </p>
    039 * <p>
    040 * From its semantics this class has the same properties as
    041 * {@link LazyInitializer}. It is a &quot;save&quot; implementation of the lazy
    042 * initializer pattern. Comparing both classes in terms of efficiency is
    043 * difficult because which one is faster depends on multiple factors. Because
    044 * {@link AtomicSafeInitializer} does not use synchronization at all it probably
    045 * outruns {@link LazyInitializer}, at least under low or moderate concurrent
    046 * access. Developers should run their own benchmarks on the expected target
    047 * platform to decide which implementation is suitable for their specific use
    048 * case.
    049 * </p>
    050 *
    051 * @since 3.0
    052 * @param <T> the type of the object managed by this initializer class
    053 */
    054public abstract class AtomicSafeInitializer<T> implements
    055        ConcurrentInitializer<T> {
    056    /** A guard which ensures that initialize() is called only once. */
    057    private final AtomicReference<AtomicSafeInitializer<T>> factory =
    058            new AtomicReference<>();
    059
    060    /** Holds the reference to the managed object. */
    061    private final AtomicReference<T> reference = new AtomicReference<>();
    062
    063    /**
    064     * Gets (and initialize, if not initialized yet) the required object
    065     *
    066     * @return lazily initialized object
    067     * @throws ConcurrentException if the initialization of the object causes an
    068     * exception
    069     */
    070    @Override
    071    public final T get() throws ConcurrentException {
    072        T result;
    073
    074        while ((result = reference.get()) == null) {
    075            if (factory.compareAndSet(null, this)) {
    076                reference.set(initialize());
    077            }
    078        }
    079
    080        return result;
    081    }
    082
    083    /**
    084     * Creates and initializes the object managed by this
    085     * {@link AtomicInitializer}. This method is called by {@link #get()} when
    086     * the managed object is not available yet. An implementation can focus on
    087     * the creation of the object. No synchronization is needed, as this is
    088     * already handled by {@code get()}. This method is guaranteed to be called
    089     * only once.
    090     *
    091     * @return the managed data object
    092     * @throws ConcurrentException if an error occurs during object creation
    093     */
    094    protected abstract T initialize() throws ConcurrentException;
    095}