package hudson.matrix;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * List of {@link Axis}.
 * 
 * @author Kohsuke Kawaguchi
 */
public class AxisList extends ArrayList<Axis> {
    public AxisList() {
    }

    public AxisList(Collection<Axis> c) {
        super(c);
    }

    public Axis find(String name) {
        for (Axis a : this) {
            if(a.name.equals(name))
                return a;
        }
        return null;
    }

    public boolean add(Axis axis) {
        return axis!=null && super.add(axis);
    }

    /**
     * List up all the possible combinations of this list.
     */
    public Iterable<Combination> list() {
        final int[] base = new int[size()];

        int b = 1;
        for( int i=size()-1; i>=0; i-- ) {
            base[i] = b;
            b *= get(i).size();
        }

        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();
                    }
                };
            }
        };
    }
}

@@ -28,83 +28,711 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Iterator
+com.thoughtworks.xstream.converters.Converter;%0Aimport com.thoughtworks.xstream.converters.MarshallingContext;%0Aimport com.thoughtworks.xstream.converters.UnmarshallingContext;%0Aimport com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;%0Aimport com.thoughtworks.xstream.mapper.Mapper;%0Aimport com.thoughtworks.xstream.io.HierarchicalStreamWriter;%0Aimport com.thoughtworks.xstream.io.HierarchicalStreamReader;%0Aimport com.thoughtworks.xstream.alias.CannotResolveClassException;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0Aimport hudson.util.CopyOnWriteList
 ;%0A%0A/
@@ -2519,11 +2519,1339 @@
 %7D;%0A    %7D
+%0A%0A    /**%0A     * %7B@link Converter%7D implementation for XStream.%0A     */%0A    public static final class ConverterImpl extends AbstractCollectionConverter %7B%0A        public ConverterImpl(Mapper mapper) %7B%0A            super(mapper);%0A        %7D%0A%0A        public boolean canConvert(Class type) %7B%0A            return type==AxisList.class;%0A        %7D%0A%0A        public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) %7B%0A            for (Object o : (AxisList) source)%0A                writeItem(o, context, writer);%0A        %7D%0A%0A        @SuppressWarnings(%22unchecked%22)%0A        public AxisList unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) %7B%0A            // read the items from xml into a list%0A            List items = new ArrayList();%0A            while (reader.hasMoreChildren()) %7B%0A                reader.moveDown();%0A                try %7B%0A                    Object item = readItem(reader, context, items);%0A                    items.add(item);%0A                %7D catch (CannotResolveClassException e) %7B%0A                    LOGGER.log(Level.WARNING,%22Failed to resolve class%22,e);%0A                %7D%0A                reader.moveUp();%0A            %7D%0A%0A            return new AxisList(items);%0A        %7D%0A    %7D%0A%0A    private static final Logger LOGGER = Logger.getLogger(AxisList.class.getName());
 %0A%7D%0A

improved serialization format.