Repository: wicket Updated Branches: refs/heads/master bad3aa708 -> 62e6be756
Remove deprecated component iterators Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/3cc60fff Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/3cc60fff Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/3cc60fff Branch: refs/heads/master Commit: 3cc60fff83e28cfc6b5c39543986ddce17157f3f Parents: bad3aa7 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Sun Mar 13 19:49:07 2016 +0100 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Sun Mar 13 19:49:07 2016 +0100 ---------------------------------------------------------------------- .../iterator/AbstractHierarchyIterator.java | 466 -------------- .../AbstractHierarchyIteratorWithFilter.java | 215 ------- .../iterator/ComponentHierarchyIterator.java | 246 ------- .../GenericComponentHierarchyIterator.java | 208 ------ .../wicket/util/iterator/IteratorFilter.java | 34 - .../iterator/AbstractHierarchyIteratorTest.java | 339 ---------- .../util/iterator/ComponentIteratorTest.java | 636 ------------------- 7 files changed, 2144 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/3cc60fff/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIterator.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIterator.java b/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIterator.java deleted file mode 100644 index fd8be61..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIterator.java +++ /dev/null @@ -1,466 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.util.iterator; - -import java.util.Iterator; - -import org.apache.wicket.util.collections.ArrayListStack; -import org.apache.wicket.util.lang.Args; - -/** - * This is a basic iterator for hierarchical structures such as Component hierarchies or HTML - * markup. It supports child first and parent first traversal and intercepts while moving down or up - * the hierarchy. - * <p> - * It assumes the container class implements <code>Iterable</code>. The leaf nodes don't need to. - * <p> - * Consecutive calls to <code>hasNext()</code> without <code>next()</code> in between, don't move - * the cursor, but return the same value until <code>next()</code> is called. - * <p> - * Every call to <code>next()</code>, with or without <code>hasNext()</code>, will move the cursor - * to the next element. - * - * @TODO Replace ChildFirst with a strategy - * - * @author Juergen Donnerstag - * @param <I> - * The type relevant for the iterator. What you expect to get back from next(), e.g. - * Form. - * @param <N> - * The base type of all nodes, e.g. Component - * @deprecated Hierarchy iterators are deprecated because they have problems with pages with - * deep component tree. Use {@link org.apache.wicket.util.visit.IVisitor} instead. - * @see org.apache.wicket.MarkupContainer#visitChildren(org.apache.wicket.util.visit.IVisitor) - * @see org.apache.wicket.MarkupContainer#visitChildren(Class, org.apache.wicket.util.visit.IVisitor) - */ -@Deprecated -public abstract class AbstractHierarchyIterator<N, I extends N> implements Iterator<I>, Iterable<I> -{ - // An iterator for each level we are down from root - private ArrayListStack<LevelIterator<N>> stack = new ArrayListStack<>(); - - // The current level iterator - private LevelIterator<N> data; - - // Whether we need to traverse into the next level with the next invocation of hasNext() - private boolean traverse; - - // An easy way to configure child or parent first iterations - private boolean childFirst; - - // If remaining siblings shall be ignored - private boolean skipRemainingSiblings; - - // True, if hasNext() was called last or next() - private boolean hasNextWasLast; - - /** - * Construct. - * - * @param root - */ - public AbstractHierarchyIterator(final N root) - { - Args.notNull(root, "root"); - - if (hasChildren(root)) - { - data = new LevelIterator<>(root, newIterator(root)); - } - } - - /** - * - * @param childFirst - * If true, than children are visited before their parent is. - */ - public final void setChildFirst(final boolean childFirst) - { - this.childFirst = childFirst; - } - - /** - * - * @param node - * @return True, if node is a container and has at least one child. - */ - abstract protected boolean hasChildren(final N node); - - /** - * If node is a container than return an iterator for its children. - * <p> - * Gets only called if {@link #hasChildren(Object)} return true. - * - * @param node - * @return container iterator - */ - abstract protected Iterator<N> newIterator(final N node); - - @Override - public boolean hasNext() - { - // Did we reach the end already? - if (data == null) - { - return false; - } - - // We did not yet reach the end. Has next() been called already? - if (hasNextWasLast == true) - { - // next() has not yet been called. - return true; - } - - // Remember that the last call was a hasNext() - hasNextWasLast = true; - - // - if (skipRemainingSiblings == true) - { - skipRemainingSiblings = false; - return moveUp(); - } - - // Do we need to traverse into the next level? - if (!childFirst && traverse) - { - // Try to find the next element - if (moveDown(data.lastNode) == false) - { - // No more elements - return false; - } - - // Found the next element in one the next levels - hasNextWasLast = true; - return true; - } - - // Get the next node on the current level. If it's a container, than move downwards. If - // there are no more elements, than move up again. - return nextNode(); - } - - /** - * Move down into the next level - * - * @param node - * @return False if no more elements were found - */ - private boolean moveDown(final N node) - { - // Remember all details of the current level - stack.push(data); - - // Initialize the data for the next level - data = new LevelIterator<>(node, newIterator(node)); - - // Get the next node on the current level. If it's a container, than move downwards. If - // there are no more elements, than move up again. - return nextNode(); - } - - /** - * Gets called for each element within the hierarchy (nodes and leafs) - * - * @param node - * @return if false, than skip (filter) the element. It'll not stop the iterator from traversing - * into children though. - */ - protected boolean onFilter(final N node) - { - return true; - } - - /** - * Gets called for each element where {@link #hasChildren(Object)} return true. - * - * @param node - * @return if false, than do not traverse into the children and grand-children. - */ - protected boolean onTraversalFilter(final N node) - { - return true; - } - - /** - * Get the next node from the underlying iterator and handle it. - * - * @return true, if one more element was found - */ - private boolean nextNode() - { - // Get the next element - while (data.hasNext()) - { - data.lastNode = data.next(); - - // Does it have children? - traverse = hasChildren(data.lastNode); - if (traverse) - { - traverse = onTraversalFilter(data.lastNode); - } - - // If it does and we do childFirst, than try to find the next child - if (childFirst && traverse) - { - if (moveDown(data.lastNode) == false) - { - // No more elements - return false; - } - } - - // The user interested in the node? - if (onFilter(data.lastNode)) - { - // Yes - return true; - } - - // If we are parent first but the user is not interested in the current node, than move - // down. - if (!childFirst && traverse) - { - if (moveDown(data.lastNode) == false) - { - // No more elements - return false; - } - - if (data == null) - { - return false; - } - - hasNextWasLast = true; - return true; - } - - if (skipRemainingSiblings == true) - { - skipRemainingSiblings = false; - break; - } - } - - // Nothing found. Move up and try to find the next element there - return moveUp(); - } - - /** - * Move up until we found the next element - * - * @return false, if no more elements are found - */ - private boolean moveUp() - { - if (data == null) - { - return false; - } - - // Are we back at the root node? - if (stack.isEmpty()) - { - data = null; - return false; - } - - // Move up one level - data = stack.pop(); - - // If we are on childFirst, then it's now time to handle the parent - if (childFirst) - { - hasNextWasLast = true; - if (onFilter(data.lastNode) == true) - { - return true; - } - return nextNode(); - } - // If we are on parent first, then get the next element - else if (data.hasNext()) - { - return nextNode(); - } - else - { - // No more elements on the current level. Move up. - return moveUp(); - } - } - - /** - * Traverse the hierarchy and get the next element - */ - @Override - @SuppressWarnings("unchecked") - public I next() - { - // Did we reach the end already? - if (data == null) - { - return null; - } - - // hasNext() is responsible to get the next element - if (hasNextWasLast == false) - { - if (hasNext() == false) - { - // No more elements - return null; - } - } - - // Remember that we need to call hasNext() to get the next element - hasNextWasLast = false; - - return (I)data.lastNode; - } - - @Override - public void remove() - { - if (data == null) - { - throw new IllegalStateException("Already reached the end of the iterator."); - } - - data.remove(); - } - - /** - * Skip all remaining siblings and return to the parent node. - * <p> - * Can as well be called within {@link IteratorFilter#onFilter(Object)}. - */ - public void skipRemainingSiblings() - { - skipRemainingSiblings = true; - traverse = false; - } - - /** - * Assuming we are currently at a container, than ignore all its children and grand-children and - * continue with the next sibling. - * <p> - * Can as well be called within {@link IteratorFilter#onFilter(Object)}. - */ - public void dontGoDeeper() - { - traverse = false; - } - - @Override - public final Iterator<I> iterator() - { - return this; - } - - @Override - public String toString() - { - StringBuilder msg = new StringBuilder(500); - msg.append("traverse=") - .append(traverse) - .append("; childFirst=") - .append(childFirst) - .append("; hasNextWasLast=") - .append(hasNextWasLast) - .append('\n'); - - msg.append("data.node=") - .append(data.node) - .append('\n') - .append("data.lastNode=") - .append(data.lastNode) - .append('\n'); - - msg.append("stack.size=").append(stack.size()); - - return msg.toString(); - } - - /** - * We need a little helper to store the iterator of the level and the last node returned by - * next(). - * - * @param <N> - */ - private static class LevelIterator<N> implements Iterator<N> - { - private final N node; - - private final Iterator<N> iter; - - private N lastNode; - - /** - * Construct. - * - * @param node - * For debugging purposes only - * @param iter - * The iterator for 'node' - */ - public LevelIterator(final N node, final Iterator<N> iter) - { - Args.notNull(iter, "iter"); - - this.node = node; - this.iter = iter; - } - - @Override - public boolean hasNext() - { - return iter.hasNext(); - } - - @Override - public N next() - { - lastNode = iter.next(); - return lastNode; - } - - @Override - public void remove() - { - iter.remove(); - } - - @Override - public String toString() - { - StringBuilder msg = new StringBuilder(500); - msg.append("node=") - .append(node) - .append('\n') - .append("lastNode=") - .append(lastNode) - .append('\n'); - - return msg.toString(); - } - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/3cc60fff/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorWithFilter.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorWithFilter.java b/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorWithFilter.java deleted file mode 100644 index 25a4c81..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorWithFilter.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.util.iterator; - -import java.util.Collection; -import java.util.List; - -import org.apache.wicket.util.lang.Args; -import org.apache.wicket.util.lang.Generics; - -/** - * Extend {@link AbstractHierarchyIterator} and add support for filters. - * - * @author Juergen Donnerstag - * @param <N> - * @param <I> - * @deprecated Hierarchy iterators are deprecated because they have problems with pages with - * deep component tree. Use {@link org.apache.wicket.util.visit.IVisitor} instead. - * @see org.apache.wicket.MarkupContainer#visitChildren(org.apache.wicket.util.visit.IVisitor) - * @see org.apache.wicket.MarkupContainer#visitChildren(Class, org.apache.wicket.util.visit.IVisitor) - */ -@Deprecated -public abstract class AbstractHierarchyIteratorWithFilter<N, I extends N> extends - AbstractHierarchyIterator<N, I> -{ - // The list of user provided filters - private List<IteratorFilter<N>> filters; - - // List of traversal filters - private List<IteratorFilter<N>> traverseFilter; - - /** - * Construct. - * - * @param root - */ - public AbstractHierarchyIteratorWithFilter(final N root) - { - super(root); - } - - /** - * Apply all registered filters - * - * @param node - * @return False, to filter the component. True, to continue processing the component. - */ - @Override - protected final boolean onFilter(final N node) - { - if (filters != null) - { - for (IteratorFilter<N> filter : filters) - { - if (filter.onFilter(node) == false) - { - return false; - } - } - } - return true; - } - - /** - * @return Gets the List of all registered filters. A new list will be created if no filter has - * been registered yet (never return null). - */ - public final List<IteratorFilter<N>> getFilters() - { - if (filters == null) - { - filters = Generics.newArrayList(); - } - - return filters; - } - - /** - * Add a filter (fluent API) - * - * @param filter - * @return this - */ - public AbstractHierarchyIteratorWithFilter<N, I> addFilter(final IteratorFilter<N> filter) - { - Args.notNull(filter, "filter"); - - getFilters().add(filter); - return this; - } - - /** - * Replace the current set of filters. Sometimes you need to first find X to than start - * searching for Y. - * - * @param filters - * New filter set. May be null to remove all filters. - * @return Old filter set. Null, if no filter was registered. - */ - public Collection<IteratorFilter<N>> replaceFilterSet( - final Collection<IteratorFilter<N>> filters) - { - List<IteratorFilter<N>> old = this.filters; - - this.filters = null; - if ((filters != null) && !filters.isEmpty()) - { - for (IteratorFilter<N> filter : filters) - { - addFilter(filter); - } - } - - return old; - } - - /** - * @param throwException - * If true, an exception is thrown if no matching element was found. - * @return Find the the first element matching all filters - */ - public final I getFirst(final boolean throwException) - { - if (hasNext()) - { - return next(); - } - - if (throwException) - { - throw new IllegalStateException("Iterator did not match any component"); - } - - return null; - } - - /** - * - * @return Gets all elements matching the filters in a list - */ - public final List<I> toList() - { - List<I> list = Generics.newArrayList(); - for (I component : this) - { - list.add(component); - } - - return list; - } - - /** - * @return Gets the List of all registered traversal filters. A new list will be created if no - * traversal filter has been registered yet (never return null). - */ - public final List<IteratorFilter<N>> getTraverseFilters() - { - if (traverseFilter == null) - { - traverseFilter = Generics.newArrayList(); - } - - return traverseFilter; - } - - /** - * Add a filter to the traversal filter list (fluent API). - * - * @param filter - * @return this - */ - public AbstractHierarchyIteratorWithFilter<N, I> addTraverseFilters( - final IteratorFilter<N> filter) - { - getTraverseFilters().add(filter); - return this; - } - - /** - * Apply all registered traversal filters - * - * @param node - * @return False, to filter the element. True, to continue processing the component. - */ - @Override - protected boolean onTraversalFilter(final N node) - { - if (traverseFilter != null) - { - for (IteratorFilter<N> filter : traverseFilter) - { - if (filter.onFilter(node) == false) - { - return false; - } - } - } - - return true; - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/3cc60fff/wicket-core/src/main/java/org/apache/wicket/util/iterator/ComponentHierarchyIterator.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/iterator/ComponentHierarchyIterator.java b/wicket-core/src/main/java/org/apache/wicket/util/iterator/ComponentHierarchyIterator.java deleted file mode 100644 index 66a60ba..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/util/iterator/ComponentHierarchyIterator.java +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.util.iterator; - -import java.util.Iterator; - -import org.apache.wicket.Component; -import org.apache.wicket.MarkupContainer; -import org.apache.wicket.util.lang.Args; - -/** - * Iterator over the complete Component hierarchy. The implementation is parent first, meaning that - * first the parent gets returned upon next() and only than it's children. - * <p> - * A fluent or builder type of API is provided to configure the iterator with filters. - * - * @author Juergen Donnerstag - * @deprecated Hierarchy iterators are deprecated because they have problems with pages with - * deep component tree. Use {@link org.apache.wicket.util.visit.IVisitor} instead. - * @see org.apache.wicket.MarkupContainer#visitChildren(org.apache.wicket.util.visit.IVisitor) - * @see org.apache.wicket.MarkupContainer#visitChildren(Class, org.apache.wicket.util.visit.IVisitor) - */ -@Deprecated -public class ComponentHierarchyIterator extends - AbstractHierarchyIteratorWithFilter<Component, Component> -{ - /** - * Construct. - * - * @param component - * Iterate over the containers children - */ - public ComponentHierarchyIterator(final Component component) - { - super(component); - } - - /** - * Convenience Constructor - * - * @param component - * Iterate over the containers children - * @param clazz - * Add filter by class - * @param visible - * Add filter by visibility - * @param enabled - * Add filter by "enabled" - */ - public ComponentHierarchyIterator(final Component component, Class<?> clazz, boolean visible, - boolean enabled) - { - this(component); - - if (clazz != null) - { - filterByClass(clazz); - } - - if (visible) - { - filterByVisibility(); - } - - if (enabled) - { - filterEnabled(); - } - } - - /** - * Convenience Constructor - * - * @param component - * Iterate over the containers children - * @param clazz - * Add filter by class - */ - public ComponentHierarchyIterator(final Component component, Class<?> clazz) - { - this(component, clazz, false, false); - } - - /** - * The component must be a MarkupContainer to contain children - */ - @Override - protected Iterator<Component> newIterator(final Component node) - { - return ((MarkupContainer)node).iterator(); - } - - /** - * Only MarkupContainer's might have children - */ - @Override - protected boolean hasChildren(Component elem) - { - if (elem instanceof MarkupContainer) - { - return ((MarkupContainer)elem).size() > 0; - } - return false; - } - - /** - * Add a filter which returns only leaf components. - * - * @return this - */ - public final ComponentHierarchyIterator filterLeavesOnly() - { - getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(final Component component) - { - if (component instanceof MarkupContainer) - { - return ((MarkupContainer)component).size() == 0; - } - return true; - } - }); - - return this; - } - - /** - * Ignore components which don't implement (instanceof) the class provided. - * - * @param clazz - * @return this - */ - public ComponentHierarchyIterator filterByClass(final Class<?> clazz) - { - if (clazz != null) - { - getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component component) - { - return clazz.isInstance(component); - } - }); - } - - return this; - } - - /** - * Ignore all Components which not visible. - * - * @return this - */ - public ComponentHierarchyIterator filterByVisibility() - { - IteratorFilter<Component> filter = new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component comp) - { - return comp.isVisibleInHierarchy(); - } - }; - - addFilter(filter); - addTraverseFilters(filter); - - return this; - } - - /** - * Ignore all Components which not enabled (disabled) in the hierarchy - * - * @return this - */ - public ComponentHierarchyIterator filterEnabled() - { - IteratorFilter<Component> filter = new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component comp) - { - return comp.isEnabledInHierarchy(); - } - }; - - addFilter(filter); - addTraverseFilters(filter); - - return this; - } - - /** - * Ignore all components which don't match the id (regex). - * - * @param match - * Regex to find Components matching - * @return this - */ - public ComponentHierarchyIterator filterById(final String match) - { - Args.notEmpty(match, "match"); - - getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component comp) - { - return comp.getId().matches(match); - } - }); - - return this; - } - - @Override - public ComponentHierarchyIterator addFilter(final IteratorFilter<Component> filter) - { - super.addFilter(filter); - return this; - } - - @Override - public ComponentHierarchyIterator addTraverseFilters(IteratorFilter<Component> filter) - { - super.addTraverseFilters(filter); - return this; - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/3cc60fff/wicket-core/src/main/java/org/apache/wicket/util/iterator/GenericComponentHierarchyIterator.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/iterator/GenericComponentHierarchyIterator.java b/wicket-core/src/main/java/org/apache/wicket/util/iterator/GenericComponentHierarchyIterator.java deleted file mode 100644 index 2f623d8..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/util/iterator/GenericComponentHierarchyIterator.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.util.iterator; - -import java.util.Iterator; - -import org.apache.wicket.Component; -import org.apache.wicket.MarkupContainer; -import org.apache.wicket.util.lang.Args; - -/** - * Iterator over the complete Component hierarchy. The implementation is parent first, meaning that - * first the parent gets returned upon next() and only than it's children. - * <p> - * A fluent or builder type of API is provided to configure the iterator with filters. - * - * @author Juergen Donnerstag - * @param <I> - * The type which next() should return (the iterator type) - * @deprecated Hierarchy iterators are deprecated because they have problems with pages with - * deep component tree. Use {@link org.apache.wicket.util.visit.IVisitor} instead. - * @see org.apache.wicket.MarkupContainer#visitChildren(org.apache.wicket.util.visit.IVisitor) - * @see org.apache.wicket.MarkupContainer#visitChildren(Class, org.apache.wicket.util.visit.IVisitor) - */ -@Deprecated -public class GenericComponentHierarchyIterator<I extends Component> extends - AbstractHierarchyIteratorWithFilter<Component, I> -{ - /** - * Construct. - * - * @param component - * Iterate over the containers children - * @param clazz - * Must be the same as the iterator type provided - */ - public GenericComponentHierarchyIterator(final Component component, - final Class<? extends I> clazz) - { - super(component); - - Args.notNull(clazz, "clazz"); - filterByClass(clazz); - } - - /** - * The component must be a MarkupContainer to contain children - */ - @Override - protected Iterator<Component> newIterator(final Component node) - { - return ((MarkupContainer)node).iterator(); - } - - /** - * Only MarkupContainer's might have children - */ - @Override - protected boolean hasChildren(Component elem) - { - if (elem instanceof MarkupContainer) - { - return ((MarkupContainer)elem).size() > 0; - } - return false; - } - - /** - * Add a filter which returns only leaf components. - * - * @return this - */ - public final GenericComponentHierarchyIterator<I> filterLeavesOnly() - { - getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(final Component component) - { - if (component instanceof MarkupContainer) - { - return ((MarkupContainer)component).size() == 0; - } - return true; - } - }); - - return this; - } - - /** - * Ignore components which don't implement (instanceof) the class provided. - * - * @param clazz - * @return this - */ - public GenericComponentHierarchyIterator<I> filterByClass(final Class<?> clazz) - { - if (clazz != null) - { - getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component component) - { - return clazz.isInstance(component); - } - }); - } - - return this; - } - - /** - * Ignore all Components which not visible. - * - * @return this - */ - public GenericComponentHierarchyIterator<I> filterByVisibility() - { - IteratorFilter<Component> filter = new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component comp) - { - return comp.isVisibleInHierarchy(); - } - }; - - addFilter(filter); - addTraverseFilters(filter); - - return this; - } - - /** - * Ignore all Components which not enabled (disabled) in the hierarchy - * - * @return this - */ - public GenericComponentHierarchyIterator<I> filterEnabled() - { - IteratorFilter<Component> filter = new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component comp) - { - return comp.isEnabledInHierarchy(); - } - }; - - addFilter(filter); - addTraverseFilters(filter); - - return this; - } - - /** - * Ignore all components which don't match the id (regex). - * - * @param match - * Regex to find Components matching - * @return this - */ - public GenericComponentHierarchyIterator<I> filterById(final String match) - { - Args.notEmpty(match, "match"); - - getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component comp) - { - return comp.getId().matches(match); - } - }); - - return this; - } - - @Override - public GenericComponentHierarchyIterator<I> addFilter(final IteratorFilter<Component> filter) - { - super.addFilter(filter); - return this; - } - - @Override - public GenericComponentHierarchyIterator<I> addTraverseFilters(IteratorFilter<Component> filter) - { - super.addTraverseFilters(filter); - return this; - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/3cc60fff/wicket-core/src/main/java/org/apache/wicket/util/iterator/IteratorFilter.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/iterator/IteratorFilter.java b/wicket-core/src/main/java/org/apache/wicket/util/iterator/IteratorFilter.java deleted file mode 100644 index 1037f73..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/util/iterator/IteratorFilter.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.util.iterator; - - -/** - * A simple filter interface for the hierarchy iterator. - * - * @author Juergen Donnerstag - * @param <S> - */ -public abstract class IteratorFilter<S> -{ - /** - * - * @param node - * @return false, to filter the component. True, to continue processing the node. - */ - abstract protected boolean onFilter(S node); -} http://git-wip-us.apache.org/repos/asf/wicket/blob/3cc60fff/wicket-core/src/test/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorTest.java b/wicket-core/src/test/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorTest.java deleted file mode 100644 index dd31779..0000000 --- a/wicket-core/src/test/java/org/apache/wicket/util/iterator/AbstractHierarchyIteratorTest.java +++ /dev/null @@ -1,339 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.util.iterator; - -import org.apache.wicket.Component; -import org.apache.wicket.Page; -import org.apache.wicket.markup.html.WebComponent; -import org.apache.wicket.markup.html.WebMarkupContainer; -import org.apache.wicket.markup.html.WebPage; -import org.apache.wicket.util.tester.WicketTestCase; -import org.junit.Test; - -/** - * - */ -public class AbstractHierarchyIteratorTest extends WicketTestCase -{ - /** */ - @Test(expected = IllegalArgumentException.class) - public void nullParent() - { - new ComponentHierarchyIterator(null); - } - - /** */ - @Test - public void emptyParent() - { - Page page = new MyPage(); - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void withComponent() - { - WebComponent comp = new WebComponent("id"); - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(comp); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void multipleRemoves() - { - } - - /** */ - @Test - public void multipleHasNext() - { - } - - /** */ - @Test - public void multipleNext() - { - } - - /** */ - @Test - public void parentWithSomeClients() - { - Page page = new MyPage(); - page.add(new WebComponent("1")); - page.add(new WebMarkupContainer("2")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertTrue(iter.hasNext()); - assertEquals("1", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("2", iter.next().getId()); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void simpleHierachy() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b21; - b2.add(b21 = new WebMarkupContainer("b21")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b21", iter.next().getId()); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void simpleHierachyDifferentOrder() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b12", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b121", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void skip() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - iter.skipRemainingSiblings(); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void skip2() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - WebComponent c; - page.add(c = new WebComponent("c")); - - // Filter leaf components only - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - iter.skipRemainingSiblings(); - assertTrue(iter.hasNext()); - assertEquals("c", iter.next().getId()); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void foreach() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - int count = 0; - String buf = ""; - for (Component component : new ComponentHierarchyIterator(page)) - { - count += 1; - buf += component.getId(); - } - - assertEquals(6, count); - assertEquals("abb1b12b121b2", buf); - } - - /** */ - @Test - public void foreachDontGoDeeper() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - int count = 0; - String buf = ""; - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - while (iter.hasNext()) - { - Component component = iter.next(); - count += 1; - buf += component.getId(); - if ("b1".equals(component.getId())) - { - iter.dontGoDeeper(); - } - } - - assertEquals(4, count); - assertEquals("abb1b2", buf); - } - - /** */ - @Test - public void childFirst() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - int count = 0; - StringBuilder buf = new StringBuilder(); - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - iter.setChildFirst(true); - while (iter.hasNext()) - { - Component component = iter.next(); - count += 1; - if (buf.length() > 0) - { - buf.append(Component.PATH_SEPARATOR); - } - buf.append(component.getId()); - } - - assertEquals(6, count); - assertEquals("a:b121:b12:b1:b2:b", buf.toString()); - } - - /** */ - public static class MyPage extends WebPage - { - private static final long serialVersionUID = 1L; - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/3cc60fff/wicket-core/src/test/java/org/apache/wicket/util/iterator/ComponentIteratorTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/util/iterator/ComponentIteratorTest.java b/wicket-core/src/test/java/org/apache/wicket/util/iterator/ComponentIteratorTest.java deleted file mode 100644 index cafd396..0000000 --- a/wicket-core/src/test/java/org/apache/wicket/util/iterator/ComponentIteratorTest.java +++ /dev/null @@ -1,636 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.util.iterator; - -import org.apache.wicket.Component; -import org.apache.wicket.MarkupContainer; -import org.apache.wicket.Page; -import org.apache.wicket.markup.html.WebComponent; -import org.apache.wicket.markup.html.WebMarkupContainer; -import org.apache.wicket.markup.html.WebPage; -import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.util.tester.WicketTestCase; -import org.junit.Test; - -/** - * - */ -public class ComponentIteratorTest extends WicketTestCase -{ - /** */ - public static class MyPage extends WebPage - { - private static final long serialVersionUID = 1L; - } - - /** */ - @Test(expected = IllegalArgumentException.class) - public void nullParent() - { - new ComponentHierarchyIterator(null); - } - - /** */ - @Test - public void emptyParent() - { - Page page = new MyPage(); - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void withComponent() - { - WebComponent comp = new WebComponent("id"); - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(comp); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void multipleRemoves() - { - } - - /** */ - @Test - public void multipleHasNext() - { - } - - /** */ - @Test - public void multipleNext() - { - } - - /** */ - @Test - public void parentWithSomeClients() - { - Page page = new MyPage(); - page.add(new WebComponent("1")); - page.add(new WebMarkupContainer("2")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertTrue(iter.hasNext()); - assertEquals("1", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("2", iter.next().getId()); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void parentWithSomeClientsAndSimpleClassFilterFluent() - { - Page page = new MyPage(); - page.add(new WebComponent("1")); - page.add(new WebMarkupContainer("2")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page).filterByClass(MarkupContainer.class); - assertTrue(iter.hasNext()); - assertEquals("2", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void checkIsVisibleFluent() - { - Page page = new MyPage(); - page.add(new WebComponent("1")); - page.add(new WebMarkupContainer("2").setVisible(false)); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page).filterByVisibility(); - assertTrue(iter.hasNext()); - assertEquals("1", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void userFilter() - { - Page page = new MyPage(); - page.add(new WebComponent("a")); - page.add(new WebMarkupContainer("2").setVisible(false)); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page).addFilter(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component comp) - { - return comp.getId().startsWith("a"); - } - }); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void simpleHierachy() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b21; - b2.add(b21 = new WebMarkupContainer("b21")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b21", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void simpleHierachyDifferentOrder() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b12", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b121", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void simpleHierachyWithFilter() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - iter.addFilter(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component component) - { - return component.getId().endsWith("2"); - } - }); - - assertTrue(iter.hasNext()); - assertEquals("b12", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void simpleHierachyWithFilterAndNoMatch() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - iter.addFilter(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component component) - { - return component.getId().endsWith("x"); - } - }); - - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void simpleHierachyWithLeafFilter() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page).filterLeavesOnly(); - iter.getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component component) - { - return component.getId().endsWith("2"); - } - }); - - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void traversalFilter() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - b1.setVisible(false); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page).filterByVisibility(); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void traversalFilterChain() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - b1.setVisible(false); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page).filterLeavesOnly() - .filterByVisibility(); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void skip() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - final ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - iter.getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component component) - { - if ("b1".equals(component.getId())) - { - iter.skipRemainingSiblings(); - // fall through and return true => available via next() - } - return true; - } - }); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void skip2() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - final ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - iter.getFilters().add(new IteratorFilter<Component>() - { - @Override - protected boolean onFilter(Component component) - { - if ("b1".equals(component.getId())) - { - iter.skipRemainingSiblings(); - // return false => not available via next() - return false; - } - return true; - } - }); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void skip3() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - - assertTrue(iter.hasNext()); - assertEquals("a", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - iter.skipRemainingSiblings(); - assertFalse(iter.hasNext()); - } - - /** */ - @Test - public void foreach() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - int count = 0; - String buf = ""; - for (Component component : new ComponentHierarchyIterator(page)) - { - count += 1; - buf += component.getId(); - } - - assertEquals(6, count); - assertEquals("abb1b12b121b2", buf); - } - - /** */ - @Test - public void foreachDontGoDeeper() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebMarkupContainer b121; - b12.add(b121 = new WebMarkupContainer("b121")); - - // Filter leaf components only - int count = 0; - String buf = ""; - ComponentHierarchyIterator iter = new ComponentHierarchyIterator(page); - while (iter.hasNext()) - { - Component component = iter.next(); - count += 1; - buf += component.getId(); - if ("b1".equals(component.getId())) - { - iter.dontGoDeeper(); - } - } - - assertEquals(4, count); - assertEquals("abb1b2", buf); - } - - /** */ - @Test - public void classFilter() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebComponent b121; - b12.add(b121 = new WebComponent("b121")); - - GenericComponentHierarchyIterator<MarkupContainer> iter = new GenericComponentHierarchyIterator<MarkupContainer>( - page, MarkupContainer.class); - - assertTrue(iter.hasNext()); - assertEquals("b", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b1", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b12", iter.next().getId()); - assertTrue(iter.hasNext()); - assertEquals("b2", iter.next().getId()); - assertFalse(iter.hasNext()); - assertNull(iter.next()); - } - - /** */ - @Test - public void classFilterWithForeach() - { - Page page = new MyPage(); - WebComponent a; - page.add(a = new WebComponent("a")); - WebMarkupContainer b; - page.add(b = new WebMarkupContainer("b")); - WebMarkupContainer b1; - b.add(b1 = new WebMarkupContainer("b1")); - WebMarkupContainer b2; - b.add(b2 = new WebMarkupContainer("b2")); - WebMarkupContainer b12; - b1.add(b12 = new WebMarkupContainer("b12")); - WebComponent b121; - b12.add(b121 = new WebComponent("b121")); - - String buf = ""; - for (MarkupContainer container : new GenericComponentHierarchyIterator<MarkupContainer>( - page, MarkupContainer.class)) - { - buf += container.getId(); - } - - assertEquals("bb1b12b2", buf); - } - - /** */ - @Test - public void classFilterWrongClass() - { - Page page = new MyPage(); - - // This is ok. It'll return only Form components, though the next() return type is - // MarkupContainer - new GenericComponentHierarchyIterator<MarkupContainer>(page, Form.class); - - // Compile Time error - // new GenericComponentHierarchyIterator<Form>(page, MarkupContainer.class); - } -}
