/*
Copyright (c) 2013, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package io.sigpipe.sing.query;

import java.io.IOException;

import io.sigpipe.sing.dataset.feature.Feature;
import io.sigpipe.sing.serialization.ByteSerializable;
import io.sigpipe.sing.serialization.SerializationException;
import io.sigpipe.sing.serialization.SerializationInputStream;
import io.sigpipe.sing.serialization.SerializationOutputStream;

/**
 * Representation of a query expression.  For example: x != 3.6.  Contains an
 * operator, and an associated value.
 *
 * @author malensek
 */
public class Expression implements ByteSerializable {

    private Operator operator;
    private Feature value;

    public Expression(Operator operator, Feature value) {
        this.operator = operator;
        this.value = value;
    }

    public Expression(String operator, Feature value) {
        this.operator = Operator.fromString(operator);
        this.value = value;
    }

    public Feature getValue() {
        return value;
    }

    public Operator getOperator() {
        return operator;
    }

    public String getOperand() {
        return value.getName();
    }

    @Override
    public String toString() {
        return value.getName() + " " + operator + " " + value.getString();
    }

    @Deserialize
    public Expression(SerializationInputStream in)
    throws IOException, SerializationException {
        operator = Operator.fromInt(in.readInt());
        boolean hasRange = (in.readInt() == 2);
        this.operand1 = new Feature(in);
        if (hasRange) {
            this.operand2 = new Feature(in);
        }
    }

    @Override
    public void serialize(SerializationOutputStream out)
    throws IOException {
        out.writeInt(operator.toInt());
        int numOperands = (operand2 != null) ? 2 : 1;
        out.writeInt(numOperands);
        operand1.serialize(out);
        if (operand2 != null) {
            operand2.serialize(out);
        }
    }
}

@@ -1783,16 +1783,59 @@
 ed value
+ (or values in the case of range operators)
 .%0A *%0A * 
@@ -1961,21 +1961,54 @@
 Feature 
-value
+operand1;%0A    private Feature operand2
 ;%0A%0A    p
@@ -2043,37 +2043,39 @@
 erator, Feature 
-value
+operand
 ) %7B%0A        this
@@ -2105,37 +2105,42 @@
        this.
-value = value
+operand1 = operand
 ;%0A    %7D%0A%0A   
@@ -2187,30 +2187,317 @@
 ure 
-value) %7B%0A        this.
+operand) %7B%0A        this(Operator.fromString(operator), operand);%0A    %7D%0A%0A    public Expression(Operator operator, Feature start, Feature end) %7B%0A        if (operator != Operator.RANGE_EXC%0A                && operator != Operator.RANGE_INC%0A                && operator != Operator.RANGE_EXC_INC%0A                && 
 oper
@@ -2493,32 +2493,33 @@
     && operator 
+!
 = Operator.fromS
@@ -2513,35 +2513,151 @@
 perator.
-fromString(
+RANGE_INC_EXC) %7B%0A%0A            throw new IllegalArgumentException(%0A                    %22Range-based expression requires a range 
 operator
 );%0A     
@@ -2648,19 +2648,31 @@
 operator
+%22
 );%0A
+        %7D%0A%0A
         
@@ -2680,82 +2680,220 @@
 his.
-value = value;%0A    %7D%0A%0A    public Feature getValue() %7B%0A        return value
+operator = operator;%0A        this.operand1 = start;%0A        this.operand2 = end;%0A    %7D%0A%0A    public Expression(String operator, Feature start, Feature end) %7B%0A        this(Operator.fromString(operator), start, end)
 ;%0A  
@@ -2976,22 +2976,23 @@
  public 
-String
+Feature
  getOper
@@ -3018,138 +3018,686 @@
 urn 
-value.getName();%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return value.getName() + %22 %22 + operator
+this.operand1;%0A    %7D%0A%0A    public Feature getSecondOperand() %7B%0A        return this.operand2;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        switch (operator) %7B%0A            case RANGE_INC:%0A            case RANGE_EXC:%0A            case RANGE_INC_EXC:%0A            case RANGE_EXC_INC:%0A                return operand1.getName() + %22 in %22%0A                    + operator.toString().charAt(0)%0A                    + operand1.getString()%0A                   
  + %22
+,
  %22
- + value
+%0A                    + operand2.getString()%0A                    + operator.toString().charAt(1);%0A%0A            default:%0A                return operand1.getName() + %22 %22 + operator + %22 %22%0A                    + operand1
 .get
@@ -3706,16 +3706,26 @@
 ring();%0A
+        %7D%0A
     %7D%0A%0A 

Update for two-operand expressions