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 * http://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.CancellationException;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Future;
24 import java.util.function.Function;
25
26 import org.apache.commons.lang3.exception.ExceptionUtils;
27
28 /**
29 * Definition of an interface for a wrapper around a calculation that takes a single parameter and returns a result. The
30 * results for the calculation will be cached for future requests.
31 *
32 * <p>
33 * This is not a fully functional cache, there is no way of limiting or removing results once they have been generated.
34 * However, it is possible to get the implementation to regenerate the result for a given parameter, if an error was
35 * thrown during the previous calculation, by setting the option during the construction of the class. If this is not
36 * set the class will return the cached exception.
37 * </p>
38 * <p>
39 * Thanks go to Brian Goetz, Tim Peierls and the members of JCP JSR-166 Expert Group for coming up with the
40 * original implementation of the class. It was also published within Java Concurrency in Practice as a sample.
41 * </p>
42 *
43 * @param <I> the type of the input to the calculation
44 * @param <O> the type of the output of the calculation
45 *
46 * @since 3.6
47 */
48 public class Memoizer<I, O> implements Computable<I, O> {
49
50 private final ConcurrentMap<I, Future<O>> cache = new ConcurrentHashMap<>();
51 private final Function<? super I, ? extends Future<O>> mappingFunction;
52 private final boolean recalculate;
53
54 /**
55 * Constructs a Memoizer for the provided Computable calculation.
56 *
57 * <p>
58 * If a calculation throws an exception for any reason, this exception will be cached and returned for all future
59 * calls with the provided parameter.
60 * </p>
61 *
62 * @param computable the computation whose results should be memorized
63 */
64 public Memoizer(final Computable<I, O> computable) {
65 this(computable, false);
66 }
67
68 /**
69 * Constructs a Memoizer for the provided Computable calculation, with the option of whether a Computation that
70 * experiences an error should recalculate on subsequent calls or return the same cached exception.
71 *
72 * @param computable the computation whose results should be memorized
73 * @param recalculate determines whether the computation should be recalculated on subsequent calls if the previous call
74 * failed
75 */
76 public Memoizer(final Computable<I, O> computable, final boolean recalculate) {
77 this.recalculate = recalculate;
78 this.mappingFunction = k -> FutureTasks.run(() -> computable.compute(k));
79 }
80
81 /**
82 * Constructs a Memoizer for the provided Function calculation.
83 *
84 * <p>
85 * If a calculation throws an exception for any reason, this exception will be cached and returned for all future
86 * calls with the provided parameter.
87 * </p>
88 *
89 * @param function the function whose results should be memorized
90 * @since 2.13.0
91 */
92 public Memoizer(final Function<I, O> function) {
93 this(function, false);
94 }
95
96 /**
97 * Constructs a Memoizer for the provided Function calculation, with the option of whether a Function that
98 * experiences an error should recalculate on subsequent calls or return the same cached exception.
99 *
100 * @param function the computation whose results should be memorized
101 * @param recalculate determines whether the computation should be recalculated on subsequent calls if the previous call
102 * failed
103 * @since 2.13.0
104 */
105 public Memoizer(final Function<I, O> function, final boolean recalculate) {
106 this.recalculate = recalculate;
107 this.mappingFunction = k -> FutureTasks.run(() -> function.apply(k));
108 }
109
110 /**
111 * This method will return the result of the calculation and cache it, if it has not previously been calculated.
112 *
113 * <p>
114 * This cache will also cache exceptions that occur during the computation if the {@code recalculate} parameter in the
115 * constructor was set to {@code false}, or not set. Otherwise, if an exception happened on the previous calculation,
116 * the method will attempt again to generate a value.
117 * </p>
118 *
119 * @param arg the argument for the calculation
120 * @return the result of the calculation
121 * @throws InterruptedException thrown if the calculation is interrupted
122 */
123 @Override
124 public O compute(final I arg) throws InterruptedException {
125 while (true) {
126 final Future<O> future = cache.computeIfAbsent(arg, mappingFunction);
127 try {
128 return future.get();
129 } catch (final CancellationException e) {
130 cache.remove(arg, future);
131 } catch (final ExecutionException e) {
132 if (recalculate) {
133 cache.remove(arg, future);
134 }
135 throw launderException(e.getCause());
136 }
137 }
138 }
139
140 /**
141 * This method launders a Throwable to either a RuntimeException, Error or any other Exception wrapped in an
142 * IllegalStateException.
143 *
144 * @param throwable the throwable to laundered
145 * @return a RuntimeException, Error or an IllegalStateException
146 */
147 private RuntimeException launderException(final Throwable throwable) {
148 throw new IllegalStateException("Unchecked exception", ExceptionUtils.throwUnchecked(throwable));
149 }
150 }