001package jmri.configurexml;
002
003import java.util.HashMap;
004import java.util.ServiceLoader;
005import javax.annotation.Nonnull;
006import jmri.InstanceManagerAutoDefault;
007
008/**
009 * Get class migrations for the {@link ConfigXmlManager}.
010 *
011 * @author Randall Wood Copyright 2017
012 */
013public class ClassMigrationManager implements InstanceManagerAutoDefault {
014
015    private final HashMap<String, String> migrations = new HashMap<>();
016
017    public ClassMigrationManager() {
018        ServiceLoader<ClassMigration> loader = ServiceLoader.load(ClassMigration.class);
019        loader.forEach((serviceProvider) -> {
020            this.migrations.putAll(serviceProvider.getMigrations());
021        });
022        loader.reload(); // mark all providers as ready to be garbage collected
023    }
024
025    /**
026     * Get the class name to be used for the given class name.
027     *
028     * @param className the class name to check for a migration against
029     * @return the class name to use; either the new name to migrate to or the
030     *         old name if no migration is required
031     */
032    @Nonnull
033    public String getClassName(@Nonnull String className) {
034        String migration = this.migrations.get(className);
035        return migration != null ? migration : className;
036    }
037
038}