|
@@ -24,8 +24,14 @@ |
|
|
|
|
|
package jenkins.scm.api.trait; |
|
|
|
|
|
import edu.umd.cs.findbugs.annotations.CheckForNull; |
|
|
import edu.umd.cs.findbugs.annotations.NonNull; |
|
|
import edu.umd.cs.findbugs.annotations.Nullable; |
|
|
import hudson.DescriptorExtensionList; |
|
|
import hudson.model.AbstractDescribableImpl; |
|
|
import java.util.ArrayList; |
|
|
import java.util.HashSet; |
|
|
import java.util.Set; |
|
|
import jenkins.model.Jenkins; |
|
|
|
|
|
/** |
|
@@ -54,4 +60,80 @@ |
|
|
return Jenkins.getActiveInstance().getDescriptorList(specialization); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Converts the supplied list of {@link SCMTrait} instances into a list where there is at most one instance of |
|
|
* each trait. |
|
|
* |
|
|
* @param list the list to apply the constraint to. |
|
|
* @param <T> type of {@link SCMTrait}. |
|
|
* @return a new list that contains the first instance of any type of trait in the supplied list. |
|
|
* @since 2.2.0 |
|
|
*/ |
|
|
@NonNull |
|
|
public static <T extends SCMTrait<?>> ArrayList<T> asSetList(@CheckForNull Iterable<? extends T> list) { |
|
|
ArrayList<T> result = new ArrayList<T>(); |
|
|
if (list != null) { |
|
|
Set<Class> seen = new HashSet<Class>(); |
|
|
for (T trait : list) { |
|
|
if (trait == null) { |
|
|
continue; |
|
|
} |
|
|
if (seen.contains(trait.getClass())) { |
|
|
continue; |
|
|
} |
|
|
seen.add(trait.getClass()); |
|
|
result.add(trait); |
|
|
} |
|
|
} |
|
|
return result; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Converts the supplied instance and list of {@link SCMTrait} instances into a list where there is at most one |
|
|
* instance of each trait. |
|
|
* |
|
|
* @param list the list to apply the constraint to. |
|
|
* @param <T> type of {@link SCMTrait}. |
|
|
* @return a new list that contains the first instance of any type of trait in the supplied list. |
|
|
* @since 2.2.0 |
|
|
*/ |
|
|
public static <T extends SCMTrait<?>> ArrayList<T> asSetList(@NonNull T first, @CheckForNull Iterable<? extends T> list) { |
|
|
ArrayList<T> result = new ArrayList<T>(); |
|
|
result.add(first); |
|
|
if (list != null) { |
|
|
Set<Class> seen = new HashSet<Class>(); |
|
|
seen.add(first.getClass()); |
|
|
for (T trait : list) { |
|
|
if (trait == null) { |
|
|
continue; |
|
|
} |
|
|
if (seen.contains(trait.getClass())) { |
|
|
continue; |
|
|
} |
|
|
seen.add(trait.getClass()); |
|
|
result.add(trait); |
|
|
} |
|
|
} |
|
|
return result; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Finds the trait of the required type. |
|
|
* |
|
|
* @param traits the traits to search. |
|
|
* @param clazz the type of trait. |
|
|
* @param <T> the type of trait. |
|
|
* @return the matching trait from the supplied traits or {@code null} if there is no matching trait. |
|
|
* @since 2.2.0 |
|
|
*/ |
|
|
@CheckForNull |
|
|
public static <T extends SCMTrait<?>> T find(@NonNull Iterable<?> traits, @NonNull Class<T> clazz) { |
|
|
for (Object trait : traits) { |
|
|
if (clazz.isInstance(trait)) { |
|
|
return clazz.cast(trait); |
|
|
} |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
} |