View Javadoc

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>ExceptAxis</h1>
40   * <p>
41   * Returns the nodes of the first operand except those of the second operand. This axis takes two node
42   * sequences as operands and returns a sequence containing all the nodes that occur in the first, but not in
43   * the second operand.
44   * </p>
45   */
46  public class ExceptAxis extends AbsAxis {
47  
48      /** First operand sequence. */
49      private final AbsAxis mOp1;
50  
51      /** Second operand sequence. */
52      private final AbsAxis mOp2;
53  
54      /**
55       * Set that is used to determine, whether an item of the first operand is
56       * also contained in the result set of the second operand.
57       */
58      private final Set<Long> mDupSet;
59  
60      /**
61       * Constructor. Initializes the internal state.
62       * 
63       * @param rtx
64       *            Exclusive (immutable) trx to iterate with.
65       * @param mOperand1
66       *            First operand
67       * @param mOperand2
68       *            Second operand
69       */
70      public ExceptAxis(final INodeReadTrx rtx, final AbsAxis mOperand1, final AbsAxis mOperand2) {
71  
72          super(rtx);
73          mOp1 = mOperand1;
74          mOp2 = mOperand2;
75          mDupSet = new HashSet<Long>();
76  
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public void reset(final long mNodeKey) {
84  
85          super.reset(mNodeKey);
86          if (mDupSet != null) {
87              mDupSet.clear();
88          }
89  
90          if (mOp1 != null) {
91              mOp1.reset(mNodeKey);
92          }
93          if (mOp2 != null) {
94              mOp2.reset(mNodeKey);
95          }
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public boolean hasNext() {
103 
104         // first all items of the second operand are stored in the set.
105         while (mOp2.hasNext()) {
106             if (getNode().getDataKey() < 0) { // only nodes are
107                 // allowed
108                 throw new XPathError(ErrorType.XPTY0004);
109             }
110             mDupSet.add(getNode().getDataKey());
111         }
112 
113         while (mOp1.hasNext()) {
114             if (getNode().getDataKey() < 0) { // only nodes are
115                 // allowed
116                 throw new XPathError(ErrorType.XPTY0004);
117             }
118 
119             // return true, if node is not already in the set, which means, that
120             // it is
121             // not also an item of the result set of the second operand
122             // sequence.
123             if (mDupSet.add(getNode().getDataKey())) {
124                 return true;
125             }
126         }
127 
128         return false;
129     }
130 
131 }