    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
    018package org.apache.commons.jxpath.ri.axes;
    019
    020import java.util.ArrayList;
    021import java.util.Iterator;
    022
    023import org.apache.commons.jxpath.BasicNodeSet;
    024import org.apache.commons.jxpath.ri.EvalContext;
    025import org.apache.commons.jxpath.ri.model.NodePointer;
    026
    027/**
    028 * EvalContext that represents a union between other contexts - result of a union operation like (a | b)
    029 */
    030public class UnionContext extends NodeSetContext {
    031
    032    private final EvalContext[] contexts;
    033    private boolean prepared;
    034
    035    /**
    036     * Constructs a new UnionContext.
    037     *
    038     * @param parentContext parent context
    039     * @param contexts      child contexts
    040     */
    041    public UnionContext(final EvalContext parentContext, final EvalContext[] contexts) {
    042        super(parentContext, new BasicNodeSet());
    043        this.contexts = contexts;
    044    }
    045
    046    @Override
    047    public int getDocumentOrder() {
    048        return contexts.length > 1 ? 1 : super.getDocumentOrder();
    049    }
    050
    051    @Override
    052    public boolean setPosition(final int position) {
    053        if (!prepared) {
    054            prepared = true;
    055            final BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
    056            final ArrayList<NodePointer> pointers = new ArrayList<>();
    057            for (final EvalContext ctx : contexts) {
    058                while (ctx.nextSet()) {
    059                    while (ctx.nextNode()) {
    060                        final NodePointer ptr = ctx.getCurrentNodePointer();
    061                        if (!pointers.contains(ptr)) {
    062                            pointers.add(ptr);
    063                        }
    064                    }
    065                }
    066            }
    067            sortPointers(pointers);
    068            for (final Iterator<NodePointer> it = pointers.iterator(); it.hasNext();) {
    069                nodeSet.add(it.next());
    070            }
    071        }
    072        return super.setPosition(position);
    073    }
    074}