/*
 * Copyright 2016 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.plugin.use.internal;

import org.gradle.api.Action;
import org.gradle.api.Namer;
import org.gradle.api.internal.DefaultNamedDomainObjectList;
import org.gradle.api.internal.artifacts.DependencyResolutionServices;
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionSelectorScheme;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.plugins.dsl.PluginRepositoryHandler;
import org.gradle.api.internal.plugins.repositories.MavenPluginRepository;
import org.gradle.api.internal.plugins.repositories.PluginRepository;
import org.gradle.internal.Factory;
import org.gradle.internal.reflect.Instantiator;

class DefaultPluginRepositoryHandler extends DefaultNamedDomainObjectList<PluginRepository> implements PluginRepositoryHandler {

    private FileResolver fileResolver;
    private Factory<DependencyResolutionServices> dependencyResolutionServicesFactory;
    private VersionSelectorScheme versionSelectorScheme;

    public DefaultPluginRepositoryHandler(FileResolver fileResolver, Factory<DependencyResolutionServices> dependencyResolutionServicesFactory, VersionSelectorScheme versionSelectorScheme, Instantiator instantiator) {
        super(PluginRepository.class, instantiator, new RepositoryNamer());
        this.fileResolver = fileResolver;
        this.dependencyResolutionServicesFactory = dependencyResolutionServicesFactory;
        this.versionSelectorScheme = versionSelectorScheme;
    }

    private static class RepositoryNamer implements Namer<PluginRepository> {
        @Override
        public String determineName(PluginRepository repository) {
            return repository.getName();
        }
    }

    @Override
    public MavenPluginRepository maven(Action<? super MavenPluginRepository> configurationAction) {
        DefaultMavenPluginRepository mavenPluginRepository = getInstantiator()
            .newInstance(DefaultMavenPluginRepository.class, fileResolver, dependencyResolutionServicesFactory.create(), versionSelectorScheme);
        configurationAction.execute(mavenPluginRepository);
        uniquifyName(mavenPluginRepository);
        add(mavenPluginRepository);
        return mavenPluginRepository;
    }

    private void uniquifyName(MavenPluginRepository mavenPluginRepository) {
        String name = mavenPluginRepository.getName();
        if (name == null) {
            name = "maven";
        }
        name = uniquifyName(name);
        mavenPluginRepository.setName(name);
    }

    private String uniquifyName(String proposedName) {
        if (findByName(proposedName) == null) {
            return proposedName;
        }
        for (int index = 2; true; index++) {
            String candidate = String.format("%s%d", proposedName, index);
            if (findByName(candidate) == null) {
                return candidate;
            }
        }
    }

}

@@ -662,119 +662,73 @@
 ort 
-org.gradle.api.Action;%0Aimport org.gradle.api.Namer;%0Aimport org.gradle.api.internal.DefaultNamedDomainObjectList
+com.google.common.collect.Iterators;%0Aimport org.gradle.api.Action
 ;%0Aim
@@ -1244,82 +1244,148 @@
 r;%0A%0A
-class DefaultPluginRepositoryHandler extends DefaultNamedDomainObjectList%3C
+import java.util.ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0Aclass Default
 Plug
@@ -1396,17 +1396,23 @@
 pository
-%3E
+Handler
  impleme
@@ -1624,16 +1624,139 @@
 rScheme;
+%0A    private Instantiator instantiator;%0A    private Set%3CString%3E repoNames;%0A    private List%3CPluginRepository%3E repositories;
 %0A%0A    pu
@@ -1979,74 +1979,40 @@
     
-super(PluginRepository.class, instantiator, new RepositoryNamer())
+this.instantiator = instantiator
 ;%0A  
@@ -2207,224 +2207,114 @@
     
-%7D%0A%0A
     
-private static class RepositoryNamer implements Namer%3CPluginRepository%3E %7B%0A        @Override%0A        public String determineName(PluginRepository repository) %7B%0A            return repository.getName();%0A        %7D
+this.repoNames = new HashSet%3CString%3E();%0A        this.repositories = new ArrayList%3CPluginRepository%3E();
 %0A   
@@ -2496,12 +2496,9 @@
 y = 
-getI
+i
 nsta
@@ -2508,23 +2508,8 @@
 ator
-()%0A            
 .new
@@ -2517,16 +2517,29 @@
 nstance(
+%0A            
 DefaultM
@@ -2714,53 +2714,8 @@
 y);%0A
-        uniquifyName(mavenPluginRepository);%0A
     
@@ -2799,20 +2799,213 @@
     
-private void
+@Override%0A    public Iterator%3CPluginRepository%3E iterator() %7B%0A        return Iterators.unmodifiableIterator(repositories.iterator());%0A    %7D%0A%0A    private void add(PluginRepository pluginRepository) %7B%0A       
  uni
@@ -3014,22 +3014,17 @@
 ifyName(
-MavenP
+p
 luginRep
@@ -3022,39 +3022,134 @@
 pluginRepository
- mavenP
+);%0A        repositories.add(pluginRepository);%0A    %7D%0A%0A    private void uniquifyName(PluginRepository p
 luginRepository)
@@ -3173,22 +3173,17 @@
  name = 
-mavenP
+p
 luginRep
@@ -3310,22 +3310,17 @@
         
-mavenP
+p
 luginRep
@@ -3417,162 +3417,121 @@
    i
-f (findByName(proposedName) == null) %7B%0A            return proposedName;%0A        %7D%0A        for (int index = 2; true; index++) %7B%0A            String candidat
+nt attempt = 1;%0A        while (repoNames.contains(proposedName)) %7B%0A            attempt++;%0A            proposedNam
 e = 
@@ -3570,13 +3570,15 @@
 me, 
-index
+attempt
 );%0A 
@@ -3588,61 +3588,55 @@
     
+%7D%0A
     
-if (findByName(candidate) == null) %7B%0A    
+    repoNames.add(proposedName);%0A
         
     
@@ -3635,59 +3635,33 @@
     
-    
 return 
-candidate;%0A            %7D%0A        %7D%0A    %7D%0A
+proposedName;%0A    %7D
 %0A%7D%0A

Remove the non-interface public methods.