/*
 * Copyright 2013 International Health Terminology Standards Development Organisation.
 *
 * Licensed 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.ihtsdo.otf.query.implementation;

import java.io.IOException;
import org.ihtsdo.otf.tcc.api.contradiction.ContradictionException;
import org.ihtsdo.otf.tcc.api.nid.ConcurrentBitSet;
import org.ihtsdo.otf.tcc.api.nid.NativeIdSetBI;
import org.ihtsdo.otf.tcc.api.spec.ValidationException;

/**
 * Returns components that are in the incoming For set and not in the set
 * returned from the computation of the clauses that are descendents of the
 * <code>Not</code> clause.
 *
 * @author kec
 */
public class Not extends ParentClause {

    NativeIdSetBI forSet;
    NativeIdSetBI notSet;

    public Not(Query enclosingQuery, Clause child) {
        super(enclosingQuery, child);
        forSet = enclosingQuery.getForSet();
        assert forSet != null;
    }

    @Override
    public NativeIdSetBI computePossibleComponents(NativeIdSetBI incomingPossibleComponents) throws IOException, ValidationException, ContradictionException {
        this.notSet = new ConcurrentBitSet();
        for (Clause c : getChildren()) {
            notSet.or(c.computePossibleComponents(incomingPossibleComponents));
        }
        return incomingPossibleComponents;
    }

    @Override
    public WhereClause getWhereClause() {
        WhereClause whereClause = new WhereClause();
        whereClause.setSemantic(ClauseSemantic.NOT);
        for (Clause clause : getChildren()) {
            whereClause.getChildren().add(clause.getWhereClause());
        }
        return whereClause;
    }

    @Override
    public NativeIdSetBI computeComponents(NativeIdSetBI incomingComponents) throws IOException, ValidationException, ContradictionException {
        NativeIdSetBI forSetCopy = new ConcurrentBitSet(forSet);
        for (Clause c : getChildren()) {
            notSet.or(c.computeComponents(incomingComponents));
        }
        forSetCopy.andNot(notSet);
        return forSetCopy;
    }
}

@@ -1127,16 +1127,17 @@
  clause.
+ 
 %0A *%0A * @
@@ -1340,84 +1340,8 @@
 d);%0A
-        forSet = enclosingQuery.getForSet();%0A        assert forSet != null;%0A
     
@@ -2233,63 +2233,79 @@
     
-NativeIdSetBI forSetCopy = new ConcurrentBitSet(forSet)
+this.forSet = enclosingQuery.getForSet();%0A        assert forSet != null
 ;%0A  
@@ -2431,20 +2431,16 @@
   forSet
-Copy
 .andNot(
@@ -2469,20 +2469,16 @@
 n forSet
-Copy
 ;%0A    %7D%0A

Fix for NullPointerException thrown by Not clause in REST queries