View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.concurrent;
18  
19  import java.util.concurrent.atomic.AtomicReference;
20  
21  import org.apache.commons.lang3.exception.ExceptionUtils;
22  import org.apache.commons.lang3.function.FailableConsumer;
23  import org.apache.commons.lang3.function.FailableSupplier;
24  
25  /**
26   * A specialized {@link ConcurrentInitializer} implementation which is similar
27   * to {@link AtomicInitializer}, but ensures that the {@link #initialize()}
28   * method is called only once.
29   *
30   * <p>
31   * As {@link AtomicInitializer} this class is based on atomic variables, so it
32   * can create an object under concurrent access without synchronization.
33   * However, it implements an additional check to guarantee that the
34   * {@link #initialize()} method which actually creates the object cannot be
35   * called multiple times.
36   * </p>
37   * <p>
38   * Because of this additional check this implementation is slightly less
39   * efficient than {@link AtomicInitializer}, but if the object creation in the
40   * {@code initialize()} method is expensive or if multiple invocations of
41   * {@code initialize()} are problematic, it is the better alternative.
42   * </p>
43   * <p>
44   * From its semantics this class has the same properties as
45   * {@link LazyInitializer}. It is a &quot;save&quot; implementation of the lazy
46   * initializer pattern. Comparing both classes in terms of efficiency is
47   * difficult because which one is faster depends on multiple factors. Because
48   * {@link AtomicSafeInitializer} does not use synchronization at all it probably
49   * outruns {@link LazyInitializer}, at least under low or moderate concurrent
50   * access. Developers should run their own benchmarks on the expected target
51   * platform to decide which implementation is suitable for their specific use
52   * case.
53   * </p>
54   *
55   * @param <T> the type of the object managed by this initializer class
56   * @since 3.0
57   */
58  public class AtomicSafeInitializer<T> extends AbstractConcurrentInitializer<T, ConcurrentException> {
59  
60      /**
61       * Builds a new instance.
62       *
63       * @param <T> The type of results supplied by this builder.
64       * @param <I> The type of the initializer managed by this builder.
65       * @since 3.14.0
66       */
67      public static class Builder<I extends AtomicSafeInitializer<T>, T> extends AbstractBuilder<I, T, Builder<I, T>, ConcurrentException> {
68  
69          /**
70           * Constructs a new instance.
71           */
72          public Builder() {
73              // empty
74          }
75  
76          @SuppressWarnings("unchecked")
77          @Override
78          public I get() {
79              return (I) new AtomicSafeInitializer(getInitializer(), getCloser());
80          }
81  
82      }
83  
84      private static final Object NO_INIT = new Object();
85  
86      /**
87       * Creates a new builder.
88       *
89       * @param <T> the type of object to build.
90       * @return a new builder.
91       * @since 3.14.0
92       */
93      public static <T> Builder<AtomicSafeInitializer<T>, T> builder() {
94          return new Builder<>();
95      }
96  
97      /** A guard which ensures that initialize() is called only once. */
98      private final AtomicReference<AtomicSafeInitializer<T>> factory = new AtomicReference<>();
99  
100     /** Holds the reference to the managed object. */
101     private final AtomicReference<T> reference = new AtomicReference<>(getNoInit());
102 
103     /**
104      * Constructs a new instance.
105      */
106     public AtomicSafeInitializer() {
107         // empty
108     }
109 
110     /**
111      * Constructs a new instance.
112      *
113      * @param initializer the initializer supplier called by {@link #initialize()}.
114      * @param closer the closer consumer called by {@link #close()}.
115      */
116     private AtomicSafeInitializer(final FailableSupplier<T, ConcurrentException> initializer, final FailableConsumer<T, ConcurrentException> closer) {
117         super(initializer, closer);
118     }
119 
120     /**
121      * Gets (and initialize, if not initialized yet) the required object.
122      *
123      * @return lazily initialized object.
124      * @throws ConcurrentException if the initialization of the object causes an exception.
125      */
126     @Override
127     public final T get() throws ConcurrentException {
128         T result;
129         while ((result = reference.get()) == getNoInit()) {
130             if (factory.compareAndSet(null, this)) {
131                 try {
132                     reference.set(initialize());
133                 } catch (final Throwable t) {
134                     // Allow retry on failure; otherwise callers spin forever.
135                     factory.set(null);
136                     // Rethrow preserving original semantics: unchecked as-is, checked wrapped.
137                     final Throwable checked = ExceptionUtils.throwUnchecked(t);
138                     throw checked instanceof ConcurrentException ? (ConcurrentException) checked : new ConcurrentException(checked);
139                 }
140             }
141         }
142         return result;
143     }
144 
145     /** Gets the internal no-init object cast for this instance. */
146     @SuppressWarnings("unchecked")
147     private T getNoInit() {
148         return (T) NO_INIT;
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     protected ConcurrentException getTypedException(final Exception e) {
156         return new ConcurrentException(e);
157     }
158 
159     /**
160      * Tests whether this instance is initialized. Once initialized, always returns true.
161      *
162      * @return whether this instance is initialized. Once initialized, always returns true.
163      * @since 3.14.0
164      */
165     @Override
166     public boolean isInitialized() {
167         return reference.get() != NO_INIT;
168     }
169 }