例如条件:(A=B)&&(B>C)
条件使用的是条件组的概念,单独的条件也是一个条件组例如,A和B是条件关联,B和C也是条件关联,(A=B)和(B>C)是组关联,那么我建的对象如下
1 public class GroupCondition implements Cloneable{ 2 3 //star 左边所有的判断 4 /** 5 * A左边的属性判断 6 */ 7 private Param paramA; 8 /** 9 * 放入的条件判断 属性和属性的条件判断 10 */ 11 private IConditionOperation iConditionOperation; 12 /** 13 * B右边的属性判断 14 */ 15 private Param paramB; 16 //end 右边所有的判断 17 /** 18 * 组的判断条件 19 */ 20 private IConditionOperation iConditionOperationGroup; 21 22 /** 23 * 组 24 */ 25 private GroupCondition groupCondition; 26 27 28 public GroupCondition(){} 29 /** 30 * 实例化一个组 只包含了 一个组的基本条件 31 * @param paramA 32 * @param iConditionOperation 33 * @param paramB 34 */ 35 public GroupCondition(Param paramA, IConditionOperation iConditionOperation, Param paramB) { 36 this.paramA = paramA; 37 this.iConditionOperation = iConditionOperation; 38 this.paramB = paramB; 39 } 40 41 /** 42 * 实例化一个完全的组 包含了三个基本条件和一个租 43 * @param paramA 44 * @param iConditionOperation 45 * @param paramB 46 * @param iConditionOperationGroup 47 * @param groupCondition 48 */ 49 public GroupCondition(Param paramA, 50 IConditionOperation iConditionOperation, 51 Param paramB, 52 IConditionOperation iConditionOperationGroup, 53 GroupCondition groupCondition) { 54 this.paramA = paramA; 55 this.iConditionOperation = iConditionOperation; 56 this.paramB = paramB; 57 this.iConditionOperationGroup = iConditionOperationGroup; 58 this.groupCondition = groupCondition; 59 } 60 61 /** 62 * 实例化两个租 63 * @param groupConditionA 64 * @param iConditionOperationGroup 65 * @param groupConditionB 66 */ 67 public GroupCondition(GroupCondition groupConditionA, 68 IConditionOperation iConditionOperationGroup, 69 GroupCondition groupConditionB) { 70 71 this.paramA = groupConditionA.getParamA(); 72 this.paramB = groupConditionA.getParamB(); 73 this.iConditionOperationGroup = iConditionOperationGroup; 74 this.groupCondition = groupConditionB; 75 } 76 77 public Param getParamA() { 78 return paramA; 79 } 80 81 public void setParamA(Param paramA) { 82 this.paramA = paramA; 83 } 84 85 public IConditionOperation getiConditionOperation() { 86 return iConditionOperation; 87 } 88 89 public void setiConditionOperation(IConditionOperation iConditionOperation) { 90 this.iConditionOperation = iConditionOperation; 91 } 92 93 public Param getParamB() { 94 return paramB; 95 } 96 97 public void setParamB(Param paramB) { 98 this.paramB = paramB; 99 } 100 101 public IConditionOperation getiConditionOperationGroup() { 102 return iConditionOperationGroup; 103 } 104 105 public void setiConditionOperationGroup(IConditionOperation iConditionOperationGroup) { 106 this.iConditionOperationGroup = iConditionOperationGroup; 107 } 108 109 public GroupCondition getGroupCondition() { 110 return groupCondition; 111 } 112 113 public void setGroupCondition(GroupCondition groupCondition) { 114 this.groupCondition = groupCondition; 115 } 116 117 @Override 118 protected GroupCondition clone() throws CloneNotSupportedException { 119 GroupCondition groupCondition = new GroupCondition(); 120 if(this.getParamA()!=null){ 121 groupCondition.setParamA(groupCondition.getParamA().clone()); 122 } 123 if(this.getParamB()!=null){ 124 groupCondition.setParamB(groupCondition.getParamB().clone()); 125 } 126 if(this.getiConditionOperation()!=null){ 127 groupCondition.setiConditionOperation(this.getiConditionOperation()); 128 } 129 if(this.getiConditionOperationGroup()!=null){ 130 groupCondition.setiConditionOperation(this.getiConditionOperationGroup()); 131 } 132 if(this.getGroupCondition()!=null){ 133 groupCondition.setGroupCondition(this.getGroupCondition().clone()); 134 } 135 return groupCondition; 136 } 137 }
代码解析
1.使用递归的关系进行实现条件A作为paramA,条件B作为paramB,中间的接口iConditionOperation来进行条件判断
条件判断的代码
1.接口
1 public interface IConditionOperation { 2 3 /** 4 * 组比较的成员 5 * @param paramA 6 * @param paramB 7 * @return 8 */ 9 boolean operation(Param paramA,Param paramB); 10 11 /** 12 * 组关系和组关系之间的逻辑判断 13 * @param flagA 14 * @param flagB 15 * @return 16 */ 17 boolean operationLogic(boolean flagA,boolean flagB); 18 }
分成两个,组内比较,和组和组之间比较实现,组和组之间的条件判断就是and或者or进行条件判断
1 public class AndIConditionOperationImpl implements IConditionOperation { 2 protected AndIConditionOperationImpl(){} 3 /** 4 * 5 * @param paramA 6 * @param paramB 7 * @return 8 */ 9 @Override 10 public boolean operation(Param paramA, Param paramB) { 11 ParamValue paramValueA = paramA.getParamValue(); 12 ParamValue paramValueB = paramB.getParamValue(); 13 if (paramValueA != null && paramValueB != null) { 14 if (StringUtils.isNotBlank(paramValueA.getValue()) 15 && StringUtils.isNotBlank(paramValueB.getValue())){ 16 /** 17 * 判断相等的时候 进行返回为true 18 */ 19 if(paramValueA.getValue().equals(paramValueB.getValue())){ 20 return true; 21 }else { 22 return false; 23 } 24 }else { 25 return false; 26 } 27 } else { 28 return false; 29 } 30 } 31 32 /** 33 * 组关系和组关系之间的逻辑判断 34 * true and true = true 35 * false and false = false 36 * true and false = false 37 * false and true = false 38 * @param flagA 39 * @param flagB 40 * @return 41 */ 42 @Override 43 public boolean operationLogic(boolean flagA, boolean flagB) { 44 if(flagA==true&&flagB==true){ 45 return true; 46 }else { 47 return false; 48 } 49 } 50 }
1 public class OrIConditionOperationImpl implements IConditionOperation { 2 protected OrIConditionOperationImpl(){} 3 /** 4 * 5 * @param paramA 6 * @param paramB 7 * @return 8 */ 9 @Override 10 public boolean operation(Param paramA, Param paramB) { 11 ParamValue paramValueA = paramA.getParamValue(); 12 ParamValue paramValueB = paramB.getParamValue(); 13 if (paramValueA != null && paramValueB != null) { 14 if (StringUtils.isNotBlank(paramValueA.getValue()) 15 && StringUtils.isNotBlank(paramValueB.getValue())){ 16 /** 17 * 判断相等的时候 进行返回为true 18 * 或的返回都是true 19 */ 20 if(paramValueA.getValue().equals(paramValueB.getValue())){ 21 return true; 22 }else { 23 return true; 24 } 25 }else { 26 return false; 27 } 28 } else { 29 return false; 30 } 31 } 32 33 /** 34 * 组关系和组关系之间的逻辑判断 35 * true or true = true 36 * true or false= true 37 * false or true = true 38 * false or false = false 39 * @param flagA 40 * @param flagB 41 * @return 42 */ 43 @Override 44 public boolean operationLogic(boolean flagA, boolean flagB) { 45 if(flagA==false&&flagB==false){ 46 return false; 47 }else { 48 return true; 49 } 50 } 51 }
2.=的实现和>的实现
1 public class EQIConditionOperationImpl implements IConditionOperation { 2 protected EQIConditionOperationImpl(){} 3 /** 4 * 5 * @param paramA 6 * @param paramB 7 * @return 8 */ 9 @Override 10 public boolean operation(Param paramA, Param paramB) { 11 ParamValue paramValueA = paramA.getParamValue(); 12 ParamValue paramValueB = paramB.getParamValue(); 13 if (paramValueA != null && paramValueB != null) { 14 if (StringUtils.isNotBlank(paramValueA.getValue()) 15 && StringUtils.isNotBlank(paramValueB.getValue())){ 16 /** 17 * 判断相等的时候 进行返回为true 18 */ 19 if(paramValueA.getValue().equals(paramValueB.getValue())){ 20 return true; 21 }else { 22 return false; 23 } 24 }else { 25 return false; 26 } 27 } else { 28 return false; 29 } 30 } 31 32 /** 33 * 组关系和组关系之间的逻辑判断 34 * 35 * @param flagA 36 * @param flagB 37 * @return 38 */ 39 @Override 40 public boolean operationLogic(boolean flagA, boolean flagB) { 41 return true; 42 } 43 }
1 public class GTIConditionOperationImpl implements IConditionOperation { 2 protected GTIConditionOperationImpl(){} 3 /** 4 * @param paramA 5 * @param paramB 6 * @return 7 */ 8 @Override 9 public boolean operation(Param paramA, Param paramB) { 10 ParamValue paramValueA = paramA.getParamValue(); 11 ParamValue paramValueB = paramB.getParamValue(); 12 try { 13 if (paramValueA != null && paramValueB != null) { 14 if (StringUtils.isNotBlank(paramValueA.getValue()) 15 && StringUtils.isNotBlank(paramValueB.getValue())) { 16 /** 17 * 判断 18 */ 19 Long paramLongA = Long.valueOf(paramValueA.getValue()); 20 Long paramLongB = Long.valueOf(paramValueB.getValue()); 21 if (paramLongA > paramLongB) { 22 return true; 23 } else { 24 return false; 25 } 26 } else { 27 return false; 28 } 29 } else { 30 return false; 31 } 32 } catch (NumberFormatException numberFormatExcepiotn) { 33 throw new ConditionParamTransException("参数转换成数值异常"); 34 } 35 } 36 37 /** 38 * 组关系和组关系之间的逻辑判断 39 * 40 * @param flagA 41 * @param flagB 42 * @return 43 */ 44 @Override 45 public boolean operationLogic(boolean flagA, boolean flagB) { 46 return false; 47 } 48 }
< ,<=,>,>=为数值判断,=使用进行值equlase进行判断。