1111
1212package com .arm .cmsis .pack .generic ;
1313
14+ import java .util .ArrayList ;
1415import java .util .Collection ;
1516import java .util .Collections ;
1617import java .util .LinkedList ;
1718
1819
1920/**
2021 * Generic template-based interface for tree like structures
21- *
22- * @param <T> type of items to store in the tree, must implement ITreeItem interface itself
22+ *
23+ * @param <T> type of items to store in the tree, must implement ITreeItem interface itself
2324 */
2425public interface ITreeItem <T extends ITreeItem <T >> extends ITreeObject {
25-
26+
2627 /**
2728 * Returns immediate parent of this item
28- * @return immediate parent item or null if this item is top-level item
29+ * @return immediate parent item or null if this item is top-level item
2930 */
3031 @ Override
3132 T getParent ();
3233
3334 /**
34- * Sets parent item for this item.
35+ * Sets parent item for this item.
3536 * @param T parent item
3637 */
3738 void setParent (T parent );
3839
39-
40+
4041 /**
41- * Returns top-level parent of the hierarchy (the item that has no parent above)
42- * @return top parent item
42+ * Returns top-level parent of the hierarchy (the item that has no parent above)
43+ * @return top parent item
4344 */
4445 default T getRoot () {
4546 if (getParent () == null ) {
4647 return getThisItem ();
4748 }
4849 return getParent ().getRoot ();
4950 }
50-
51-
51+
52+
5253 /**
5354 * Returns this item
5455 * @return this item
@@ -58,33 +59,33 @@ default T getThisItem() {
5859 return (T )this ; // we know that this item type is T : T extends ITreeItem<T>
5960 }
6061
61-
62+
6263 /**
63- * Returns object of type T effectively associated with this item
64+ * Returns object of type T effectively associated with this item
6465 * @return effective object of type T
65- * @see #getEffectiveHierarchyItem()
66+ * @see #getEffectiveHierarchyItem()
6667 */
67- T getEffectiveItem ();
68+ default T getEffectiveItem () { return getThisItem ();} // default returns this
6869
6970 /**
7071 * Function symmetric to <code>getEffectiveItem()</code> fulfilling condition:
7172 * <p/>
72- * <code>getEffectiveItem().getEffectiveHierarchyItem() == this</code>
73+ * <code>getEffectiveItem().getEffectiveHierarchyItem() == this</code>
7374 * <p/>
7475 * @return item that represents a node in effective tree. That could be:
75- * <ul>
76+ * <ul>
7677 * <li> the item itself
7778 * <li> item's effective parent
7879 * </ul>
79- * @see #getEffectiveItem()
80+ * @see #getEffectiveItem()
8081 */
81- T getEffectiveHierarchyItem ();
82+ default T getEffectiveHierarchyItem () { return getThisItem ();} // default returns this
8283
8384 /**
84- * Returns effective parent of this item which might be the immediate parent or higher-level parent in the hierarchy
85- * @return effective parent item
85+ * Returns effective parent of this item which might be the immediate parent or higher-level parent in the hierarchy
86+ * @return effective parent item
8687 */
87- T getEffectiveParent ();
88+ default T getEffectiveParent () { return getParent ();}
8889
8990
9091 /**
@@ -101,7 +102,7 @@ default <C> C getParentOfType(Class<C> type) {
101102 }
102103 return null ;
103104 }
104-
105+
105106 /**
106107 * Returns collection of children of specified type
107108 * @param type type class type to search
@@ -110,7 +111,7 @@ default <C> C getParentOfType(Class<C> type) {
110111 default <C > Collection <C > getChildrenOfType (Class <C > type ) {
111112 if (!hasChildren ())
112113 return Collections .emptyList ();
113-
114+
114115 LinkedList <C > typedChildren = new LinkedList <>();
115116 for ( T child : getChildren ()) {
116117 if (type .isInstance (child )) {
@@ -119,7 +120,7 @@ default <C> Collection<C> getChildrenOfType(Class<C> type) {
119120 }
120121 return typedChildren ;
121122 }
122-
123+
123124 /**
124125 * Returns collection of children of specified type recursively
125126 * @param type type class type to search
@@ -140,79 +141,79 @@ default <C> Collection<C> getAllChildrenOfType(Collection<C> allChildren, Class<
140141
141142 /**
142143 * Returns list of of child items
143- * @return list of child items or null if item has no child elements
144+ * @return list of child items or null if item has no child elements
144145 */
145- Collection <? extends T > getChildren ();
146-
147-
146+ Collection <? extends T > getChildren ();
147+
148+
148149 /**
149150 * Returns list of of effective child items.
150151 * <p> Effective child items can be a subset of own children (filtering) or grand children ( level is skipped)</p>
151152 * Default should return all own children
152- * @return list of effective child items or null if item has no effective child elements
153+ * @return list of effective child items or null if item has no effective child elements
153154 */
154- Collection <? extends T > getEffectiveChildren ();
155+ Collection <? extends T > getEffectiveChildren ();
155156
156157 /**
157158 * Returns number of effective child items.
158- * @return effective child count
159+ * @return effective child count
159160 */
160- int getEffectiveChildCount ();
161+ int getEffectiveChildCount ();
161162
162163 /**
163164 * Checks if the item has effective children
164- * @return true if item has effective children
165+ * @return true if item has effective children
165166 */
166- boolean hasEffectiveChildren ();
167+ boolean hasEffectiveChildren ();
167168
168169 /**
169- * Adds item to children list
170- * @param item child item to add
170+ * Adds item to children list
171+ * @param item child item to add
171172 */
172173 void addChild (T item );
173174
174-
175+
175176 /**
176- * Returns implementation-dependent string key corresponding to the item
177+ * Returns implementation-dependent string key corresponding to the item
177178 * @param item item to get key from
178179 * @return implementation-dependent key that can be used in functions using key parameter
179180 */
180181 String getItemKey (T item );
181-
182-
182+
183+
183184 /**
184- * Returns the first child item
185- * @return first child item
185+ * Returns the first child item
186+ * @return first child item
186187 */
187188 T getFirstChild ();
188-
189+
189190 /**
190- * Returns implementation-depended string key of the first child
191+ * Returns implementation-depended string key of the first child
191192 * @return key of the very first child
192- * @see #getItemKey(ITreeItem)
193+ * @see #getItemKey(ITreeItem)
193194 */
194- String getFirstChildKey ();
195-
195+ String getFirstChildKey ();
196+
196197
197198 /**
198199 * Searches child collection for the first item corresponding to the given string key
199200 * @param key implementation-dependent string to search for
200201 * @return child item if found, null otherwise
201- * @see #getItemKey(ITreeItem)
202+ * @see #getItemKey(ITreeItem)
202203 */
203204 T getFirstChild (final String key );
204205
205-
206+
206207 /**
207208 * Returns first child's text
208209 * @param key implementation-dependent string to search for
209210 * @return child item's text if found, null otherwise
210- * @see #getFirstChild(ITreeItem)
211+ * @see #getFirstChild(ITreeItem)
211212 * @see #getItemKey(ITreeItem)
212213 */
213214 String getFirstChildText (final String key );
214215
215-
216+
216217 /**
217218 * Searches child collection for the first item corresponding to the given class type
218219 * @param type class type to search
@@ -242,7 +243,7 @@ default <C> C getFirstChildOfType(String key, Class<C> type) {
242243 return type .cast (child );
243244 return null ;
244245 }
245-
246+
246247 /**
247248 * Removes child from the collection
248249 * @param childToRemove child to remove
@@ -253,11 +254,11 @@ default <C> C getFirstChildOfType(String key, Class<C> type) {
253254 * Removes first child corresponding to the given string
254255 * @param key implementation-dependent string to search for
255256 * @return removed child item if existed, null otherwise
256- * @see #getItemKey(ITreeItem)
257+ * @see #getItemKey(ITreeItem)
257258 */
258259 T removeFirstChild (final String key );
259260
260-
261+
261262 /**
262263 * Removes all children corresponding to the given string
263264 * @param key implementation-dependent string to search for
@@ -267,37 +268,67 @@ default <C> C getFirstChildOfType(String key, Class<C> type) {
267268 T removeAllChildren (final String key );
268269
269270 /**
270- * Adds item to children list and removes all other children with the same key
271+ * Adds item to children list and removes all other children with the same key
271272 * @param item item to replace others
272- * @see #getItemKey(ITreeItem)
273+ * @see #getItemKey(ITreeItem)
273274 */
274275 void replaceChild (T item );
275276
276-
277+
277278 /**
278- * Returns first item in the hierarchy that matches given wildcard pattern
279- * @param pattern wildcard string to search for
279+ * Returns first item in the hierarchy that matches given wildcard pattern
280+ * @param pattern wildcard string to search for
280281 * @return first item matching given pattern if found, null otherwise
281282 */
282283 T getFirstItem (final String pattern );
283-
284+
285+
286+ /**
287+ * Returns collection of segments from root to this
288+ * @return ArrayList of items from root to this
289+ */
290+ default ArrayList <T > getHierachyPathList () {
291+ ArrayList <T > segments = new ArrayList <>();
292+ segments .add (getThisItem ());
293+ for (T item = getParent (); item != null ; item = item .getParent ()){
294+ if (item .getParent () != null ) {
295+ segments .add (0 , item );
296+ }
297+ }
298+ return segments ;
299+ }
300+
301+ /**
302+ * Returns array of segments from root to this
303+ * @return array of objects from root to this
304+ */
305+ default Object [] getHierachyPath () { return getHierachyPathList ().toArray ();}
306+
284307
285308 /**
286- * Returns collection of segments from root to this
287- * @return collection of items from root to this
309+ * Returns collection of segments from root to this item as effective hierarchy path
310+ * @return collection of items from root to this
288311 */
289- Object [] getHierachyPath ();
290-
312+ default ArrayList <T > getEffectiveHierachyPathList () {
313+ ArrayList <T > segments = new ArrayList <>();
314+ for (T item = getEffectiveHierarchyItem (); item != null ; item = item .getEffectiveParent ()){
315+ if (item .getParent () != null ) {
316+ segments .add (0 , item );
317+ }
318+ }
319+ return segments ;
320+ }
321+
291322 /**
292- * Returns collection of segments from root to this item as effective hierarchy path
293- * @return collection of items from root to this
323+ * Returns collection of segments from root to this item as effective hierarchy path
324+ * @return array of objects from root to this
294325 */
295- Object [] getEffectiveHierachyPath ();
326+ default Object [] getEffectiveHierachyPath () { return getEffectiveHierachyPathList (). toArray ();}
296327
297328 /**
298329 * Returns array of effective child items as generic Objects
299- * @return array of effective child items or empty array if item has no children
330+ * @return array of effective child items or empty array if item has no children
300331 */
301- Object [] getEffectiveChildArray ();
302-
332+ default Object [] getEffectiveChildArray () { return getEffectiveItem (). getChildArray ();}
333+
303334}
0 commit comments