
org.nuiton.topia.service.migration.resources.MigrationVersionResourceProvider Maven / Gradle / Ivy
Show all versions of topia-extension-migration Show documentation
package org.nuiton.topia.service.migration.resources;
/*-
* #%L
* ObServe Toolkit :: ToPIA Migration service
* %%
* Copyright (C) 2017 - 2018 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.topia.persistence.TopiaMigrationServiceException;
import org.nuiton.version.Version;
import org.nuiton.version.VersionComparator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
/**
* Provider of {@link MigrationVersionResource}.
*
* Created by tchemit on 05/05/2018.
*
* @author Tony Chemit - [email protected]
*/
@SuppressWarnings("WeakerAccess")
public class MigrationVersionResourceProvider {
private static final Log log = LogFactory.getLog(MigrationVersionResourceProvider.class);
private static MigrationVersionResourceProvider INSTANCE;
/**
* Available migration resources found in class-path indexed by their version.
*/
protected final Map resources;
protected MigrationVersionResourceProvider() {
ImmutableMap.Builder resourcesBuilder = ImmutableMap.builder();
for (MigrationVersionResource resource : ServiceLoader.load(MigrationVersionResource.class)) {
resourcesBuilder.put(resource.getVersion(), resource);
}
resources = resourcesBuilder.build();
log.info(String.format("Found %d migration resource(s).", resources.size()));
}
public static synchronized MigrationVersionResourceProvider get() {
return INSTANCE == null ? INSTANCE = new MigrationVersionResourceProvider() : INSTANCE;
}
public List getAvailableVersions() {
return resources.keySet().stream().sorted(new VersionComparator()).collect(Collectors.toList());
}
public Version getLastVersion() {
List values = getAvailableVersions();
return values.isEmpty() ? null : values.get(values.size() - 1);
}
public ImmutableList getVersionsAfter(Version current) {
ImmutableList.Builder builder = ImmutableList.builder();
for (Version version : getAvailableVersions()) {
if (version.after(current)) {
builder.add(version);
}
}
return builder.build();
}
public MigrationVersionResource getResource(Version version) {
MigrationVersionResource result = resources.get(version);
if (result == null) {
throw new TopiaMigrationServiceException(String.format("Can't find resource for version: %s.", version));
}
return result;
}
}