List of usage examples for org.apache.commons.collections.iterators IteratorChain getIterators
public List getIterators()
From source file:org.apache.jackrabbit.core.state.SessionItemStateManager.java
/** * Returns an iterator over those transient item state instances that are * direct or indirect descendants of the item state with the given * <code>parentId</code>. The transient item state instance with the given * <code>parentId</code> itself (if there is such) not be included. * <p/>/*from w ww .java 2 s.c om*/ * The instances are returned in depth-first tree traversal order. * * @param parentId the id of the common parent of the transient item state * instances to be returned. * @return an iterator over descendant transient item state instances * @throws InvalidItemStateException if any descendant item state has been * deleted externally * @throws RepositoryException if another error occurs */ public Iterator<ItemState> getDescendantTransientItemStates(NodeId parentId) throws InvalidItemStateException, RepositoryException { if (transientStore.isEmpty()) { List<ItemState> empty = Collections.emptyList(); return empty.iterator(); } // build ordered collection of descendant transient states // sorted by decreasing relative depth // use an array of lists to group the descendants by relative depth; // the depth is used as array index List[] la = new List[10]; try { Iterator iter = transientStore.values().iterator(); while (iter.hasNext()) { ItemState state = (ItemState) iter.next(); // determine relative depth: > 0 means it's a descendant int depth; try { depth = hierMgr.getShareRelativeDepth(parentId, state.getId()); } catch (ItemNotFoundException infe) { /** * one of the parents of the specified item has been * removed externally; as we don't know its path, * we can't determine if it is a descendant; * InvalidItemStateException should only be thrown if * a descendant is affected; * => throw InvalidItemStateException for now * todo FIXME */ // unable to determine relative depth, assume that the item // (or any of its ancestors) has been removed externally String msg = state.getId() + ": the item seems to have been removed externally."; log.debug(msg); throw new InvalidItemStateException(msg); } if (depth < 1) { // not a descendant continue; } // ensure capacity if (depth > la.length) { List[] old = la; la = new List[depth + 10]; System.arraycopy(old, 0, la, 0, old.length); } List list = la[depth - 1]; if (list == null) { list = new ArrayList(); la[depth - 1] = list; } list.add(state); } } catch (RepositoryException re) { log.warn("inconsistent hierarchy state", re); } // create an iterator over the collected descendants // in decreasing depth order IteratorChain resultIter = new IteratorChain(); for (int i = la.length - 1; i >= 0; i--) { List list = la[i]; if (list != null) { resultIter.addIterator(list.iterator()); } } /** * if the resulting iterator chain is empty return * EMPTY_LIST.iterator() instead because older versions * of IteratorChain (pre Commons Collections 3.1) * would throw UnsupportedOperationException in this * situation */ if (resultIter.getIterators().isEmpty()) { List<ItemState> empty = Collections.emptyList(); return empty.iterator(); } return resultIter; }
From source file:org.apache.jackrabbit.core.state.SessionItemStateManager.java
/** * Same as <code>{@link #getDescendantTransientItemStates(NodeId)}</code> * except that item state instances in the attic are returned. * * @param parentId the id of the common parent of the transient item state * instances to be returned. * @return an iterator over descendant transient item state instances in the attic */// www . j ava2 s.co m public Iterator<ItemState> getDescendantTransientItemStatesInAttic(NodeId parentId) { if (atticStore.isEmpty()) { List<ItemState> empty = Collections.emptyList(); return empty.iterator(); } // build ordered collection of descendant transient states in attic // sorted by decreasing relative depth // use a special attic-aware hierarchy manager ZombieHierarchyManager zombieHierMgr = new ZombieHierarchyManager(hierMgr, this, getAttic()); // use an array of lists to group the descendants by relative depth; // the depth is used as array index List[] la = new List[10]; try { for (ItemState state : atticStore.values()) { // determine relative depth: > 0 means it's a descendant //int depth = zombieHierMgr.getRelativeDepth(parentId, state.getId()); int depth = zombieHierMgr.getShareRelativeDepth(parentId, state.getId()); if (depth < 1) { // not a descendant continue; } // ensure capacity if (depth > la.length) { List[] old = la; la = new List[depth + 10]; System.arraycopy(old, 0, la, 0, old.length); } List list = la[depth - 1]; if (list == null) { list = new ArrayList(); la[depth - 1] = list; } list.add(state); } } catch (RepositoryException re) { log.warn("inconsistent hierarchy state", re); } // create an iterator over the collected descendants // in decreasing depth order IteratorChain resultIter = new IteratorChain(); for (int i = la.length - 1; i >= 0; i--) { List list = la[i]; if (list != null) { resultIter.addIterator(list.iterator()); } } /** * if the resulting iterator chain is empty return * EMPTY_LIST.iterator() instead because older versions * of IteratorChain (pre Commons Collections 3.1) * would throw UnsupportedOperationException in this * situation */ if (resultIter.getIterators().isEmpty()) { List<ItemState> empty = Collections.emptyList(); return empty.iterator(); } return resultIter; }
From source file:org.apache.jackrabbit.core.state.SessionItemStateManager.java
/** * Returns an iterator over those transient item state instances that are * direct or indirect descendants of the item state with the given * <code>parentId</code>. The transient item state instance with the given * <code>parentId</code> itself (if there is such) not be included. * <p/>/*w ww .j a v a 2 s .com*/ * The instances are returned in depth-first tree traversal order. * * @param parentId the id of the common parent of the transient item state * instances to be returned. * @return an iterator over descendant transient item state instances * @throws InvalidItemStateException if any descendant item state has been * deleted externally * @throws RepositoryException if another error occurs */ public Iterator getDescendantTransientItemStates(NodeId parentId) throws InvalidItemStateException, RepositoryException { if (transientStore.isEmpty()) { return Collections.EMPTY_LIST.iterator(); } // build ordered collection of descendant transient states // sorted by decreasing relative depth // use an array of lists to group the descendants by relative depth; // the depth is used as array index List[] la = new List[10]; try { Iterator iter = transientStore.values().iterator(); while (iter.hasNext()) { ItemState state = (ItemState) iter.next(); // determine relative depth: > 0 means it's a descendant int depth; try { depth = hierMgr.getShareRelativeDepth(parentId, state.getId()); } catch (ItemNotFoundException infe) { /** * one of the parents of the specified item has been * removed externally; as we don't know its path, * we can't determine if it is a descendant; * InvalidItemStateException should only be thrown if * a descendant is affected; * => throw InvalidItemStateException for now * todo FIXME */ // unable to determine relative depth, assume that the item // (or any of its ancestors) has been removed externally String msg = state.getId() + ": the item seems to have been removed externally."; log.debug(msg); throw new InvalidItemStateException(msg); } if (depth < 1) { // TODO SN: move this to HierarchyManager // if state is shareable, it has actually more than one parent if (state.isNode()) { NodeState ns = (NodeState) state; if (ns.isShareable()) { Set sharedSet = ns.getSharedSet(); if (ns.hasOverlayedState()) { sharedSet = ((NodeState) ns.getOverlayedState()).getSharedSet(); } Iterator sharedParentIds = sharedSet.iterator(); while (sharedParentIds.hasNext()) { NodeId sharedParentId = (NodeId) sharedParentIds.next(); int depth2 = hierMgr.getRelativeDepth(parentId, sharedParentId); if (depth2 >= 0) { depth = depth2 + 1; break; } } } } if (depth < 1) { // not a descendant continue; } } // ensure capacity if (depth > la.length) { List[] old = la; la = new List[depth + 10]; System.arraycopy(old, 0, la, 0, old.length); } List list = la[depth - 1]; if (list == null) { list = new ArrayList(); la[depth - 1] = list; } list.add(state); } } catch (RepositoryException re) { log.warn("inconsistent hierarchy state", re); } // create an iterator over the collected descendants // in decreasing depth order IteratorChain resultIter = new IteratorChain(); for (int i = la.length - 1; i >= 0; i--) { List list = la[i]; if (list != null) { resultIter.addIterator(list.iterator()); } } /** * if the resulting iterator chain is empty return * EMPTY_LIST.iterator() instead because older versions * of IteratorChain (pre Commons Collections 3.1) * would throw UnsupportedOperationException in this * situation */ if (resultIter.getIterators().isEmpty()) { return Collections.EMPTY_LIST.iterator(); } return resultIter; }
From source file:org.apache.jackrabbit.core.state.SessionItemStateManager.java
/** * Same as <code>{@link #getDescendantTransientItemStates(NodeId)}</code> * except that item state instances in the attic are returned. * * @param parentId the id of the common parent of the transient item state * instances to be returned. * @return an iterator over descendant transient item state instances in the attic *//*from ww w . j av a2s . co m*/ public Iterator getDescendantTransientItemStatesInAttic(NodeId parentId) { if (atticStore.isEmpty()) { return Collections.EMPTY_LIST.iterator(); } // build ordered collection of descendant transient states in attic // sorted by decreasing relative depth // use a special attic-aware hierarchy manager ZombieHierarchyManager zombieHierMgr = new ZombieHierarchyManager(hierMgr, this, getAttic()); // use an array of lists to group the descendants by relative depth; // the depth is used as array index List[] la = new List[10]; try { Iterator iter = atticStore.values().iterator(); while (iter.hasNext()) { ItemState state = (ItemState) iter.next(); // determine relative depth: > 0 means it's a descendant //int depth = zombieHierMgr.getRelativeDepth(parentId, state.getId()); int depth = zombieHierMgr.getShareRelativeDepth(parentId, state.getId()); if (depth < 1) { // not a descendant continue; } // ensure capacity if (depth > la.length) { List[] old = la; la = new List[depth + 10]; System.arraycopy(old, 0, la, 0, old.length); } List list = la[depth - 1]; if (list == null) { list = new ArrayList(); la[depth - 1] = list; } list.add(state); } } catch (RepositoryException re) { log.warn("inconsistent hierarchy state", re); } // create an iterator over the collected descendants // in decreasing depth order IteratorChain resultIter = new IteratorChain(); for (int i = la.length - 1; i >= 0; i--) { List list = la[i]; if (list != null) { resultIter.addIterator(list.iterator()); } } /** * if the resulting iterator chain is empty return * EMPTY_LIST.iterator() instead because older versions * of IteratorChain (pre Commons Collections 3.1) * would throw UnsupportedOperationException in this * situation */ if (resultIter.getIterators().isEmpty()) { return Collections.EMPTY_LIST.iterator(); } return resultIter; }
From source file:org.apache.jackrabbit.core.state.SessionItemStateManager.java
/** * Returns an iterator over those transient item state instances that are * direct or indirect descendants of the item state with the given * <code>parentId</code>. The transient item state instance with the given * <code>parentId</code> itself (if there is such) will not be included. * <p/>/*ww w . j av a 2 s .c o m*/ * The instances are returned in depth-first tree traversal order. * * @param parentId the id of the common parent of the transient item state * instances to be returned. * @return an iterator over descendant transient item state instances * @throws InvalidItemStateException if any descendant item state has been * deleted externally * @throws RepositoryException if another error occurs */ public Iterator getDescendantTransientItemStates(NodeId parentId) throws InvalidItemStateException, RepositoryException { if (transientStore.isEmpty()) { return Collections.EMPTY_LIST.iterator(); } // build ordered collection of descendant transient states // sorted by decreasing relative depth // use an array of lists to group the descendants by relative depth; // the depth is used as array index List[] la = new List[10]; try { Iterator iter = transientStore.values().iterator(); while (iter.hasNext()) { ItemState state = (ItemState) iter.next(); // determine relative depth: > 0 means it's a descendant int depth; try { depth = hierMgr.getRelativeDepth(parentId, state.getId()); } catch (ItemNotFoundException infe) { /** * one of the parents of the specified item has been * removed externally; as we don't know its path, * we can't determine if it is a descendant; * InvalidItemStateException should only be thrown if * a descendant is affected; * => throw InvalidItemStateException for now * todo FIXME */ // unable to determine relative depth, assume that the item // (or any of its ancestors) has been removed externally String msg = state.getId() + ": the item seems to have been removed externally."; log.debug(msg); throw new InvalidItemStateException(msg); } if (depth < 1) { // TODO SN: move this to HierarchyManager // if state is shareable, it has actually more than one parent if (state.isNode()) { NodeState ns = (NodeState) state; if (ns.isShareable()) { Set sharedSet = ns.getSharedSet(); if (ns.hasOverlayedState()) { sharedSet = ((NodeState) ns.getOverlayedState()).getSharedSet(); } Iterator sharedParentIds = sharedSet.iterator(); while (sharedParentIds.hasNext()) { NodeId sharedParentId = (NodeId) sharedParentIds.next(); int depth2 = hierMgr.getRelativeDepth(parentId, sharedParentId); if (depth2 >= 0) { depth = depth2 + 1; break; } } } } if (depth < 1) { // not a descendant continue; } } // ensure capacity if (depth > la.length) { List[] old = la; la = new List[depth + 10]; System.arraycopy(old, 0, la, 0, old.length); } List list = la[depth - 1]; if (list == null) { list = new ArrayList(); la[depth - 1] = list; } list.add(state); } } catch (RepositoryException re) { log.warn("inconsistent hierarchy state", re); } // create an iterator over the collected descendants // in decreasing depth order IteratorChain resultIter = new IteratorChain(); for (int i = la.length - 1; i >= 0; i--) { List list = la[i]; if (list != null) { resultIter.addIterator(list.iterator()); } } /** * if the resulting iterator chain is empty return * EMPTY_LIST.iterator() instead because older versions * of IteratorChain (pre Commons Collections 3.1) * would throw UnsupportedOperationException in this * situation */ if (resultIter.getIterators().isEmpty()) { return Collections.EMPTY_LIST.iterator(); } return resultIter; }
From source file:org.apache.jackrabbit.core.state.SessionItemStateManager.java
/** * Same as <code>{@link #getDescendantTransientItemStates(NodeId)}</code> * except that item state instances in the attic are returned. * * @param parentId the id of the common parent of the transient item state * instances to be returned. * @return an iterator over descendant transient item state instances in the attic *///from w ww. j av a 2 s . c om public Iterator getDescendantTransientItemStatesInAttic(NodeId parentId) { if (atticStore.isEmpty()) { return Collections.EMPTY_LIST.iterator(); } // build ordered collection of descendant transient states in attic // sorted by decreasing relative depth // use a special attic-aware hierarchy manager ZombieHierarchyManager zombieHierMgr = new ZombieHierarchyManager(hierMgr, this, getAttic()); // use an array of lists to group the descendants by relative depth; // the depth is used as array index List[] la = new List[10]; try { Iterator iter = atticStore.values().iterator(); while (iter.hasNext()) { ItemState state = (ItemState) iter.next(); // determine relative depth: > 0 means it's a descendant int depth = zombieHierMgr.getRelativeDepth(parentId, state.getId()); if (depth < 1) { // not a descendant continue; } // ensure capacity if (depth > la.length) { List[] old = la; la = new List[depth + 10]; System.arraycopy(old, 0, la, 0, old.length); } List list = la[depth - 1]; if (list == null) { list = new ArrayList(); la[depth - 1] = list; } list.add(state); } } catch (RepositoryException re) { log.warn("inconsistent hierarchy state", re); } // create an iterator over the collected descendants // in decreasing depth order IteratorChain resultIter = new IteratorChain(); for (int i = la.length - 1; i >= 0; i--) { List list = la[i]; if (list != null) { resultIter.addIterator(list.iterator()); } } /** * if the resulting iterator chain is empty return * EMPTY_LIST.iterator() instead because older versions * of IteratorChain (pre Commons Collections 3.1) * would throw UnsupportedOperationException in this * situation */ if (resultIter.getIterators().isEmpty()) { return Collections.EMPTY_LIST.iterator(); } return resultIter; }
From source file:org.apache.jackrabbit.core.state.SessionItemStateManager.java
/** * Returns an iterator over those transient item state instances that are * direct or indirect descendants of the item state with the given * <code>parentId</code>. The transient item state instance with the given * <code>parentId</code> itself (if there is such) not be included. * <p/>//from w w w . j ava2s . c o m * The instances are returned in depth-first tree traversal order. * * @param parentId the id of the common parent of the transient item state * instances to be returned. * @return an iterator over descendant transient item state instances * @throws InvalidItemStateException if any descendant item state has been * deleted externally * @throws RepositoryException if another error occurs */ public Iterator getDescendantTransientItemStates(NodeId parentId) throws InvalidItemStateException, RepositoryException { if (transientStore.isEmpty()) { return Collections.EMPTY_LIST.iterator(); } // build ordered collection of descendant transient states // sorted by decreasing relative depth // use an array of lists to group the descendants by relative depth; // the depth is used as array index List[] la = new List[10]; try { Iterator iter = transientStore.values().iterator(); while (iter.hasNext()) { ItemState state = (ItemState) iter.next(); // determine relative depth: > 0 means it's a descendant int depth; try { depth = hierMgr.getShareRelativeDepth(parentId, state.getId()); } catch (ItemNotFoundException infe) { /** * one of the parents of the specified item has been * removed externally; as we don't know its path, * we can't determine if it is a descendant; * InvalidItemStateException should only be thrown if * a descendant is affected; * => throw InvalidItemStateException for now * todo FIXME */ // unable to determine relative depth, assume that the item // (or any of its ancestors) has been removed externally String msg = state.getId() + ": the item seems to have been removed externally."; log.debug(msg); throw new InvalidItemStateException(msg); } if (depth < 1) { // not a descendant continue; } // ensure capacity if (depth > la.length) { List[] old = la; la = new List[depth + 10]; System.arraycopy(old, 0, la, 0, old.length); } List list = la[depth - 1]; if (list == null) { list = new ArrayList(); la[depth - 1] = list; } list.add(state); } } catch (RepositoryException re) { log.warn("inconsistent hierarchy state", re); } // create an iterator over the collected descendants // in decreasing depth order IteratorChain resultIter = new IteratorChain(); for (int i = la.length - 1; i >= 0; i--) { List list = la[i]; if (list != null) { resultIter.addIterator(list.iterator()); } } /** * if the resulting iterator chain is empty return * EMPTY_LIST.iterator() instead because older versions * of IteratorChain (pre Commons Collections 3.1) * would throw UnsupportedOperationException in this * situation */ if (resultIter.getIterators().isEmpty()) { return Collections.EMPTY_LIST.iterator(); } return resultIter; }
From source file:org.openanzo.jdbc.utils.IteratorUtils.java
/** * Makes a best effort to close any closable iterator. Can close iterators nested in IteratorChain, TransformIterator, FilterIterator and CollatingIterator * instances.//from www . j a v a 2s .c o m * * @param iterator * The iterator to close. */ public static void close(Iterator<?> iterator) { if (iterator == null) return; // obvious case if (iterator instanceof ClosableIterator<?>) { ((ClosableIterator<?>) iterator).close(); } // now check for all cases we know of where iterators can be nested.. else if (iterator instanceof IteratorChain) { IteratorChain chain = (IteratorChain) iterator; @SuppressWarnings("unchecked") // marshal from apache commons collections List<Iterator<?>> iterators = chain.getIterators(); for (Iterator<?> iteratorElement : iterators) { close(iteratorElement); } } else if (iterator instanceof TransformIterator) { TransformIterator trans = (TransformIterator) iterator; close(trans.getIterator()); } else if (iterator instanceof FilterIterator) { FilterIterator filter = (FilterIterator) iterator; close(filter.getIterator()); } else if (iterator instanceof CollatingIterator) { CollatingIterator iter = (CollatingIterator) iterator; @SuppressWarnings("unchecked") // marshal from apache commons collections List<Iterator<?>> iterators = iter.getIterators(); for (Iterator<?> iteratorElement : iterators) { close(iteratorElement); } } }