Combination.java 4.9 KB
Newer Older
1 2 3
package hudson.matrix;

import java.util.Arrays;
K
kohsuke 已提交
4 5
import java.util.Collections;
import java.util.HashMap;
K
kohsuke 已提交
6
import java.util.Iterator;
7 8 9
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
K
kohsuke 已提交
10
import java.util.TreeMap;
11
import java.util.Collection;
K
kohsuke 已提交
12 13
import java.util.Set;
import java.util.HashSet;
14 15 16 17 18 19 20 21 22

/**
 * A particular combination of {@link Axis} values.
 *
 * For example, when axes are "x={1,2},y={3,4}", then
 * [1,3] is a combination (out of 4 possible combinations)
 *
 * @author Kohsuke Kawaguchi
 */
K
kohsuke 已提交
23
public final class Combination extends TreeMap<String,String> implements Comparable<Combination> {
24 25 26

    public Combination(AxisList axisList, List<String> values) {
        for(int i=0; i<axisList.size(); i++)
27
            super.put(axisList.get(i).name,values.get(i));
28 29 30 31 32 33 34
    }

    public Combination(AxisList axisList,String... values) {
        this(axisList,Arrays.asList(values));
    }

    public Combination(Map<String,String> keyValuePairs) {
K
kohsuke 已提交
35 36
        for (Map.Entry<String, String> e : keyValuePairs.entrySet())
            super.put(e.getKey(),e.getValue());
37 38
    }

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    public int compareTo(Combination that) {
        int d = this.size()-that.size();
        if(d!=0)    return d;

        Iterator<Map.Entry<String,String>> itr = this.entrySet().iterator();
        Iterator<Map.Entry<String,String>> jtr = that.entrySet().iterator();
        while(itr.hasNext()) {
            Map.Entry<String,String> i = itr.next();
            Map.Entry<String,String> j = jtr.next();

            d = i.getKey().compareTo(j.getKey());
            if(d!=0)    return d;
            d = i.getValue().compareTo(j.getValue());
            if(d!=0)    return d;
        }
        return 0;
    }

    /**
     * Works like {@link #toString()} but only include the given axes.
     */
    public String toString(Collection<Axis> subset) {
        StringBuilder buf = new StringBuilder();
        for (Axis a : subset) {
            if(buf.length()>0) buf.append(',');
            buf.append(a.name).append('=').append(get(a.name));
        }
        if(buf.length()==0) buf.append("default"); // special case to avoid 0-length name.
        return buf.toString();
    }
    
70 71 72 73 74 75 76 77 78 79
    /**
     * Converts to the ID string representation:
     * <tt>axisName=value,axisName=value,...</tt>
     */
    public String toString() {
        StringBuilder buf = new StringBuilder();
        for (Map.Entry<String,String> e : entrySet()) {
            if(buf.length()>0) buf.append(',');
            buf.append(e.getKey()).append('=').append(e.getValue());
        }
80
        if(buf.length()==0) buf.append("default"); // special case to avoid 0-length name.
81 82 83 84 85 86 87
        return buf.toString();
    }

    /**
     * Reverse operation of {@link #toString()}.
     */
    public static Combination fromString(String id) {
88 89 90
        if(id.equals("default"))
            return new Combination(Collections.<String,String>emptyMap());

91 92 93 94 95 96 97 98 99 100 101 102
        Map<String,String> m = new HashMap<String,String>();
        StringTokenizer tokens = new StringTokenizer(id, ",");
        while(tokens.hasMoreTokens()) {
            String token = tokens.nextToken();
            int idx = token.indexOf('=');
            if(idx<0)
                throw new IllegalArgumentException("Can't parse "+id);
            m.put(token.substring(0,idx),token.substring(idx+1));
        }
        return new Combination(m);
    }

K
kohsuke 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    /**
     * Creates compact string representataion suitable for display purpose.
     *
     * <p>
     * The string is made compact by looking for {@link Axis} whose values
     * are unique, and omit the axis name.
     */
    public String toCompactString(AxisList axes) {
        Set<String> nonUniqueAxes = new HashSet<String>();
        Map<String,Axis> axisByValue = new HashMap<String,Axis>();

        for (Axis a : axes) {
            for (String v : a.values) {
                Axis old = axisByValue.put(v,a);
                if(old!=null) {
                    // these two axes have colliding values
                    nonUniqueAxes.add(old.name);
                    nonUniqueAxes.add(a.name);
                }
            }
        }

        StringBuilder buf = new StringBuilder();
        for (Map.Entry<String,String> e : entrySet()) {
            if(buf.length()>0) buf.append(',');
            if(nonUniqueAxes.contains(e.getKey()))
                buf.append(e.getKey()).append('=');
            buf.append(e.getValue());
        }
        if(buf.length()==0) buf.append("default"); // special case to avoid 0-length name.
        return buf.toString();
    }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    // read-only
    public void clear() {
        throw new UnsupportedOperationException();
    }

    public void putAll(Map<? extends String, ? extends String> map) {
        throw new UnsupportedOperationException();
    }

    public String put(String key, String value) {
        throw new UnsupportedOperationException();
    }

    public String remove(Object key) {
        throw new UnsupportedOperationException();
    }
}