Beispiel #1
0
  /**
   * Common code for PyFloat, {@link PyInteger} and {@link PyLong} to prepare a {@link
   * FloatFormatter} from a parsed specification. The object returned has format method {@link
   * FloatFormatter#format(double)}.
   *
   * @param spec a parsed PEP-3101 format specification.
   * @return a formatter ready to use, or null if the type is not a floating point format type.
   * @throws PyException(ValueError) if the specification is faulty.
   */
  @SuppressWarnings("fallthrough")
  static FloatFormatter prepareFormatter(Spec spec) {

    // Slight differences between format types
    switch (spec.type) {
      case 'n':
        if (spec.grouping) {
          throw Formatter.notAllowed("Grouping", "float", spec.type);
        }
        // Fall through

      case Spec.NONE:
      case 'e':
      case 'f':
      case 'g':
      case 'E':
      case 'F':
      case 'G':
      case '%':
        // Check for disallowed parts of the specification
        if (spec.alternate) {
          throw FloatFormatter.alternateFormNotAllowed("float");
        }
        // spec may be incomplete. The defaults are those commonly used for numeric formats.
        spec = spec.withDefaults(Spec.NUMERIC);
        return new FloatFormatter(spec);

      default:
        return null;
    }
  }