001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.jxpath.ri.axes;
018    
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    
022    import org.apache.commons.jxpath.BasicNodeSet;
023    import org.apache.commons.jxpath.Pointer;
024    import org.apache.commons.jxpath.ri.EvalContext;
025    import org.apache.commons.jxpath.ri.model.NodePointer;
026    
027    /**
028     * EvalContext that represents a union between other contexts - result
029     * of a union operation like (a | b)
030     *
031     * @author Dmitri Plotnikov
032     * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
033     */
034    public class UnionContext extends NodeSetContext {
035        private EvalContext[] contexts;
036        private boolean prepared;
037    
038        /**
039         * Create a new UnionContext.
040         * @param parentContext parent context
041         * @param contexts child contexts
042         */
043        public UnionContext(EvalContext parentContext, EvalContext[] contexts) {
044            super(parentContext, new BasicNodeSet());
045            this.contexts = contexts;
046        }
047    
048        public int getDocumentOrder() {
049            return contexts.length > 1 ? 1 : super.getDocumentOrder();
050        }
051    
052        public boolean setPosition(int position) {
053            if (!prepared) {
054                prepared = true;
055                BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
056                ArrayList pointers = new ArrayList();
057                for (int i = 0; i < contexts.length; i++) {
058                    EvalContext ctx = (EvalContext) contexts[i];
059                    while (ctx.nextSet()) {
060                        while (ctx.nextNode()) {
061                            NodePointer ptr = ctx.getCurrentNodePointer();
062                            if (!pointers.contains(ptr)) {
063                                pointers.add(ptr);
064                            }
065                        }
066                    }
067                }
068                sortPointers(pointers);
069    
070                for (Iterator it = pointers.iterator(); it.hasNext();) {
071                    nodeSet.add((Pointer) it.next());
072                }
073            }
074            return super.setPosition(position);
075        }
076    }