1 /** 2 * Copyright (c) 2011, University of Konstanz, Distributed Systems Group 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of the University of Konstanz nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 package org.treetank.service.xml.xpath.axis; 29 30 import java.util.HashSet; 31 import java.util.Set; 32 33 import org.treetank.api.INodeReadTrx; 34 import org.treetank.axis.AbsAxis; 35 import org.treetank.service.xml.xpath.XPathError; 36 import org.treetank.service.xml.xpath.XPathError.ErrorType; 37 38 /** 39 * <h1>IntersectAxis</h1> 40 * <p> 41 * Returns an intersection of two operands. This axis takes two node sequences as operands and returns a 42 * sequence containing all the nodes that occur in both operands. 43 * </p> 44 */ 45 public class IntersectAxis extends AbsAxis { 46 47 /** First operand sequence. */ 48 private final AbsAxis mOp1; 49 50 /** Second operand sequence. */ 51 private final AbsAxis mOp2; 52 53 /** Set to decide, if an item is contained in both sequences. */ 54 private final Set<Long> mDupSet; 55 56 /** 57 * Constructor. Initializes the internal state. 58 * 59 * @param rtx 60 * Exclusive (immutable) trx to iterate with. 61 * @param mOperand1 62 * First operand 63 * @param mOperand2 64 * Second operand 65 */ 66 public IntersectAxis(final INodeReadTrx rtx, final AbsAxis mOperand1, final AbsAxis mOperand2) { 67 68 super(rtx); 69 mOp1 = mOperand1; 70 mOp2 = mOperand2; 71 mDupSet = new HashSet<Long>(); 72 73 } 74 75 /** 76 * {@inheritDoc} 77 */ 78 @Override 79 public void reset(final long mNodeKey) { 80 81 super.reset(mNodeKey); 82 83 if (mDupSet != null) { 84 mDupSet.clear(); 85 } 86 87 if (mOp1 != null) { 88 mOp1.reset(mNodeKey); 89 } 90 if (mOp2 != null) { 91 mOp2.reset(mNodeKey); 92 } 93 } 94 95 /** 96 * {@inheritDoc} 97 */ 98 @Override 99 public boolean hasNext() { 100 101 // store all item keys of the first sequence to the set. 102 while (mOp1.hasNext()) { 103 if (getNode().getDataKey() < 0) { // only nodes are 104 // allowed 105 throw new XPathError(ErrorType.XPTY0004); 106 } 107 108 mDupSet.add(getNode().getDataKey()); 109 } 110 111 while (mOp2.hasNext()) { 112 113 if (getNode().getDataKey() < 0) { // only nodes are 114 // allowed 115 throw new XPathError(ErrorType.XPTY0004); 116 } 117 118 // return true, if item key is already in the set -> item is 119 // contained in 120 // both input sequences. 121 if (!mDupSet.add(getNode().getDataKey())) { 122 return true; 123 } 124 } 125 126 return false; 127 } 128 129 }