1 2 5 14 package org.jacorb.trading.constraint; 15 16 import java.io.*; 17 18 19 public class Constraint 20 { 21 private PropertySchema m_schema; 22 private Expression m_expr; 23 private String m_constraint; 24 25 26 private Constraint() 27 { 28 } 29 30 31 public Constraint(PropertySchema schema) 32 { 33 m_schema = schema; 34 } 35 36 37 public void parse(String constraint) 38 throws ParseException 39 { 40 if( constraint.length() == 0 ) 41 constraint = "TRUE"; 42 43 StringReader reader = new StringReader(constraint); 44 Lex lex = new Lex(reader); 45 46 m_constraint = constraint; 47 48 m_expr = new Expression(m_schema); 49 50 ValueType type = m_expr.parse(lex); 51 52 if (! ValueType.isCompatible(type.getId(), ValueType.BOOLEAN) || 55 type.isSequence()) 56 throw new ParseException("constraint expression must be boolean"); 57 } 58 59 60 public String getConstraint() 61 { 62 return m_constraint; 63 } 64 65 66 public boolean evaluate(PropertySource source) 67 { 68 boolean result = false; 69 70 Value nv = m_expr.evaluate(source); 71 if (nv != null) { 72 Boolean b = (Boolean )nv.getValue(); 73 result = b.booleanValue(); 74 } 75 76 return result; 77 } 78 79 80 101 } 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | Popular Tags |