Skip to content

Commit

Permalink
Use 2 short internally instead of String in Version class.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Sep 15, 2021
1 parent 36e0b64 commit 13c4784
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public class VersionConverter implements ITypeConverter<Version> {

@Override
public Version convert(String input) {
Version version = new Version(input);

String err = version.validate();
String err = Version.validate(input);
if (err != null)
throw new TypeConversionException(err);

return version;
return new Version(input);
}
};
64 changes: 38 additions & 26 deletions leshan-core/src/main/java/org/eclipse/leshan/core/LwM2m.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LwM2mVersion extends Version {
public static LwM2mVersion V1_1 = new LwM2mVersion("1.1", true);
private static LwM2mVersion[] supportedVersions = new LwM2mVersion[] { V1_0, V1_1 };

private boolean supported;
private final boolean supported;

protected LwM2mVersion(String version, boolean supported) {
super(version);
Expand All @@ -39,15 +39,20 @@ public boolean isSupported() {

public static LwM2mVersion get(String version) {
for (LwM2mVersion constantVersion : supportedVersions) {
if (constantVersion.value.equals(version)) {
if (constantVersion.toString().equals(version)) {
return constantVersion;
}
}
return new LwM2mVersion(version, false);
}

public static boolean isSupported(String version) {
return get(version).isSupported();
for (LwM2mVersion constantVersion : supportedVersions) {
if (constantVersion.toString().equals(version)) {
return true;
}
}
return false;
}

public static LwM2mVersion getDefault() {
Expand Down Expand Up @@ -88,23 +93,32 @@ public class Version implements Comparable<Version> {

public static final Version MAX = new Version(Short.MAX_VALUE, Short.MIN_VALUE);

protected String value;
protected final Short major;
protected final Short minor;

public Version(Short major, Short minor) {
this.value = major + "." + minor;
this.major = major;
this.minor = minor;
}

public Version(String version) {
this.value = version;
try {
String[] versionPart = version.split("\\.");
this.major = Short.parseShort(versionPart[0]);
this.minor = Short.parseShort(versionPart[1]);
} catch (RuntimeException e) {
String err = Version.validate(version);
if (err != null) {
throw new IllegalArgumentException(err);
} else {
throw e;
}
}
}

@Override
public String toString() {
return value;
}

public String validate() {
return Version.validate(value);
return String.format("%d.%d", major, minor);
}

public static String validate(String version) {
Expand Down Expand Up @@ -133,7 +147,8 @@ public static String validate(String version) {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
result = prime * result + ((major == null) ? 0 : major.hashCode());
result = prime * result + ((minor == null) ? 0 : minor.hashCode());
return result;
}

Expand All @@ -146,28 +161,25 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
Version other = (Version) obj;
if (value == null) {
if (other.value != null)
if (major == null) {
if (other.major != null)
return false;
} else if (!major.equals(other.major))
return false;
if (minor == null) {
if (other.minor != null)
return false;
} else if (!value.equals(other.value))
} else if (!minor.equals(other.minor))
return false;
return true;
}

@Override
public int compareTo(Version other) {
String[] versionPart = this.value.split("\\.");
String[] otherVersionPart = other.value.split("\\.");

short major = Short.parseShort(versionPart[0]);
short oMajor = Short.parseShort(otherVersionPart[0]);
if (major != oMajor)
return major - oMajor;

short minor = Short.parseShort(versionPart[1]);
short oMinor = Short.parseShort(otherVersionPart[1]);
if (major != other.major)
return major - other.major;

return minor - oMinor;
return minor - other.minor;
}

public boolean newerThan(Version version) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ public Key(Integer id, Version version) {
this.version = version;
}

public void validate() {
LwM2mNodeUtil.validateObjectId(id);
String err = version.validate();
if (err != null) {
throw new IllegalStateException(
String.format("Invalid version %s for object %d : %s", version, id, err));
}
}

@Override
public int compareTo(Key o) {
// handle null
Expand Down Expand Up @@ -119,13 +110,17 @@ public LwM2mModelRepository(Collection<ObjectModel> objectModels) {
} else {
NavigableMap<Key, ObjectModel> map = new TreeMap<>();
for (ObjectModel model : objectModels) {
Key key = getKey(model);
// validate Key
validateKey(model.id, model.version);

// create Key
Key key = getKey(model.id, model.version);
if (key == null) {
throw new IllegalArgumentException(
String.format("Model %s is invalid : object id is missing.", model));
}
key.validate();

// add to the map
ObjectModel old = map.put(key, model);
if (old != null) {
LOG.debug("Model already exists for object {} in version {}. Overriding it.", model.id,
Expand All @@ -137,15 +132,22 @@ public LwM2mModelRepository(Collection<ObjectModel> objectModels) {
}
}

public ObjectModel getObjectModel(Integer objectId, String version) {
Validate.notNull(objectId, "objectid must not be null");
Validate.notNull(version, "version must not be null");
private void validateKey(Integer id, String version) {
LwM2mNodeUtil.validateObjectId(id);
String err = Version.validate(version);
if (err != null) {
throw new IllegalArgumentException(
String.format("Invalid version %s for object %d : %s", version, id, err));
}
}

public ObjectModel getObjectModel(Integer objectId, String version) {
validateKey(objectId, version);
return objects.get(getKey(objectId, version));
}

public ObjectModel getObjectModel(Integer objectId, Version version) {
Validate.notNull(objectId, "objectid must not be null");
LwM2mNodeUtil.validateObjectId(objectId);
Validate.notNull(version, "version must not be null");

return objects.get(getKey(objectId, version));
Expand All @@ -155,27 +157,20 @@ public ObjectModel getObjectModel(Integer objectId, Version version) {
* @return most recent version of the model.
*/
public ObjectModel getObjectModel(Integer objectId) {
Validate.notNull(objectId, "objectid must not be null");

LwM2mNodeUtil.validateObjectId(objectId);
Key floorKey = objects.floorKey(getKey(objectId, Version.MAX));
if (floorKey == null || !floorKey.id.equals(objectId)) {
return null;
}
return objects.get(floorKey);
}

private Key getKey(ObjectModel objectModel) {
return getKey(objectModel.id, objectModel.version);
}

private Key getKey(Integer objectId, String version) {
return getKey(objectId, new Version(version));
}

private Key getKey(Integer objectId, Version version) {
if (objectId == null) {
return null;
}
return new Key(objectId, version);
}

Expand Down

0 comments on commit 13c4784

Please sign in to comment.