package org.jenkinsci.plugins.ghprb.extensions;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.functors.InstanceofPredicate;

import jenkins.model.Jenkins;
import hudson.DescriptorExtensionList;
import hudson.model.Descriptor;

@SuppressWarnings("unchecked")
public abstract class GhprbExtensionDescriptor extends Descriptor<GhprbExtension> {

    public boolean isApplicable(Class<?> type) {
        return true;
    }

    public static DescriptorExtensionList<GhprbExtension, GhprbExtensionDescriptor> getExtensions(Class<? extends GhprbExtensionType>... types) {
        DescriptorExtensionList<GhprbExtension, GhprbExtensionDescriptor> list = Jenkins.getInstance().getDescriptorList(GhprbExtension.class);
        filterExtensions(list, types);
        return list;
    }

    private static void filterExtensions(DescriptorExtensionList<GhprbExtension, GhprbExtensionDescriptor> descriptors, Class<? extends GhprbExtensionType>... types) {
        List<Predicate> predicates = new ArrayList<Predicate>(types.length);
        for (Class<? extends GhprbExtensionType> type : types) {
            predicates.add(InstanceofPredicate.getInstance(type));

        }
        Predicate anyPredicate = PredicateUtils.anyPredicate(predicates);
        for (GhprbExtensionDescriptor descriptor : descriptors) {
            if (!anyPredicate.evaluate(descriptor)) {
                descriptors.remove(descriptor);
            }
        }
    }

    public static DescriptorExtensionList<GhprbExtension, GhprbExtensionDescriptor> allProject() {
        DescriptorExtensionList<GhprbExtension, GhprbExtensionDescriptor> list = Jenkins.getInstance().getDescriptorList(GhprbExtension.class);
        filterExtensions(list, GhprbProjectExtension.class);
        return list;
    }

    public static DescriptorExtensionList<GhprbExtension, GhprbExtensionDescriptor> allGlobal() {
        DescriptorExtensionList<GhprbExtension, GhprbExtensionDescriptor> list = Jenkins.getInstance().getDescriptorList(GhprbExtension.class);
        filterExtensions(list, GhprbGlobalExtension.class);
        return list;
    }

}
@@ -66,24 +66,51 @@
 .ArrayList;%0A
+import java.util.Iterator;%0A
 import java.
@@ -610,48 +610,13 @@
 tic 
-DescriptorExtensionList%3CGhprbExtension, 
+List%3C
 Ghpr
@@ -711,48 +711,13 @@
     
-DescriptorExtension
 List%3C
-GhprbExtension, 
 Ghpr
@@ -749,53 +749,11 @@
 t = 
-Jenkins.getInstance().getDescriptorList(Ghprb
+get
 Exte
@@ -749,38 +749,34 @@
 t = getExtension
-.clas
 s
+(
 );%0A        filte
@@ -874,48 +874,13 @@
 ons(
-DescriptorExtensionList%3CGhprbExtension, 
+List%3C
 Ghpr
@@ -1264,21 +1264,25 @@
         
-for (
+Iterator%3C
 GhprbExt
@@ -1297,16 +1297,24 @@
 scriptor
+%3E iter =
  descrip
@@ -1320,25 +1320,117 @@
 ptor
- : descriptors) %7B
+s.iterator();%0A        while (iter.hasNext()) %7B%0A            GhprbExtensionDescriptor descriptor = iter.next();
 %0A   
@@ -1500,37 +1500,20 @@
     
-descriptors.remove(descriptor
+iter.remove(
 );%0A 
@@ -1537,35 +1537,40 @@
     %7D%0A    %7D%0A
+    
 %0A    p
-ublic
+rivate
  static Desc
@@ -1635,144 +1635,533 @@
 or%3E 
-allProject() %7B%0A        DescriptorExtensionList%3CGhprbExtension, GhprbExtensionDescriptor%3E list = Jenkins.getInstance().getDescriptor
+getExtensionList() %7B%0A        return Jenkins.getInstance().getDescriptorList(GhprbExtension.class);%0A    %7D%0A    %0A    /**%0A     * Don't mutate the list from Jenkins, they will persist;%0A     * @return list of extensions%0A     */%0A    private static List%3CGhprbExtensionDescriptor%3E getExtensions() %7B%0A        List%3CGhprbExtensionDescriptor%3E list = new ArrayList%3CGhprbExtensionDescriptor%3E();%0A        list.addAll(getExtensionList());%0A        return list;%0A    %7D%0A%0A    public static List%3CGhprbExtensionDescriptor%3E allProject() %7B%0A        
 List
-(
+%3C
 Ghpr
@@ -2162,38 +2162,65 @@
 t%3CGhprbExtension
-.class
+Descriptor%3E list = getExtensions(
 );%0A        filte
@@ -2317,48 +2317,13 @@
 tic 
-DescriptorExtensionList%3CGhprbExtension, 
+List%3C
 Ghpr
@@ -2370,48 +2370,13 @@
     
-DescriptorExtension
 List%3C
-GhprbExtension, 
 Ghpr
@@ -2408,53 +2408,11 @@
 t = 
-Jenkins.getInstance().getDescriptorList(Ghprb
+get
 Exte
@@ -2408,38 +2408,34 @@
 t = getExtension
-.clas
 s
+(
 );%0A        filte

Fix errors where Jenkins list of extensions was being mutated.