View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.utils;
21  
22  import java.lang.ref.WeakReference;
23  
24  /**
25   * A wrapper class for {@link WeakReference} that provides a convenient way
26   * to manage weak references to objects.
27   *
28   * <p>
29   * This class encapsulates the creation and retrieval of weak references,
30   * simplifying the common pattern of storing and accessing weakly referenced objects.
31   * </p>
32   *
33   * @param <T> the type of the referenced object
34   */
35  public final class WeakReferenceHolder<T> {
36  
37      /** The weak reference to the object. */
38      private WeakReference<T> reference;
39  
40      /**
41       * Constructs a new {@code WeakReferenceHolder} with no initial reference.
42       */
43      public WeakReferenceHolder() {
44          reference = new WeakReference<>(null);
45      }
46  
47      /**
48       * Returns the object held by this weak reference, or {@code null} if
49       * the object has been garbage collected.
50       *
51       * @return the referenced object, or {@code null} if it has been collected
52       */
53      public T get() {
54          return reference.get();
55      }
56  
57      /**
58       * Updates the referenced object only if the new object is different
59       * from the currently referenced object. After updating, runs the
60       * specified callback if provided.
61       *
62       * @param newObject the new object to reference;
63       * @param afterUpdate a callback to run after updating the reference;
64       */
65      public void lazyUpdate(T newObject, Runnable afterUpdate) {
66          final T previous = reference.get();
67          if (previous != newObject) {
68              reference = new WeakReference<>(newObject);
69              afterUpdate.run();
70          }
71      }
72  
73  }