Permalink
Browse files

Added support for runtime modifiable Axis

Sometimes there's the need to change the value of an axis right when the build starts.
I did this adding a couple of methods to Axis and letting MatrixBuild use them.
It should all be backwards compatible.
  • Loading branch information...
emanuelez committed Apr 19, 2012
1 parent 593df97 commit b56d34e0a7ed75e0a0e0e7ba2db91d951fc67be1
@@ -67,7 +67,7 @@
* @deprecated as of 1.373
* Use {@link #getValues()}
*/
- public final List<String> values;
+ public List<String> values;
public Axis(String name, List<String> values) {
this.name = name;
@@ -144,6 +144,14 @@ public String getName() {
return Collections.unmodifiableList(values);
}
+ public boolean isComputable() {
+ return false;
+ }
+
+ public List<String> computeValues() {
+ return getValues();
+ }
+
@Override
public AxisDescriptor getDescriptor() {
return (AxisDescriptor)super.getDescriptor();
@@ -23,15 +23,16 @@
*/
package hudson.matrix;
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
import com.thoughtworks.xstream.XStream;
import hudson.Util;
import hudson.util.RobustCollectionConverter;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Arrays;
+import javax.annotation.Nullable;
+import java.util.*;
/**
* List of {@link Axis}.
@@ -74,43 +75,26 @@ public boolean add(Axis axis) {
* List up all the possible combinations of this list.
*/
public Iterable<Combination> list() {
- final int[] base = new int[size()];
- if (base.length==0) return Collections.<Combination>emptyList();
- int b = 1;
- for( int i=size()-1; i>=0; i-- ) {
- base[i] = b;
- b *= get(i).size();
+ List<Set<String>> axesList = Lists.newArrayList();
+
+ for (Axis axis : this) {
+ if (axis.isComputable()) {
+ axesList.add(new HashSet<String>(axis.computeValues()));
+ } else {
+ axesList.add(new HashSet<String>(axis.getValues()));
+ }
}
- final int total = b; // number of total combinations
-
- return new Iterable<Combination>() {
- public Iterator<Combination> iterator() {
- return new Iterator<Combination>() {
- private int counter = 0;
-
- public boolean hasNext() {
- return counter<total;
- }
-
- public Combination next() {
- String[] data = new String[size()];
- int x = counter++;
- for( int i=0; i<data.length; i++) {
- data[i] = get(i).value(x/base[i]);
- x %= base[i];
- }
- assert x==0;
- return new Combination(AxisList.this,data);
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
+ Set<List<String>> combinations = Sets.cartesianProduct(axesList);
+
+ return Iterables.transform(combinations, new Function<List<String>, Combination>() {
+ public Combination apply(@Nullable List<String> strings) {
+ assert strings != null;
+ return new Combination(AxisList.this, (String[]) strings.toArray(new String[0]));
}
- };
+ });
+
}
/**
@@ -546,6 +546,11 @@ private File getConfigurationsDir() {
*/
@Exported
public Collection<MatrixConfiguration> getActiveConfigurations() {
+ try {
+ rebuildConfigurations();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
return activeConfigurations;
}

0 comments on commit b56d34e

Please sign in to comment.