Skip to content

Commit

Permalink
Make LwM2mPath Comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 3, 2021
1 parent 7ba642a commit d69f571
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
Expand Up @@ -20,7 +20,7 @@
/**
* A path pointing to a LwM2M node (root, object, object instance, resource or resource instance).
*/
public class LwM2mPath {
public class LwM2mPath implements Comparable<LwM2mPath> {

public static final byte ROOT_DEPTH = 1;
public static final byte OBJECT_DEPTH = 2;
Expand Down Expand Up @@ -368,6 +368,35 @@ public boolean equals(Object obj) {
return true;
}

@Override
public int compareTo(LwM2mPath o) {
int res = compareInteger(this.objectId, o.objectId);
if (res != 0 || this.objectId == null)
return res;

res = compareInteger(this.objectInstanceId, o.objectInstanceId);
if (res != 0 || this.objectInstanceId == null)
return res;

res = compareInteger(this.resourceId, o.resourceId);
if (res != 0 || this.resourceId == null)
return res;

return compareInteger(this.resourceInstanceId, o.resourceInstanceId);
}

private int compareInteger(Integer i1, Integer i2) {
if (i1 == i2) {
return 0;
} else if (i1 == null) {
return -1;
} else if (i2 == null) {
return 1;
} else {
return i1.compareTo(i2);
}
}

/**
* Parse a string containing a full LWM2M path containing rootpath (rt="oma.lwm2m").
* <p>
Expand Down
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2021 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.node;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class LwM2mParhTest {

@Test
public void test_compare_path() {
assertEquals("/", "/");
assertEquals("/1", "/1");
assertEquals("/1/2", "/1/2");
assertEquals("/1/2/3", "/1/2/3");
assertEquals("/1/2/3/4", "/1/2/3/4");

assertFirstSmaller("/", "/1");
assertFirstSmaller("/", "/1/1");
assertFirstSmaller("/", "/1/1/1");
assertFirstSmaller("/", "/1/1/1/1");

assertFirstSmaller("/1", "/1/1");
assertFirstSmaller("/1", "/1/1/1");
assertFirstSmaller("/1", "/1/1/1/1");

assertFirstSmaller("/1/1", "/1/1/1");
assertFirstSmaller("/1/1", "/1/1/1/1");

assertFirstSmaller("/1/1/1", "/1/1/1/1");

assertFirstSmaller("/1", "/2");
assertFirstSmaller("/1/1", "/2");
assertFirstSmaller("/1/1/1", "/2");
assertFirstSmaller("/1/1/1/1", "/2");

assertFirstSmaller("/1", "/2/1");
assertFirstSmaller("/1/1", "/2/1");
assertFirstSmaller("/1/1/1", "/2/1");
assertFirstSmaller("/1/1/1/1", "/2/1");

assertFirstSmaller("/1", "/2/1/1");
assertFirstSmaller("/1/1", "/2/1/1");
assertFirstSmaller("/1/1/1", "/2/1/1");
assertFirstSmaller("/1/1/1/1", "/2/1/1");

assertFirstSmaller("/1", "/2/1/1/1");
assertFirstSmaller("/1/1", "/2/1/1/1");
assertFirstSmaller("/1/1/1", "/2/1/1/1");
assertFirstSmaller("/1/1/1/1", "/2/1/1/1");
}

private void assertEquals(String path1, String path2) {
assertTrue(new LwM2mPath(path1).compareTo(new LwM2mPath(path2)) == 0);
}

private void assertFirstSmaller(String path1, String path2) {
assertTrue(new LwM2mPath(path1).compareTo(new LwM2mPath(path2)) == -1);
assertTrue(new LwM2mPath(path2).compareTo(new LwM2mPath(path1)) == 1);

}
}

0 comments on commit d69f571

Please sign in to comment.