Skip to content
Permalink
Browse files

[JENKINS-43507] Add some helper methods

  • Loading branch information...
stephenc committed Jul 5, 2017
1 parent 81dcf18 commit 82ee345a8c12df789d13a8962ab06e74960e7480
@@ -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;
}

}
@@ -61,7 +61,7 @@
@DataBoundConstructor
public MockSCMNavigator(String controllerId, List<SCMTrait<?>> traits) {
this.controllerId = controllerId;
this.traits = new ArrayList<SCMTrait<?>>(traits);
this.traits = SCMTrait.asSetList(traits);
}

public MockSCMNavigator(String controllerId, SCMTrait<?>... traits) {
@@ -71,7 +71,7 @@ public MockSCMNavigator(String controllerId, SCMTrait<?>... traits) {
public MockSCMNavigator(MockSCMController controller, List<SCMTrait<?>> traits) {
this.controllerId = controller.getId();
this.controller = controller;
this.traits = new ArrayList<SCMTrait<?>>(traits);
this.traits = SCMTrait.asSetList(traits);
}

public MockSCMNavigator(MockSCMController controller, SCMTrait<?>... traits) {
@@ -29,7 +29,6 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.RestrictedSince;
import hudson.model.Action;
import hudson.model.TaskListener;
import hudson.scm.SCM;
@@ -59,13 +58,12 @@
import jenkins.scm.api.trait.SCMSourceRequest;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import jenkins.scm.api.trait.SCMTrait;
import jenkins.scm.impl.ChangeRequestSCMHeadCategory;
import jenkins.scm.impl.TagSCMHeadCategory;
import jenkins.scm.impl.UncategorizedSCMHeadCategory;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

@@ -79,7 +77,7 @@
public MockSCMSource(String controllerId, String repository, List<SCMSourceTrait> traits) {
this.controllerId = controllerId;
this.repository = repository;
this.traits = new ArrayList<SCMSourceTrait>(traits);
this.traits = SCMTrait.asSetList(traits);
}

public MockSCMSource(String controllerId, String repository, SCMSourceTrait... traits) {
@@ -90,7 +88,7 @@ public MockSCMSource(MockSCMController controller, String repository, List<SCMSo
this.controllerId = controller.getId();
this.controller = controller;
this.repository = repository;
this.traits = new ArrayList<SCMSourceTrait>(traits);
this.traits = SCMTrait.asSetList(traits);
}

public MockSCMSource(MockSCMController controller, String repository, SCMSourceTrait... traits) {

0 comments on commit 82ee345

Please sign in to comment.
You can’t perform that action at this time.