1 package org.kuali.common.devops.jenkins.scan.function;
2
3 import static com.google.common.base.Preconditions.checkArgument;
4 import static org.kuali.common.util.base.Exceptions.illegalState;
5 import static org.kuali.common.util.base.Precondition.checkNotNull;
6
7 import java.io.File;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.TimeZone;
12
13 import org.kuali.common.core.build.ValidatingBuilder;
14 import org.kuali.common.core.validate.annotation.IdiotProofImmutable;
15 import org.kuali.common.util.file.CanonicalFile;
16
17 import com.google.common.base.Function;
18
19 @IdiotProofImmutable
20 public final class JobExecutionStartTimeFunction implements Function<File, Long> {
21
22 private final String datePattern;
23 private final String timeZoneId;
24
25 @Override
26 public Long apply(File jenkinsBuildDirectory) {
27
28
29 checkNotNull(jenkinsBuildDirectory, "jenkinsBuildDirectory");
30
31
32 checkArgument(jenkinsBuildDirectory.isDirectory(), "[%s] is not an existing directory", jenkinsBuildDirectory);
33
34
35 SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
36 sdf.setTimeZone(TimeZone.getTimeZone(timeZoneId));
37
38
39 File canonical = new CanonicalFile(jenkinsBuildDirectory);
40
41
42 String path = canonical.getPath();
43
44
45 String dateFragment = path.substring(path.lastIndexOf(File.separator) + 1);
46
47
48 try {
49 Date date = sdf.parse(dateFragment);
50 return date.getTime();
51 } catch (ParseException e) {
52 throw illegalState(e);
53 }
54 }
55
56 private JobExecutionStartTimeFunction(Builder builder) {
57 this.datePattern = builder.datePattern;
58 this.timeZoneId = builder.timeZoneId;
59 }
60
61 public static JobExecutionStartTimeFunction build() {
62 return builder().build();
63 }
64
65 public static Builder builder() {
66 return new Builder();
67 }
68
69 public static class Builder extends ValidatingBuilder<JobExecutionStartTimeFunction> {
70
71 private String datePattern = "yyyy-MM-dd_HH-mm-ss";
72 private String timeZoneId = "US/Eastern";
73
74 public Builder withDatePattern(String pattern) {
75 this.datePattern = pattern;
76 return this;
77 }
78
79 public Builder withTimeZoneId(String timeZoneId) {
80 this.timeZoneId = timeZoneId;
81 return this;
82 }
83
84 @Override
85 public JobExecutionStartTimeFunction build() {
86 return validate(new JobExecutionStartTimeFunction(this));
87 }
88 }
89
90 public String getDatePattern() {
91 return datePattern;
92 }
93
94 public String getTimeZoneId() {
95 return timeZoneId;
96 }
97 }