Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / instruction / NotExprAdapter.java @ 10627

History | View | Annotate | Download (2.35 KB)

1
package com.hardcode.gdbms.engine.instruction;
2

    
3
import com.hardcode.gdbms.engine.data.driver.DriverException;
4
import com.hardcode.gdbms.engine.values.BooleanValue;
5
import com.hardcode.gdbms.engine.values.Value;
6
import com.hardcode.gdbms.parser.Node;
7

    
8

    
9
/**
10
 * Adapter de las Expresiones Not del arbol sint?ctico
11
 *
12
 * @author Fernando Gonz?lez Cort?s
13
 */
14
public class NotExprAdapter extends AbstractExpression implements Expression {
15
        private boolean not = false;
16

    
17
        /**
18
         * Evalua expresi?n invocando el m?todo adecuado en funci?n del tipo de
19
         * expresion (suma, producto, ...) de los objetos Value de la expresion,
20
         * de las subexpresiones y de los objetos Field
21
         *
22
         * @param row Fila en la que se eval?a la expresi?n, en este caso no es
23
         *                   necesario, pero las subexpresiones sobre las que se opera pueden
24
         *                   ser campos de una tabla, en cuyo caso si es necesario
25
         *
26
         * @return Objeto Value resultado de la operaci?n propia de la expresi?n
27
         *                    representada por el nodo sobre el cual ?ste objeto es adaptador
28
         *
29
         * @throws SemanticException Si se produce un error sem?ntico
30
         * @throws DriverException Si se produce un error de I/O
31
         * @throws IncompatibleTypesException Si la expresi?n es una negaci?n y la
32
         *                    expresi?n que se niega no es booleana
33
         */
34
        public Value evaluate(long row) throws EvaluationException {
35
                Value ret = null;
36

    
37
                Expression c = (Expression) getChilds()[0];
38

    
39
                try {
40
                        Value value = c.evaluateExpression(row);
41

    
42
                        if (not) {
43
                                ((BooleanValue) value).setValue(!((BooleanValue) value).getValue());
44
                        }
45

    
46
                        return value;
47
                } catch (ClassCastException e) {
48
                        throw new EvaluationException();
49
                }
50
        }
51

    
52
        /**
53
         * @see com.hardcode.gdbms.engine.instruction.Expression#getFieldName()
54
         */
55
        public String getFieldName() {
56
                return null;
57
        }
58

    
59
        /**
60
         * @see com.hardcode.gdbms.engine.instruction.Expression#isLiteral()
61
         */
62
        public boolean isLiteral() {
63
                return ((Expression) getChilds()[0]).isLiteral();
64
        }
65

    
66
        /**
67
         * @see com.hardcode.gdbms.engine.instruction.Expression#simplify()
68
         */
69
        public void simplify() {
70
                if (!not) {
71
                        getParent().replaceChild(this, getChilds()[0]);
72
                }
73
        }
74

    
75
        /**
76
         * @see com.hardcode.gdbms.engine.instruction.Adapter#setEntity(com.hardcode.gdbms.parser.Node)
77
         */
78
        public void setEntity(Node o) {
79
                super.setEntity(o);
80

    
81
                String text = Utilities.getText(getEntity()).trim();
82

    
83
                if (text.startsWith("not")) {
84
                        not = true;
85
                }
86
        }
87
}