Skip to content

Commit 95f4164

Browse files
committed
Add a general-purpose tree node data structure
This will come in handy for the ImageJ-OMERO project's ROI support.
1 parent d872c54 commit 95f4164

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.72.1-SNAPSHOT</version>
13+
<version>2.73.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.</description>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.util;
34+
35+
import java.util.ArrayList;
36+
import java.util.List;
37+
38+
/**
39+
* Default implementation of {@link TreeNode}.
40+
*
41+
* @author Alison Walter
42+
* @param <T> type of data associated with the node
43+
*/
44+
public class DefaultTreeNode<T> implements TreeNode<T> {
45+
46+
private TreeNode<?> parent;
47+
private final List<TreeNode<?>> children;
48+
private final T data;
49+
50+
/**
51+
* Creates a new tree node wrapping the given data, located in the tree
52+
* beneath the specified parent.
53+
*
54+
* @param data The data to wrap.
55+
* @param parent The parent node of the tree.
56+
*/
57+
public DefaultTreeNode(final T data, final TreeNode<?> parent) {
58+
this.data = data;
59+
this.parent = parent;
60+
children = new ArrayList<>();
61+
}
62+
63+
@Override
64+
public T data() {
65+
return data;
66+
}
67+
68+
@Override
69+
public TreeNode<?> parent() {
70+
return parent;
71+
}
72+
73+
@Override
74+
public void setParent(final TreeNode<?> parent) {
75+
this.parent = parent;
76+
}
77+
78+
@Override
79+
public List<TreeNode<?>> children() {
80+
return children;
81+
}
82+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.util;
34+
35+
import java.util.List;
36+
37+
/**
38+
* A wrapper around a data object, for storing it in a tree structure.
39+
*
40+
* @author Alison Walter
41+
* @param <T> type of data associated with the node
42+
*/
43+
public interface TreeNode<T> {
44+
45+
/** Gets the data associated with the node. */
46+
T data();
47+
48+
/** Gets the parent of this node. */
49+
TreeNode<?> parent();
50+
51+
void setParent(TreeNode<?> parent);
52+
53+
/**
54+
* Gets the node's children. If this list is mutated, the children will be
55+
* affected accordingly. It is the responsibility of the caller to ensure
56+
* continued integrity, particularly of parent linkages.
57+
*/
58+
List<TreeNode<?>> children();
59+
60+
/** Adds the given list of children to this node. */
61+
default void addChildren(final List<? extends TreeNode<?>> nodes) {
62+
for (final TreeNode<?> child : nodes) {
63+
child.setParent(parent());
64+
}
65+
children().addAll(nodes);
66+
}
67+
}

0 commit comments

Comments
 (0)