Package org.dmd.dmv.shared.extended.rulesdmo

Source Code of org.dmd.dmv.shared.extended.rulesdmo.RelatedNumbersRule

package org.dmd.dmv.shared.extended.rulesdmo;

import org.dmd.dmc.DmcAttribute;
import org.dmd.dmc.DmcObject;
import org.dmd.dmc.rules.DmcRuleException;
import org.dmd.dmc.rules.DmcRuleExceptionSet;
import org.dmd.dmv.shared.generated.dmo.RelatedNumbersRuleDataDMO;
import org.dmd.dmv.shared.generated.rulesdmo.RelatedNumbersRuleBaseImpl;

/**
* On the surface, this seems like a pretty trivial rule, but things get interesting
* when you consider that we want to be able to handle comparisons between all different
* numeric types. For simplicity, we do all comparisons using Double, but we can get into
* difficulty when we get near the boundaries, so beware!
* http://en.wikipedia.org/wiki/Strictfp
* http://stackoverflow.com/questions/4349155/ieee-754-double-64-bit-floating-point-vs-long-64-bit-integer-revisited
*
*/
public class RelatedNumbersRule extends RelatedNumbersRuleBaseImpl {

  public RelatedNumbersRule(){
   
  }
 
  public RelatedNumbersRule(RelatedNumbersRuleDataDMO dmo) {
    super(dmo);
  }

  @Override
  public void execute(DmcObject obj) throws DmcRuleExceptionSet {
    DmcAttribute<?> lhs = obj.get(ruleDMO.getLhs().getObjectName().getNameString());
    DmcAttribute<?> rhs = obj.get(ruleDMO.getRhs().getObjectName().getNameString());
   
    if ( (lhs != null) && (rhs != null)){
      Double   lv     = (Double) lhs.getSV();
      Double   rv     = (Double) rhs.getSV();
      boolean  okay   = false;
     
      // We have both values, test the relationship
      switch(ruleDMO.getNumericRelation()){
      case EQUALS:
        if (lv == rv)
          okay = true;
        break;
      case GT:
        if (lv > rv)
          okay = true;
        break;
      case GTE:
        if (lv >= rv)
          okay = true;
        break;
      case LT:
        if (lv < rv)
          okay = true;
        break;
      case LTE:
        if (lv <= rv)
          okay = true;
        break;
      case NE:
        if (lv != rv)
          okay = true;
        break;
      }
     
      if (!okay){
        DmcRuleExceptionSet ex = new DmcRuleExceptionSet();
        ex.add(new DmcRuleException(this.getRuleTitle(), this));
        throw(ex);
      }
    }
  }

}
TOP

Related Classes of org.dmd.dmv.shared.extended.rulesdmo.RelatedNumbersRule

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.