class PluginInitializer {

    @VisibleForTesting
    static void verifySamePluginVersion(
            @NonNull ConcurrentMap<Object, String> projectToPluginVersionMap,
            @NonNull Project project,
            @NonNull String pluginVersion) {
        Preconditions.checkState(
                !projectToPluginVersionMap.containsKey(project),
                String.format(
                        "Android Gradle plugin %1$s must not be applied to project '%2$s'"
                                + " since version %3$s was already applied to this project",
                        pluginVersion,
                        project.getProjectDir().getAbsolutePath(),
                        projectToPluginVersionMap.get(project)));

        projectToPluginVersionMap.put(project, pluginVersion);

        if (projectToPluginVersionMap.values().stream().distinct().count() > 1) {
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append(
                    "Using multiple versions of the Android Gradle plugin in the same build"
                            + " is not allowed.");
            for (Map.Entry<Object, String> entry : projectToPluginVersionMap.entrySet()) {
                Preconditions.checkState(
                        entry.getKey() instanceof Project,
                        Project.class + " should be loaded only once");
                Project fromProject = (Project) entry.getKey();
                String toPluginVersion = entry.getValue();
                errorMessage.append(
                        String.format(
                                "\n\t'%1$s' is using version %2$s",
                                fromProject.getProjectDir().getAbsolutePath(), toPluginVersion));
            }
            throw new IllegalStateException(errorMessage.toString());
        }
    }

}