-
Notifications
You must be signed in to change notification settings - Fork 215
/
MessageExpiryInterval.java
104 lines (90 loc) · 3.34 KB
/
MessageExpiryInterval.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.model.mqtt;
import java.util.Objects;
import java.util.OptionalLong;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* Representation of the MQTT 5 message expiry interval.
* The minimum seconds is {@value MIN_INTERVAL_SECONDS}.
* The maximum seconds is {@value MAX_INTERVAL_SECONDS}.
*/
@Immutable
public final class MessageExpiryInterval {
public static final long MIN_INTERVAL_SECONDS = 1L;
public static final long MAX_INTERVAL_SECONDS = 4_294_967_295L;
@Nullable private final Long seconds;
private MessageExpiryInterval(@Nullable final Long seconds) {
this.seconds = seconds;
}
/**
* Returns an instance of {@code MessageExpiryInterval} for the specified interval.
*
* @param seconds the message expiry interval duration in seconds. The value must be between
* {@value #MIN_INTERVAL_SECONDS} and {@value #MAX_INTERVAL_SECONDS} (both inclusive).
* @return the MessageExpiryInterval for interval of {@code seconds}.
* @throws IllegalMessageExpiryIntervalSecondsException if the seconds is less than {@value MIN_INTERVAL_SECONDS} or greater than {@value #MAX_INTERVAL_SECONDS}.
*/
public static MessageExpiryInterval of(final long seconds)
throws IllegalMessageExpiryIntervalSecondsException {
if (seconds < MIN_INTERVAL_SECONDS || seconds > MAX_INTERVAL_SECONDS) {
throw new IllegalMessageExpiryIntervalSecondsException(
String.format("Expected message expiry interval seconds to be within [%d, %d] but it was <%d>.",
MIN_INTERVAL_SECONDS,
MAX_INTERVAL_SECONDS,
seconds),
null);
}
return new MessageExpiryInterval(seconds);
}
/**
* Returns an instance of the empty {@code MessageExpiryInterval}.
*
* @return the empty message expiry interval.
*/
public static MessageExpiryInterval empty() {
return new MessageExpiryInterval(null);
}
/**
* Returns message expiry interval seconds.
*
* @return optional count of seconds in the interval.
*/
public OptionalLong getAsOptionalLong() {
return seconds != null ?
OptionalLong.of(seconds) :
OptionalLong.empty();
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final MessageExpiryInterval that = (MessageExpiryInterval) o;
return Objects.equals(seconds, that.seconds);
}
@Override
public int hashCode() {
return Objects.hash(seconds);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"seconds=" + seconds +
"]";
}
}