compare method

bool compare(
  1. Map<String, NUIConditionValue> map,
  2. NUIConditionValue val1,
  3. NUIConditionValue val2,
  4. String comparator,
  5. NUIConfigLogic mapper
)

Implementation

bool compare(Map<String, NUIConditionValue> map, NUIConditionValue val1, NUIConditionValue val2, String comparator, NUIConfigLogic mapper){
  value1Raw = NUIConditionValue.fromRawSingle(raw: this.value1, value: value1Raw);
  value2Raw = NUIConditionValue.fromRawSingle(raw: this.value2, value: value2Raw);

  if((val1 == null || val2 == null) && !(match(comparator, NUIComparisonOpt.IS_NOT_NULL.value()) || match(comparator, NUIComparisonOpt.IS_NULL.value()))){
    throwNUI("NUIConfigLogic", "ICondition -> value1Raw or value2Raw cannot be null");
  }
  if(!(match(comparator, NUIComparisonOpt.IS_NOT_NULL.value()) || match(comparator, NUIComparisonOpt.IS_NULL.value())) && !match(val1.dataType, val2.dataType) && (isNullOrEmpty(val1.getValuesRaw()) || isNullOrEmpty(val2.getValuesRaw()))){
    throwNUI("NUIConfigLogic", "ICondition -> comparing between two different types of data is not available");
  }

  NUIConditionValue value1 = val1;
  NUIConditionValue value2 = val2 != null ? val2 : NUIConditionValue();

  List<NUIConditionValue> values1 = value1.isKey != true ? value1.getValuesRaw() : value1.getIsNull(map, mapper) ? List() : value1.getValueFromMap(map).getValuesRaw();
  List<NUIConditionValue> values2 = value2.isKey != true ? value2.getValuesRaw() : value2.getIsNull(map, mapper) ? List() : value2.getValueFromMap(map).getValuesRaw();

  if(match(comparator, (NUIComparisonOpt.EQUAL.value()))){
    if(isNullOrEmpty(values1) || isNullOrEmpty(values2)) {
      //Compare directly the value between value1Raw and value2Raw
      if(match(value1.dataType, NUILogicDataType.BOOL.value())) {
        return value1.getBool(map, null) == value2.getBool(map, mapper);
      }
      else  {
        return match(value1.getString(map, null), value2.getString(map, mapper));
      }
    }
    else{
      //Compare list values
      List<NUIConditionValue> baseList = values1;
      List<NUIConditionValue> compareList = values2;
      int mustMatchSize = compareList.length > baseList.length ? compareList.length : baseList.length;

      int currentMatchedSize = 0;
      for(NUIConditionValue curVal in baseList){
        for(NUIConditionValue innVal in compareList){
          if(match(curVal.dataType, innVal.dataType)){
            //Same data type, so compare
            if((isNullOrEmpty(curVal.id) || isNullOrEmpty(innVal.id)) || match(curVal.id, innVal.id)) {
              bool isMatch = compare(map, curVal, innVal, NUIComparisonOpt.EQUAL.value(), null) || match(curVal.id, innVal.id);
              if (isMatch) {
                currentMatchedSize++;
                break;
              }
            }
          }
        }
      }

      //Having the same match count means all the data matches, in terms of id if valid, and their values
      return currentMatchedSize == mustMatchSize;
    }
  }
  else if(match(comparator, NUIComparisonOpt.NOT_EQUAL.value())) {
    if(isNullOrEmpty(values1) || isNullOrEmpty(values2)) {
      if(match(value1.dataType, NUILogicDataType.INT.value())) {
        return (value1.getInt(map, mapper)) != (value2.getInt(map, mapper));
      }
      else if(match(value1.dataType, NUILogicDataType.DOUBLE.value())) {
        return (value1.getDouble(map, mapper)) != (value2.getDouble(map, mapper));
      }
      else if(match(value1.dataType, NUILogicDataType.LONG.value())) {
        return (value1.getLong(map, mapper)) != (value2.getLong(map, mapper));
      }
      else if(match(value1.dataType, NUILogicDataType.DATE.value())) {
        return (value1.getDate(map, mapper)).millisecondsSinceEpoch != (value2.getDate(map, mapper)).millisecondsSinceEpoch;
      }
      else if(match(value1.dataType, NUILogicDataType.BOOL.value())) {
        return value1.getBool(map, mapper) != value2.getBool(map, mapper);
      }
      else {
        return !match(value1.getString(map, mapper), value2.getString(map, mapper));
      }
    }
    else{
      //Compare list values
      List<NUIConditionValue> baseList = values1;
      List<NUIConditionValue> compareList = values2;
      int mustMatchSize = compareList.length > baseList.length ? compareList.length : baseList.length;

      int currentMatchedSize = 0;
      for(NUIConditionValue curVal in baseList){
        for(NUIConditionValue innVal in compareList){
          if(match(curVal.dataType, innVal.dataType)){
            //Same data type, so compare
            if((isNullOrEmpty(curVal.id) || isNullOrEmpty(innVal.id)) || match(curVal.id, innVal.id)) {
              bool isMatch = compare(map, curVal, innVal, NUIComparisonOpt.EQUAL.value(), null) || match(curVal.id, innVal.id);
              if (isMatch) {
                currentMatchedSize++;
                break;
              }
            }
          }
        }
      }

      //Having the different match count means not all the data matches, in terms of id if valid, and their values
      return currentMatchedSize != mustMatchSize;
    }
  }
  else if(match(comparator, NUIComparisonOpt.LESS_THAN.value())) {
    if(match(value1.dataType, NUILogicDataType.INT.value())) {
      return (value1.getInt(map, mapper)) < (value2.getInt(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DOUBLE.value())){
      return (value1.getDouble(map, mapper)) < (value2.getDouble(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.LONG.value())) {
      return (value1.getLong(map, mapper)) < (value2.getLong(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DATE.value())) {
      return (value1.getDate(map, mapper)).millisecondsSinceEpoch < (value2.getDate(map, mapper)).millisecondsSinceEpoch;
    }
    else {
      return false;
    }
  }
  else if(match(comparator, NUIComparisonOpt.MORE_THAN.value())) {
    if(match(value1.dataType, NUILogicDataType.INT.value())) {
      return (value1.getInt(map, mapper)) > (value2.getInt(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DOUBLE.value())) {
      return (value1.getDouble(map, mapper)) > (value2.getDouble(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.LONG.value())) {
      return (value1.getLong(map, mapper)) > (value2.getLong(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DATE.value())) {
      return (value1.getDate(map, mapper)).millisecondsSinceEpoch > (value2.getDate(map, mapper)).millisecondsSinceEpoch;
    }
    else{
      return false;
    }
  }
  else if(match(comparator, NUIComparisonOpt.LESS_THAN_EQUAL_TO.value())) {
    if(match(value1.dataType, NUILogicDataType.INT.value())) {
      return (value1.getInt(map, mapper)) <= (value2.getInt(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DOUBLE.value())) {
      return (value1.getDouble(map, mapper)) <= (value2.getDouble(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.LONG.value())) {
      return (value1.getLong(map, mapper)) <= (value2.getLong(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DATE.value())) {
      return (value1.getDate(map, mapper)).millisecondsSinceEpoch <= (value2.getDate(map, mapper)).millisecondsSinceEpoch;
    }
    else  {
      return false;
    }
  }
  else if(match(comparator, NUIComparisonOpt.MORE_THAN_EQUAL_TO.value())) {
    if(match(value1.dataType, NUILogicDataType.INT.value())) {
      return (value1.getInt(map, mapper)) >= (value2.getInt(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DOUBLE.value())) {
      return (value1.getDouble(map, mapper)) >= (value2.getDouble(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.LONG.value())) {
      return (value1.getLong(map, mapper)) >= (value2.getLong(map, mapper));
    }
    else if(match(value1.dataType, NUILogicDataType.DATE.value())) {
      return (value1.getDate(map, mapper)).millisecondsSinceEpoch >= (value2.getDate(map, mapper)).millisecondsSinceEpoch;
    }
    else {
      return false;
    }
  }
  else if(match(comparator, NUIComparisonOpt.LIKE.value()))  {
    if(isNullOrEmpty(values1) || isNullOrEmpty(values2)) {
      return contains(value1.getString(map, mapper), value2.getString(map, mapper));
    }
    else{
      //Compare list values
      List<NUIConditionValue> baseList = values1;
      List<NUIConditionValue> compareList = values2;
      int mustMatchSize = compareList.length;

      int currentMatchedSize = 0;
      for(NUIConditionValue curVal in baseList){
        for(NUIConditionValue innVal in compareList){
          if(match(curVal.dataType, innVal.dataType)){
            //Same data type, so compare
            if((isNullOrEmpty(curVal.id) || isNullOrEmpty(innVal.id)) || match(curVal.id, innVal.id)) {
              bool isMatch = compare(map, curVal, innVal, NUIComparisonOpt.EQUAL.value(), null) || match(curVal.id, innVal.id);
              if (isMatch) {
                currentMatchedSize++;
                break;
              }
            }
          }
        }
      }

      //Must match all the data in the compare list
      return currentMatchedSize >= mustMatchSize;
    }
  }
  else if(match(comparator, NUIComparisonOpt.IS_NULL.value())) {
    return value1.getIsNull(map, mapper);
  }
  else if(match(comparator, NUIComparisonOpt.IS_NOT_NULL.value())) {
    return !value1.getIsNull(map, mapper);
  }
  else if(match(comparator, NUIComparisonOpt.IS_NULL_OR_EMPTY.value())) {
    return value1.getIsNullOrEmpty(map, mapper);
  }
  else if(match(comparator, NUIComparisonOpt.IS_NOT_NULL_OR_EMPTY.value())) {
    return !value1.getIsNullOrEmpty(map, mapper);
  }
  else {
    throwNUI("NUIConfigLogic", "ICondition -> Incorrect comparator");
  }
}