1 23 24 package org.objectweb.medor.query.lib; 25 26 import org.objectweb.jorm.type.api.PType; 27 import org.objectweb.medor.api.Field; 28 import org.objectweb.medor.api.MedorException; 29 import org.objectweb.medor.expression.api.BinaryOperator; 30 import org.objectweb.medor.filter.api.FieldOperand; 31 import org.objectweb.medor.expression.api.Operator; 32 import org.objectweb.medor.expression.api.Expression; 33 import org.objectweb.medor.query.api.CalculatedField; 34 import org.objectweb.medor.query.api.QueryTree; 35 36 import java.util.ArrayList ; 37 import java.util.Map ; 38 39 43 public class BasicCalculatedField extends BasicQueryTreeField implements CalculatedField { 44 45 protected Expression e = null; 46 47 public BasicCalculatedField() { 48 } 49 50 public BasicCalculatedField(String name, 51 PType type, 52 QueryTree qt, 53 Expression e) throws MedorException { 54 super(name, type, qt); 55 this.e = e; 56 } 57 58 public BasicCalculatedField(Expression e) { 59 this.e = e; 60 } 61 62 public Object clone(Object clone, 63 Map obj2clone) throws CloneNotSupportedException { 64 clone = super.clone(clone, obj2clone); 65 ((BasicCalculatedField) clone).e = e; 66 return clone; 67 } 68 69 public Expression getExpression() { 70 return e; 71 } 72 73 public void setExpression(Expression e) { 74 this.e = e; 75 } 76 77 public Field[] getUsedFields() { 78 return (Field[]) getUsed().toArray(new Field[0]); 79 } 80 81 public Field[] getOriginUsedFields() { 82 return (Field[]) getOriginUsedFields(getUsed(), new ArrayList ()) 83 .toArray(new Field[0]); 84 } 85 86 protected ArrayList getOriginUsedFields(ArrayList src, ArrayList dst) { 87 ArrayList res = dst; 88 for (int i=0; i<src.size(); i++) { 89 Field f = (Field) src.get(i); 90 if (f instanceof BasicPropagatedField) { 91 res = ((BasicPropagatedField) f).getOriginFields( 92 ((BasicPropagatedField) f).getPrevious(), res); 93 } 94 else if (f instanceof BasicCalculatedField) { 95 res = ((BasicCalculatedField) f).getOriginUsedFields( 96 ((BasicCalculatedField) f).getUsed(), res); 97 } 98 else { 99 res.add(f); 100 } 101 } 102 return res; 103 } 104 105 public ArrayList getUsed() { 106 ArrayList res = new ArrayList (); 107 ArrayList todo = new ArrayList (); 108 todo.add(e); 109 while(todo.size()>0) { 110 Expression current = (Expression) todo.remove(0); 111 if (current instanceof BinaryOperator) { 112 for (int i = 0; i<((Operator) current).getOperandNumber(); i++) { 113 todo.add( ((Operator) current).getExpression(i) ); 114 } 115 } 116 else if (current instanceof FieldOperand) { 117 res.add(((FieldOperand) current).getField()); 118 } 119 } 120 return res; 121 } 122 123 public short getNullStatus() { 124 return 99; 126 } 127 } 128 | Popular Tags |