org.gradle.api.internaltransform.CacheableInvocation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.artifacts.transform;
import org.gradle.internal.Try;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* An invocation which can come from the cache.
*
* Invoking can be expensive when the result is not from the cache.
*
* @param The type which will be computed.
*/
public interface CacheableInvocation {
/**
* The result of the invocation when it is already in the cache.
*/
Optional> getCachedResult();
/**
* Obtain the result of the invocation, either by returning the cached result or by computing it.
*/
Try invoke();
/**
* Maps the result of the invocation via a mapper.
*
* @param mapper An inexpensive function on the result.
*/
default CacheableInvocation map(Function super T, U> mapper) {
return new CacheableInvocation() {
@Override
public Optional> getCachedResult() {
return CacheableInvocation.this.getCachedResult().map(result -> result.map(mapper));
}
@Override
public Try invoke() {
return CacheableInvocation.this.invoke().map(mapper);
}
};
}
/**
* Chains two {@link CacheableInvocation}s.
*
* @param mapper A function which creates the next {@link CacheableInvocation} from the result of the first one.
* Creating the invocation may be expensive, so this method avoids calling the mapper twice if possible.
*/
default CacheableInvocation flatMap(Function super T, CacheableInvocation> mapper) {
return getCachedResult()
.map(cachedResult -> cachedResult
.tryMap(mapper)
.getOrMapFailure(failure -> cached(Try.failure(failure)))
).orElseGet(() ->
nonCached(() ->
invoke().flatMap(intermediateResult -> mapper.apply(intermediateResult).invoke())
)
);
}
/**
* An invocation returning a fixed result from the cache.
*/
static CacheableInvocation cached(Try result) {
return new CacheableInvocation() {
@Override
public Optional> getCachedResult() {
return Optional.of(result);
}
@Override
public Try invoke() {
return result;
}
};
}
/**
* An invocation with no cached result, requiring to do the expensive computation on {@link #invoke}.
*/
static CacheableInvocation nonCached(Supplier> result) {
return new CacheableInvocation() {
@Override
public Optional> getCachedResult() {
return Optional.empty();
}
@Override
public Try invoke() {
return result.get();
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy