/
LwM2m.java
216 lines (186 loc) · 7.27 KB
/
LwM2m.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
* Copyright (c) 2017 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.core;
public interface LwM2m {
/**
* Version of LWM2M specification.
*/
public class LwM2mVersion extends Version {
public static final LwM2mVersion V1_0 = new LwM2mVersion("1.0", true);
public static final LwM2mVersion V1_1 = new LwM2mVersion("1.1", true);
private static final LwM2mVersion[] supportedVersions = new LwM2mVersion[] { V1_0, V1_1 };
private final boolean supported;
protected LwM2mVersion(String version, boolean supported) {
super(version);
this.supported = supported;
}
public boolean isSupported() {
return supported;
}
public static LwM2mVersion get(String version) {
for (LwM2mVersion constantVersion : supportedVersions) {
if (constantVersion.toString().equals(version)) {
return constantVersion;
}
}
return new LwM2mVersion(version, false);
}
public static boolean isSupported(String version) {
for (LwM2mVersion constantVersion : supportedVersions) {
if (constantVersion.toString().equals(version)) {
return true;
}
}
return false;
}
public static LwM2mVersion getDefault() {
return V1_0;
}
public static LwM2mVersion lastSupported() {
return V1_1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + (supported ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
LwM2mVersion other = (LwM2mVersion) obj;
if (supported != other.supported)
return false;
return true;
}
}
/**
* Generic class to handle Version (e.g. Object versioning)
*/
public class Version implements Comparable<Version> {
public static final Version V1_0 = new Version("1.0");
public static final Version MAX = new Version(Short.MAX_VALUE, Short.MAX_VALUE);
private static short toShortExact(int value, String format, Object... args) {
if ((short) value != value) {
throw new IllegalArgumentException(String.format(format, args));
}
return (short) value;
}
protected final short major;
protected final short minor;
public Version(int major, int minor) {
this(toShortExact(major, "version (%d.%d) major part (%d) is not a valid short", major, minor, major),
toShortExact(minor, "version (%d.%d) minor part (%d) is not a valid short", major, minor, minor));
}
public Version(short major, short minor) {
this.major = major;
if (this.major < 0) {
throw new IllegalArgumentException(
String.format("version (%d.%d) major part (%d) must not be negative", major, minor, major));
}
this.minor = minor;
if (this.minor < 0) {
throw new IllegalArgumentException(
String.format("version (%d.%d) minor part (%d) must not be negative", major, minor, minor));
}
}
public Version(String version) {
String err = Version.validate(version);
if (err != null) {
throw new IllegalArgumentException(err);
}
String[] versionPart = version.split("\\.");
this.major = Short.parseShort(versionPart[0]);
this.minor = Short.parseShort(versionPart[1]);
}
@Override
public String toString() {
return String.format("%d.%d", major, minor);
}
public static Version getDefault() {
return V1_0;
}
public static String validate(String version) {
if (version == null || version.isEmpty())
return "version MUST NOT be null or empty";
String[] versionPart = version.split("\\.");
if (versionPart.length != 2) {
return String.format("version (%s) MUST be composed of 2 parts", version);
}
for (int i = 0; i < 2; i++) {
try {
short parsedShort = Short.parseShort(versionPart[i]);
if (parsedShort < 0) {
return String.format("version (%s) part %d (%s) must not be negative", version, i + 1,
versionPart[i]);
}
} catch (Exception e) {
return String.format("version (%s) part %d (%s) is not a valid short", version, i + 1,
versionPart[i]);
}
}
return null;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + major;
result = prime * result + minor;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Version other = (Version) obj;
if (major != other.major)
return false;
if (minor != other.minor)
return false;
return true;
}
@Override
public int compareTo(Version other) {
if (major != other.major)
return major - other.major;
return minor - other.minor;
}
public boolean newerThan(Version version) {
return this.compareTo(version) > 0;
}
public boolean olderThan(Version version) {
return this.compareTo(version) < 0;
}
public boolean newerThan(String version) {
return newerThan(new Version(version));
}
}
/** The default CoAP port for unsecured CoAP communication */
public static final int DEFAULT_COAP_PORT = 5683;
/** The default CoAP port for secure CoAP communication */
public static final int DEFAULT_COAP_SECURE_PORT = 5684;
}