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