-
Notifications
You must be signed in to change notification settings - Fork 215
/
ReceiveMaximum.java
124 lines (107 loc) · 3.7 KB
/
ReceiveMaximum.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2022 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.service.config;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.common.ConditionChecker;
/**
* Representation of the MQTT 5 Receive Maximum.
* The Server uses this value to limit the number of QoS 1 and QoS 2 publications that it is willing to process
* concurrently for the Client.
* It does not provide a mechanism to limit the QoS 0 publications that the Client might try to send.
*/
@Immutable
public final class ReceiveMaximum implements Comparable<ReceiveMaximum> {
/**
* The minimum value the Receive Maximum as defined by protocol specification.
*/
static final int MIN_VALUE = 1;
/**
* The maximum value (65535) for the Receive Maximum as defined by protocol specification.
*/
static final int MAX_VALUE = 0xFFFF;
/**
* The default value for the Receive Maximum.
*/
public static final int DEFAULT_VALUE = MAX_VALUE;
private final int value;
private ReceiveMaximum(final int value) {
this.value = value;
}
/**
* Returns an instance of {@code ReceiveMaximum} for the specified integer value.
*
* @param value the value of the Receive Maximum. It must be between (inclusive) {@value #MIN_VALUE} and
* {@value #DEFAULT_VALUE}.
* @return the ReceiveMaximum for {@code value}.
* @throws IllegalReceiveMaximumValueException if {@code value} is less than {@value #MIN_VALUE} or greater than
* {@value #DEFAULT_VALUE}.
*/
public static ReceiveMaximum of(final int value) throws IllegalReceiveMaximumValueException {
if (value < MIN_VALUE || value > MAX_VALUE) {
throw new IllegalReceiveMaximumValueException(
String.format("Expected value to be within [%d, %d] but it was <%d>.",
MIN_VALUE,
MAX_VALUE,
value),
null
);
} else {
return new ReceiveMaximum(value);
}
}
/**
* Returns an instance of {@code ReceiveMaximum} with the default value of {@value #DEFAULT_VALUE}.
* This is the default and value that is determined by MQTT 5 protocol specification.
*
* @return the default Receive Maximum.
*/
public static ReceiveMaximum defaultReceiveMaximum() {
return new ReceiveMaximum(DEFAULT_VALUE);
}
/**
* Returns the value of this Receive Maximum.
*
* @return the value.
*/
public int getValue() {
return value;
}
@Override
public int compareTo(final ReceiveMaximum o) {
ConditionChecker.checkNotNull(o, "o");
return Integer.compare(value, o.value);
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (ReceiveMaximum) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"value=" + value +
"]";
}
}