package hudson.plugins.robot.graph;

import hudson.model.Run;
import hudson.plugins.robot.model.RobotTestObject;

import java.text.SimpleDateFormat;
import java.util.Date;


public class RobotBuildLabel implements Comparable<RobotBuildLabel>
{
    private final int buildNr;
    private final String buildLabel;

	public RobotBuildLabel(RobotTestObject obj, String format) {
        Run<?,?> build = obj.getOwner();
        buildNr = build.number;
        buildLabel = formatBuildLabel(format, build.getTime());
    }

    private String formatBuildLabel(String format, Date startTime) {
        String pattern = format.replace("$build",""+buildNr);
        return new SimpleDateFormat(pattern).format(startTime);
    }

	@Override
	public int compareTo(RobotBuildLabel that) {
        return this.buildNr-that.buildNr;
	}

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof RobotBuildLabel))    return false;
        RobotBuildLabel that = (RobotBuildLabel) o;
        return buildNr==that.buildNr;
    }

    @Override
    public int hashCode() {
        return buildNr;
    }

    @Override
    public String toString() { return buildLabel; }
}

@@ -259,19 +259,20 @@
 nal 
-int buildNr
+Run%3C?,?%3E run
 ;%0A  
@@ -381,22 +381,11 @@
     
-R
+r
 un
-%3C?,?%3E build
  = o
@@ -403,40 +403,8 @@
 ();%0A
-        buildNr = build.number;%0A
     
@@ -445,21 +445,19 @@
 format, 
-build
+run
 .getTime
@@ -589,22 +589,25 @@
 ild%22,%22%22+
-buildN
+run.numbe
 r);%0A    
@@ -754,27 +754,33 @@
 his.
-buildNr-that.buildN
+run.number-that.run.numbe
 r;%0A%09
@@ -966,29 +966,21 @@
 urn 
-buildNr==that.buildNr
+run==that.run
 ;%0A  
@@ -1045,15 +1045,22 @@
 urn 
-buildNr
+run.hashCode()
 ;%0A  

fix failing unit tests (non-unique build numbers in unit tests)