View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.impl.cache;
20  
21  import java.util.function.Function;
22  
23  /**
24   * A caching supplier wrapper that caches results and exceptions from the underlying supplier.
25   * Used internally to cache expensive computations in the session.
26   *
27   * @param <REQ> The request type
28   * @param <REP> The response type
29   */
30  public class CachingSupplier<REQ, REP> implements Function<REQ, REP> {
31      protected final Function<REQ, REP> supplier;
32      protected volatile Object value;
33  
34      public CachingSupplier(Function<REQ, REP> supplier) {
35          this.supplier = supplier;
36      }
37  
38      public Object getValue() {
39          return value;
40      }
41  
42      @Override
43      @SuppressWarnings({"unchecked", "checkstyle:InnerAssignment"})
44      public REP apply(REQ req) {
45          Object v;
46          if ((v = value) == null) {
47              synchronized (this) {
48                  if ((v = value) == null) {
49                      try {
50                          v = value = supplier.apply(req);
51                      } catch (Exception e) {
52                          v = value = new AltRes(e);
53                      }
54                  }
55              }
56          }
57          if (v instanceof AltRes altRes) {
58              DefaultRequestCache.uncheckedThrow(altRes.throwable);
59          }
60          return (REP) v;
61      }
62  
63      /**
64       * Special holder class for exceptions that occur during supplier execution.
65       * Allows caching and re-throwing of exceptions on subsequent calls.
66       */
67      public static class AltRes {
68          protected final Throwable throwable;
69  
70          /**
71           * Creates a new AltRes with the given throwable.
72           *
73           * @param throwable The throwable to store
74           */
75          public AltRes(Throwable throwable) {
76              this.throwable = throwable;
77          }
78  
79          public Throwable getThrowable() {
80              return throwable;
81          }
82      }
83  }