Result.java 2.5 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5
package hudson.model;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter;

6 7
import java.io.Serializable;

8 9
import hudson.api.CustomExposureBean;

K
kohsuke 已提交
10 11 12 13 14
/**
 * The build outcome.
 *
 * @author Kohsuke Kawaguchi
 */
15
public final class Result implements Serializable, CustomExposureBean {
K
kohsuke 已提交
16 17 18
    /**
     * The build didn't have any fatal errors not errors.
     */
19
    public static final Result SUCCESS = new Result("SUCCESS",BallColor.BLUE,0);
K
kohsuke 已提交
20 21 22
    /**
     * The build didn't have any fatal errors but some errors.
     */
23
    public static final Result UNSTABLE = new Result("UNSTABLE",BallColor.YELLOW,1);
K
kohsuke 已提交
24 25 26
    /**
     * The build had a fatal error.
     */
27
    public static final Result FAILURE = new Result("FAILURE",BallColor.RED,2);
K
kohsuke 已提交
28 29 30
    /**
     * The build was manually aborted.
     */
31
    public static final Result ABORTED = new Result("ABORTED",BallColor.GREY,3);
K
kohsuke 已提交
32 33 34 35 36 37 38 39

    private final String name;

    /**
     * Bigger numbers are worse.
     */
    private final int ordinal;

40 41 42 43 44 45
    /**
     * Default ball color for this status.
     */
    public final BallColor color;

    private Result(String name, BallColor color, int ordinal) {
K
kohsuke 已提交
46
        this.name = name;
47
        this.color = color;
K
kohsuke 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        this.ordinal = ordinal;
    }

    /**
     * Combines two {@link Result}s and returns the worse one.
     */
    public Result combine(Result that) {
        if(this.ordinal < that.ordinal)
            return that;
        else
            return this;
    }

    public boolean isWorseThan(Result that) {
        return this.ordinal > that.ordinal;
    }

K
kohsuke 已提交
65 66 67 68
    public boolean isWorseOrEqualTo(Result that) {
        return this.ordinal >= that.ordinal;
    }

K
kohsuke 已提交
69 70 71
    public String toString() {
        return name;
    }
72 73 74 75 76 77 78 79
    
    private Object readResolve() {
        for (Result r : all)
            if (ordinal==r.ordinal)
                return r;
        return FAILURE;
    }

80 81 82 83
    public Object toExposedObject() {
        return name;
    }

84
    private static final long serialVersionUID = 1L;
K
kohsuke 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    private static final Result[] all = new Result[] {SUCCESS,UNSTABLE,FAILURE,ABORTED};

    public static final Converter conv = new AbstractBasicConverter () {
        public boolean canConvert(Class clazz) {
            return clazz==Result.class;
        }

        protected Object fromString(String s) {
            for (Result r : all)
                if (s.equals(r.name))
                    return r;
            return FAILURE;
        }
    };
}