ItEr54S04ValidacionEProbasFuncionaisItEr53S04: Add findObjectAt path method to MutableTreeModel.
This commit is contained in:
parent
e20cb57b83
commit
3183a6a61e
2 changed files with 67 additions and 2 deletions
|
|
@ -192,6 +192,32 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
|
|||
return asIntArray(path);
|
||||
}
|
||||
|
||||
public int[] getPath(Object last) {
|
||||
return getPath(getRoot(), last);
|
||||
}
|
||||
|
||||
public T findObjectAt(int... path) {
|
||||
T current = getRoot();
|
||||
for (int i = 0; i < path.length; i++) {
|
||||
int position = path[i];
|
||||
if (position >= getChildCount(current)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Failure acessing the path at: "
|
||||
+ stringRepresentationUntil(path, i));
|
||||
}
|
||||
current = getChild(current, position);
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static String stringRepresentationUntil(int[] path, int endExclusive) {
|
||||
String valid = Arrays.toString(Arrays
|
||||
.copyOfRange(path, 0, endExclusive));
|
||||
String invalid = Arrays.toString(Arrays.copyOfRange(path, endExclusive,
|
||||
path.length));
|
||||
return valid + "^" + invalid;
|
||||
}
|
||||
|
||||
private int[] asIntArray(List<Integer> path) {
|
||||
int[] result = new int[path.size()];
|
||||
int i = 0;
|
||||
|
|
@ -417,6 +443,4 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
|
|||
asList(each, result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -162,6 +162,47 @@ public class MutableTreeModelTest {
|
|||
assertThat(parents, equalTo(Arrays.asList(child, root)));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void findObjectAtInvalidPath() {
|
||||
Prueba root = new Prueba();
|
||||
MutableTreeModel<Prueba> model = MutableTreeModel.create(Prueba.class,
|
||||
root);
|
||||
model.findObjectAt(0, 1);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void findObjectAtInvalidPathInTheMiddle() {
|
||||
Prueba root = new Prueba();
|
||||
MutableTreeModel<Prueba> model = MutableTreeModel.create(Prueba.class,
|
||||
root);
|
||||
Prueba p1 = new Prueba();
|
||||
model.addToRoot(p1);
|
||||
model.addToRoot(new Prueba());
|
||||
model.add(p1, new Prueba());
|
||||
model.findObjectAt(1, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAtPathCanReceiveGetPathResult() {
|
||||
Prueba root = new Prueba();
|
||||
MutableTreeModel<Prueba> model = MutableTreeModel.create(Prueba.class,
|
||||
root);
|
||||
assertTrue(canBeRetrievedWithGetPath(model, model.getRoot()));
|
||||
model.addToRoot(new Prueba());
|
||||
Prueba p2 = new Prueba();
|
||||
model.addToRoot(p2);
|
||||
assertTrue(canBeRetrievedWithGetPath(model, p2));
|
||||
Prueba grandChild = new Prueba();
|
||||
model.add(p2, grandChild);
|
||||
assertTrue(canBeRetrievedWithGetPath(model, grandChild));
|
||||
}
|
||||
|
||||
private static <T> boolean canBeRetrievedWithGetPath(
|
||||
final MutableTreeModel<T> tree, T object) {
|
||||
int[] path = tree.getPath(object);
|
||||
return tree.findObjectAt(path).equals(object);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getParentOfRootThrowsException() {
|
||||
Prueba root = new Prueba();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue