1 package com.atlassian.scheduler.cron;
2
3 import com.atlassian.scheduler.SchedulerServiceException;
4
5 import javax.annotation.Nonnull;
6 import javax.annotation.Nullable;
7
8 import static com.google.common.base.MoreObjects.firstNonNull;
9
10
11
12
13
14
15 @SuppressWarnings("SerializableHasSerializationMethods")
16 public class CronSyntaxException extends SchedulerServiceException {
17 private static final long serialVersionUID = 5594187147397941674L;
18
19 private final ErrorCode errorCode;
20 private final String cronExpression;
21 private final String value;
22 private final int errorOffset;
23
24
25
26
27 CronSyntaxException(final Builder builder) {
28 super(builder.toMessage());
29 this.errorCode = firstNonNull(builder.errorCode, ErrorCode.INTERNAL_PARSER_FAILURE);
30 this.cronExpression = firstNonNull(builder.cronExpression, "");
31 this.value = builder.value;
32 this.errorOffset = builder.errorOffset;
33 if (builder.cause != null) {
34 initCause(builder.cause);
35 }
36 }
37
38
39
40
41
42
43 @Nonnull
44 public ErrorCode getErrorCode() {
45 return errorCode;
46 }
47
48
49
50
51
52
53 @Nonnull
54 public String getCronExpression() {
55 return cronExpression;
56 }
57
58
59
60
61
62
63
64
65
66 @Nullable
67 public String getValue() {
68 return value;
69 }
70
71
72
73
74
75
76
77 public int getErrorOffset() {
78 return errorOffset;
79 }
80
81
82
83
84
85
86 public static Builder builder() {
87 return new Builder();
88 }
89
90
91
92
93
94 public static class Builder {
95 String cronExpression;
96 String value;
97 ErrorCode errorCode;
98 int errorOffset = -1;
99 Throwable cause;
100
101 Builder() {
102 }
103
104
105
106
107
108 public Builder cronExpression(@Nullable String cronExpression) {
109 this.cronExpression = cronExpression;
110 return this;
111 }
112
113
114
115
116
117 public Builder value(@Nullable String value) {
118 this.value = value;
119 return this;
120 }
121
122
123
124
125
126 public Builder value(char value) {
127 this.value = String.valueOf(value);
128 return this;
129 }
130
131
132
133
134
135 public Builder errorCode(@Nullable ErrorCode errorCode) {
136 this.errorCode = errorCode;
137 return this;
138 }
139
140
141
142
143
144 public Builder errorOffset(int errorOffset) {
145 this.errorOffset = (errorOffset >= 0) ? errorOffset : -1;
146 return this;
147 }
148
149
150
151
152 public Builder cause(@Nullable Throwable cause) {
153 this.cause = cause;
154 return this;
155 }
156
157
158
159
160
161
162 public CronSyntaxException build() {
163 return new CronSyntaxException(this);
164 }
165
166 @Nonnull
167 String toMessage() {
168 return errorCode.toMessage(value);
169 }
170 }
171 }