joerghoh commented on code in PR #2182: URL: https://github.com/apache/jackrabbit-oak/pull/2182#discussion_r2012762334
########## oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/OrderedChildnameIterator.java: ########## @@ -0,0 +1,129 @@ +/* + * 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.jackrabbit.oak.plugins.tree.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +/** + * Return the childrenNames in the order defined by the orderedChildren iterator, and merges it + * with the existing children defined by allChildren. + * + * This implementation focuses on being as lazy as possible; especially consuming the + * allChildren iterator can be slow. + */ + +public class OrderedChildnameIterator implements Iterator<String>{ + + final Iterator<String> orderedChildren; + final Iterator<String> allChildren; + + private String nextResult; + + private final List<String> nonOrderedChildren = new ArrayList<>(); + private Iterator<String> nonOrderedChildrenIterator = null; + + public OrderedChildnameIterator (Iterable<String> orderedChildren, Iterable<String> allChildren) { + this.orderedChildren = orderedChildren == null ? Collections.emptyIterator() : orderedChildren.iterator(); + this.allChildren = allChildren.iterator(); + nextResult = getNextElement(); + } + + private String getNextElement() { + if (orderedChildren.hasNext()) { + String elem = getNextOrderedChild(); + if (elem != null) { + return elem; + } + } + // if the flow comes here, all orderedChildren have already been consumed, and the + // nonOrderedChildren list is no longer changed, so it's safe to create the iterator here + if (nonOrderedChildrenIterator == null) { + nonOrderedChildrenIterator = nonOrderedChildren.iterator(); + } + // return all children which have already been read into the nonOrderedChildren list + if (nonOrderedChildrenIterator.hasNext()) { + return nonOrderedChildrenIterator.next(); + } + // return all children which have not been consumed from the allChildren iterator + if (allChildren.hasNext()) { + return allChildren.next(); + } + // all iterators consumed, no children anymore + return null; + } + + /** + * Consume the next element from the orderedChild list and validates that it's actually present + * @return the next ordered child or {code null} if all ordered children have already been returned + */ + @SuppressWarnings("java:S2589") + private String getNextOrderedChild() { + String current = null; + // check that this element is actually present in the allChildren iterable + while (current == null && orderedChildren.hasNext()) { Review Comment: that's a good spot, I even put in an explicit ignore, because I mis-understood it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
