UpToDateChecker is a small and basic library for checking if something is up-to-date or not.
repositories {
// ...
mavenCentral()
}
dependencies {
implementation 'io.github.gonalez.uptodatechecker:uptodatechecker:0.0.2'
}<dependency>
<groupId>io.github.gonalez.uptodatechecker</groupId>
<artifactId>uptodatechecker</artifactId>
<version>0.0.2</version>
</dependency>The first step to make use of the API is to create an instance of
the UpToDateChecker, this is the entry point of the library.
UpToDateChecker upToDateChecker = UpToDateCheckerBuilder.newBuilder()
.setExecutor(MoreExecutors.directExecutor())
.setVersionMatchStrategy(String::equals)
.build();One of the best features of the API is the ability to download updates when a
request is not up-to-date, this can be done by setting an UpdateDownloader to the
UpToDateChecker, you can use the setOptionalUpdateDownloader method for this.
The library provides a default implementation of the UpdateDownloader called
FileUpdateDownloader, where the updates will be automatically downloaded to
the request path.
new FileUpdateDownloader(executor, httpClient, options);The API provides a method called addVersionProvider, this can be used to register a version
provider into the UpToDateChecker. A version provider is responsible for obtaining the
latest version of something i.e. a GitHub repository, for the request context.
upToDateChecker.addVersionProvider(new GithubVersionProvider(executor, httpClient));To check for up-to-date something, you first must create an CheckUpToDateRequest instance.
We determine if the version is up-to-date by applying the versionMatchStrategy to the
currentVersion of the request to the latest version of the VersionProvider.
There must be a version provider registered for the given request context type, otherwise the API won't know what version we're trying to compare against the request, current version.
CheckUpToDateRequest request = CheckUpToDateRequest.newBuilder()
.setContext(
GithubVersionProviderContext.newBuilder()
.setRepoName("uptodatechecker")
.setRepoOwner("gonalez")
.build())
.setCurrentVersion("0.0.2")
.build();You can also set a callback to the request to listen when we get the latest version or if there are any errors, etc.
request.setOptionalCallback(Optional.of(new UpToDateChecker.Callback() {
@Override
public void onUpToDate(CheckUpToDateResponse response) {
// called if the response is up-to-date
}
@Override
public void onNotUpToDate(CheckUpToDateResponse response) {
// called if the response is not up-to-date
}
}));Finally, you can send the request to check if the request is up-to-date.
ListenableFuture<CheckUpToDateResponse> response =
upToDateChecker.checkWithDownloadingAndScheduling()
.requesting(request)
.response();
assertTrue(response.isUpToDate());You can combine several operations to be executed when checking for up-to-date something by using the
ThenOperation. This example uses the download operation to download the update if the request is not up-to-date
(for this to work you must register an UpdateDownloader as explained in the Update Downloading section):
upToDateChecker.checkWithDownloadingAndScheduling()
.requesting(request)
.then()
.download(downloadRequest)
// ...
.response();Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0