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.CancellationException; 020import java.util.concurrent.ConcurrentHashMap; 021import java.util.concurrent.ConcurrentMap; 022import java.util.concurrent.ExecutionException; 023import java.util.concurrent.Future; 024import java.util.function.Function; 025 026import org.apache.commons.lang3.exception.ExceptionUtils; 027 028/** 029 * Definition of an interface for a wrapper around a calculation that takes a single parameter and returns a result. The 030 * results for the calculation will be cached for future requests. 031 * 032 * <p> 033 * This is not a fully functional cache, there is no way of limiting or removing results once they have been generated. 034 * However, it is possible to get the implementation to regenerate the result for a given parameter, if an error was 035 * thrown during the previous calculation, by setting the option during the construction of the class. If this is not 036 * set the class will return the cached exception. 037 * </p> 038 * <p> 039 * Thanks go to Brian Goetz, Tim Peierls and the members of JCP JSR-166 Expert Group for coming up with the 040 * original implementation of the class. It was also published within Java Concurrency in Practice as a sample. 041 * </p> 042 * 043 * @param <I> the type of the input to the calculation 044 * @param <O> the type of the output of the calculation 045 * 046 * @since 3.6 047 */ 048public class Memoizer<I, O> implements Computable<I, O> { 049 050 private final ConcurrentMap<I, Future<O>> cache = new ConcurrentHashMap<>(); 051 private final Function<? super I, ? extends Future<O>> mappingFunction; 052 private final boolean recalculate; 053 054 /** 055 * Constructs a Memoizer for the provided Computable calculation. 056 * 057 * <p> 058 * If a calculation throws an exception for any reason, this exception will be cached and returned for all future 059 * calls with the provided parameter. 060 * </p> 061 * 062 * @param computable the computation whose results should be memorized 063 */ 064 public Memoizer(final Computable<I, O> computable) { 065 this(computable, false); 066 } 067 068 /** 069 * Constructs a Memoizer for the provided Computable calculation, with the option of whether a Computation that 070 * experiences an error should recalculate on subsequent calls or return the same cached exception. 071 * 072 * @param computable the computation whose results should be memorized 073 * @param recalculate determines whether the computation should be recalculated on subsequent calls if the previous call 074 * failed 075 */ 076 public Memoizer(final Computable<I, O> computable, final boolean recalculate) { 077 this.recalculate = recalculate; 078 this.mappingFunction = k -> FutureTasks.run(() -> computable.compute(k)); 079 } 080 081 /** 082 * Constructs a Memoizer for the provided Function calculation. 083 * 084 * <p> 085 * If a calculation throws an exception for any reason, this exception will be cached and returned for all future 086 * calls with the provided parameter. 087 * </p> 088 * 089 * @param function the function whose results should be memorized 090 * @since 2.13.0 091 */ 092 public Memoizer(final Function<I, O> function) { 093 this(function, false); 094 } 095 096 /** 097 * Constructs a Memoizer for the provided Function calculation, with the option of whether a Function that 098 * experiences an error should recalculate on subsequent calls or return the same cached exception. 099 * 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}