package org.intermine.objectstore.query;

/*
 * Copyright (C) 2002-2013 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.List;

/**
 * Object representing a combination of ObjectStoreBags.
 *
 * @author Matthew Wakeling
 */
public class ObjectStoreBagCombination implements QuerySelectable
{
    private final List<ObjectStoreBag> bags = new ArrayList<ObjectStoreBag>();
    private final int op;
    /** Constant representing a UNION operation */
    public static final int UNION = 879234;
    /** Constant representing an INTERSECTION operation */
    public static final int INTERSECT = 519552;
    /** Constant representing an EXCEPT operation */
    public static final int EXCEPT = 281056;
    /** Constant representing an all but intersect operation */
    public static final int ALLBUTINTERSECT = 853915;

    /**
     * Constructs a new ObjectStoreBagCombination.
     *
     * @param op the type of combination, out of UNION, INTERSECT, and EXCEPT
     */
    public ObjectStoreBagCombination(int op) {
        if ((op != UNION) && (op != INTERSECT) && (op != EXCEPT) && (op != ALLBUTINTERSECT)) {
            throw new IllegalArgumentException("Illegal type: " + op);
        }
        this.op = op;
    }

    /**
     * Adds a bag to this combination
     *
     * @param bag an ObjectStoreBag
     */
    public void addBag(ObjectStoreBag bag) {
        bags.add(bag);
    }

    /**
     * Returns the op of this combination.
     *
     * @return an int
     */
    public int getOp() {
        return op;
    }

    /**
     * Returns the List of bags.
     *
     * @return a List of ObjectStoreBags
     */
    public List<ObjectStoreBag> getBags() {
        return bags;
    }

    /**
     * {@inheritDoc}
     */
    public Class<?> getType() {
        return Integer.class;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object o) {
        if (o instanceof ObjectStoreBagCombination) {
            return bags.equals(((ObjectStoreBagCombination) o).bags)
                && (op == ((ObjectStoreBagCombination) o).op);
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return bags.hashCode() + op;
    }
}

@@ -549,16 +549,73 @@
 table%0A%7B%0A
+    // List of either Bags or ObjectStoreBagCombinations%0A
     priv
@@ -629,30 +629,31 @@
 al List%3C
-ObjectStoreBag
+QuerySelectable
 %3E bags =
@@ -667,30 +667,31 @@
 rayList%3C
-ObjectStoreBag
+QuerySelectable
 %3E();%0A   
@@ -1700,32 +1700,133 @@
 dd(bag);%0A    %7D%0A%0A
+    public void addBagCombination(ObjectStoreBagCombination combo) %7B%0A        bags.add(combo);%0A    %7D%0A%0A
     /**%0A     * R
@@ -2061,30 +2061,31 @@
 ic List%3C
-ObjectStoreBag
+QuerySelectable
 %3E getBag

Made it possible for bag combinations to be recursive.