コード例 #1
0
 public void addOperand(String newOperand) throws FormatException {
   Rational test = format.parse(newOperand, base);
   String test2 = format.toString(test, base);
   if (!newOperand.contains(".")) {
     newOperand = newOperand + ".0";
   }
   if (!test2.equals(newOperand)) {
     try {
       throw new NumberBaseException("Wrong number for this base.");
     } catch (NumberBaseException e) {
       System.out.println(e.getMessage());
     }
   } else {
     operand_1 = operand_0;
     operand_0 = format.parse(newOperand, base);
   }
 }
コード例 #2
0
ファイル: BindyCsvFactory.java プロジェクト: Jeanne0523/camel
 /**
  * Set the default values for the non defined fields.
  *
  * @param model the model which has its default fields set.
  * @throws IllegalAccessException if the underlying fields are inaccessible
  * @throws Exception In case the field cannot be parsed
  */
 private void setDefaultValuesForFields(final Map<String, Object> model)
     throws IllegalAccessException, Exception {
   // Set the default values, if defined
   for (int i = 1; i <= dataFields.size(); i++) {
     Field field = annotatedFields.get(i);
     field.setAccessible(true);
     DataField dataField = dataFields.get(i);
     Object modelField = model.get(field.getDeclaringClass().getName());
     if (field.get(modelField) == null && !dataField.defaultValue().isEmpty()) {
       Format<?> format = FormatFactory.getFormat(field.getType(), getLocale(), dataField);
       Object value = format.parse(dataField.defaultValue());
       field.set(modelField, value);
     }
   }
 }
コード例 #3
0
ファイル: BindyCsvFactory.java プロジェクト: Jeanne0523/camel
  public void bind(List<String> tokens, Map<String, Object> model, int line) throws Exception {

    int pos = 1;
    int counterMandatoryFields = 0;

    for (String data : tokens) {

      // Get DataField from model
      DataField dataField = dataFields.get(pos);
      ObjectHelper.notNull(
          dataField, "No position " + pos + " defined for the field: " + data + ", line: " + line);

      if (dataField.trim()) {
        data = data.trim();
      }

      if (dataField.required()) {
        // Increment counter of mandatory fields
        ++counterMandatoryFields;

        // Check if content of the field is empty
        // This is not possible for mandatory fields
        if (data.equals("")) {
          throw new IllegalArgumentException(
              "The mandatory field defined at the position "
                  + pos
                  + " is empty for the line: "
                  + line);
        }
      }

      // Get Field to be setted
      Field field = annotatedFields.get(pos);
      field.setAccessible(true);

      if (LOG.isDebugEnabled()) {
        LOG.debug("Pos: {}, Data: {}, Field type: {}", new Object[] {pos, data, field.getType()});
      }

      // Create format object to format the field
      Format<?> format = FormatFactory.getFormat(field.getType(), getLocale(), dataField);

      // field object to be set
      Object modelField = model.get(field.getDeclaringClass().getName());

      // format the data received
      Object value = null;

      if (!data.equals("")) {
        try {
          value = format.parse(data);
        } catch (FormatException ie) {
          throw new IllegalArgumentException(
              ie.getMessage() + ", position: " + pos + ", line: " + line, ie);
        } catch (Exception e) {
          throw new IllegalArgumentException(
              "Parsing error detected for field defined at the position: "
                  + pos
                  + ", line: "
                  + line,
              e);
        }
      } else {
        if (!dataField.defaultValue().isEmpty()) {
          value = format.parse(dataField.defaultValue());
        } else {
          value = getDefaultValueForPrimitive(field.getType());
        }
      }

      field.set(modelField, value);

      ++pos;
    }

    LOG.debug("Counter mandatory fields: {}", counterMandatoryFields);

    if (counterMandatoryFields < numberMandatoryFields) {
      throw new IllegalArgumentException("Some mandatory fields are missing, line: " + line);
    }

    if (pos < totalFields) {
      setDefaultValuesForFields(model);
    }
  }