1 package com.atlassian.scheduler.config;
2
3 import com.atlassian.annotations.PublicApi;
4
5 import javax.annotation.Nullable;
6 import javax.annotation.concurrent.Immutable;
7 import java.io.Serializable;
8
9 import static com.atlassian.util.concurrent.Assertions.notNull;
10
11 /**
12 * A wrapper to distinguish job runner keys from simple strings and to make it easier
13 * to avoid confusing them with Job IDs
14 *
15 * @since v1.0
16 */
17 @Immutable
18 @PublicApi
19 public final class JobRunnerKey implements Serializable, Comparable<JobRunnerKey> {
20 private static final long serialVersionUID = 1L;
21
22 /**
23 * Wraps the provided string as a {@code JobRunnerKey}.
24 * <p>
25 * Although it is not necessary for correctness, it will usually make sense to create a single instance of
26 * the {@code JobRunnerKey} and reuse it as a constant, as in:
27 * </p>
28 * <pre><code>
29 * private static final JobRunnerKey <strong>POP3_SERVICE</strong> = JobRunnerKey.of("com.example.plugin.Pop3Service");
30 *
31 * // ...
32 *
33 * private void registerJobRunner()
34 * {
35 * schedulerService.registerJobRunner(<strong>POP3_SERVICE</strong>, new Pop3JobRunner());
36 * }
37 *
38 * private String scheduleJobWithGeneratedId(String cronExpression)
39 * {
40 * JobConfig jobConfig = JobConfig.forJobRunnerKey(<strong>POP3_SERVICE</strong>)
41 * .withSchedule(Schedule.forCronExpression(cronExpression));
42 * return schedulerService.scheduleJobWithGeneratedId(jobConfig);
43 * }
44 * </code></pre>
45 *
46 * @param key the job runner key, as a string
47 * @return the wrapped job runner key
48 */
49 public static JobRunnerKey of(String key) {
50 return new JobRunnerKey(key);
51 }
52
53
54 private final String key;
55
56 private JobRunnerKey(final String key) {
57 this.key = notNull("key", key);
58 }
59
60
61 @Override
62 @SuppressWarnings("SimplifiableIfStatement")
63 public boolean equals(@Nullable final Object o) {
64 if (this == o) {
65 return true;
66 }
67 return o != null
68 && o.getClass() == getClass()
69 && ((JobRunnerKey) o).key.equals(key);
70 }
71
72 @Override
73 public int compareTo(final JobRunnerKey o) {
74 return key.compareTo(o.key);
75 }
76
77 @Override
78 public int hashCode() {
79 return key.hashCode();
80 }
81
82 public String toString() {
83 return key;
84 }
85 }