コード例 #1
0
  /**
   * Convert the value from a source unit to a target unit Note: if the value is an age, the method
   * adjusts the value to return the right age
   *
   * @param activeInstrumentRunService
   * @param targetInstrumentRunValue
   * @param sourceInstrumentRunValue
   */
  @SuppressWarnings("unchecked")
  public void convert(
      ActiveInstrumentRunService activeInstrumentRunService,
      InstrumentRunValue targetInstrumentRunValue,
      InstrumentRunValue sourceInstrumentRunValue) {

    InstrumentParameter sourceParameter =
        activeInstrumentRunService
            .getInstrumentType()
            .getInstrumentParameter(sourceInstrumentRunValue.getInstrumentParameter());
    InstrumentParameter targetParameter =
        activeInstrumentRunService
            .getInstrumentType()
            .getInstrumentParameter(targetInstrumentRunValue.getInstrumentParameter());

    log.debug(
        "Converting parameters from source {} to target {}", sourceParameter, targetParameter);

    Unit sourceUnit = Unit.valueOf(sourceParameter.getMeasurementUnit());
    Unit targetUnit = Unit.valueOf(targetParameter.getMeasurementUnit());

    log.debug(
        "Converting units from source {} to target {}",
        sourceUnit.toString(),
        targetUnit.toString());

    double sourceValue;
    // Extract the source value and convert it to a double
    try {
      sourceValue =
          Double.parseDouble(
              sourceInstrumentRunValue.getData(sourceParameter.getDataType()).getValueAsString());
    } catch (NumberFormatException e) {
      Data sourceData = sourceInstrumentRunValue.getData(sourceParameter.getDataType());
      log.error(
          "Error converting between measurement units. Original value {} of type {} cannot be converted to a double, which is required to convert between measurement units.",
          sourceData.getValueAsString(),
          sourceData.getType());
      throw e;
    }

    double newValue = sourceUnit.getConverterTo(targetUnit).convert(sourceValue);

    switch (activeInstrumentRunService
        .getInstrumentType()
        .getInstrumentParameter(targetInstrumentRunValue.getInstrumentParameter())
        .getDataType()) {
      case DECIMAL:
        targetInstrumentRunValue.setData(DataBuilder.buildDecimal(newValue));
        break;

      case INTEGER:
        if (targetUnit.toString().equalsIgnoreCase("year")) newValue = Math.floor(newValue);
        targetInstrumentRunValue.setData(DataBuilder.buildInteger(Math.round(newValue)));
        break;
    }
  }
コード例 #2
0
  /**
   * Convert the value from a source DataType to a target DataType (DECIMAL vs INTEGER)
   *
   * @param targetInstrumentRunValue
   * @param sourceData
   */
  @SuppressWarnings("unchecked")
  public void convert(
      InstrumentParameter targetInstrumentParameter,
      InstrumentRunValue targetInstrumentRunValue,
      Data sourceData) {

    log.debug(
        "Converting parameters from source {} to target {}",
        sourceData.getType(),
        targetInstrumentParameter.getDataType());

    double newValue = Double.parseDouble(sourceData.getValueAsString());

    switch (targetInstrumentParameter.getDataType()) {
      case DECIMAL:
        targetInstrumentRunValue.setData(DataBuilder.buildDecimal(newValue));
        break;

      case INTEGER:
        targetInstrumentRunValue.setData(DataBuilder.buildInteger(Math.round(newValue)));
        break;
    }
  }
  @Override
  protected Data modify(Data data, Participant participant) {

    if (data == null) return null;
    if (!data.getType().equals(DataType.DATE))
      throw new IllegalArgumentException(
          "DataType " + DataType.DATE + " expected, " + data.getType() + " received.");

    Calendar cal = Calendar.getInstance();
    cal.setTime((Date) data.getValue());

    for (DateModifier dateModifier : getDateModifiers()) {
      dateModifier.modify(cal, participant);
    }

    return DataBuilder.buildDate(cal.getTime());
  }