Github user robertgmoss commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/218#discussion_r69440473
--- Diff: core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
---
@@ -655,34 +677,112 @@ public static boolean isDescendant(Entity ancestor,
Entity potentialDescendant)
* @see #descendants(Entity)
* @see Iterables#filter(Iterable, Class)
*/
- public static <T extends Entity> Iterable<T> descendants(Entity root,
Class<T> ofType) {
- return Iterables.filter(descendants(root), ofType);
+ public static <T extends Entity> Iterable<T> descendantsAndSelf(Entity
root, Class<T> ofType) {
+ return Iterables.filter(descendantsAndSelf(root), ofType);
}
/** Returns the entity, its parent, its parent, and so on. */
+ @SuppressWarnings("unused")
+ public static Iterable<Entity> ancestorsAndSelf(final Entity root) {
+ if (false) {
+ // Keeping this unused code, so that anonymous inner class
numbers don't change!
+ return new Iterable<Entity>() {
+ @Override
+ public Iterator<Entity> iterator() {
+ return new Iterator<Entity>() {
+ Entity next = root;
+ @Override
+ public boolean hasNext() {
+ return next!=null;
+ }
+ @Override
+ public Entity next() {
+ Entity result = next;
+ next = next.getParent();
+ return result;
+ }
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ };
+ }
+ Set<Entity> result = Sets.newLinkedHashSet();
+ Entity current = root;
+ while (current != null) {
+ result.add(current);
+ current = current.getParent();
+ }
+ return result;
+ }
+
+ /** Returns the entity's parent, its parent's parent, and so on. */
+ public static Iterable<Entity> ancestorsWithoutSelf(Entity root) {
+ Set<Entity> result = Sets.newLinkedHashSet();
+ Entity parent = (root != null) ? root.getParent() : null;
+ while (parent != null) {
+ result.add(parent);
+ parent = parent.getParent();
+ }
+ return result;
+ }
+
+ /**
+ * Side-effects {@code result} to return descendants (not including
{@code root}).
+ */
+ private static void descendantsWithoutSelf(Entity root,
Collection<Entity> result) {
+ Stack<Entity> tovisit = new Stack<Entity>();
+ tovisit.add(root);
+
+ while (!tovisit.isEmpty()) {
+ Entity e = tovisit.pop();
+ result.addAll(e.getChildren());
+ tovisit.addAll(e.getChildren());
+ }
+ }
+
+
+
+
+
+
+
+
--- End diff --
excessive whitespace
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---