default String serialize()

in src/main/java/com/spotify/github/Parameters.java [48:80]


  default String serialize() {
    return Arrays.stream(this.getClass().getInterfaces())
        .filter(Parameters.class::isAssignableFrom)
        .map(Class::getMethods)
        .flatMap(Arrays::stream)
        // Filter out any method defined in this interface.
        .filter(method -> !method.getDeclaringClass().equals(Parameters.class))
        .collect(
            toMap(
                Method::getName,
                method -> {
                  try {
                    final Object invocationResult = method.invoke(this);
                    /* Wrap everything in an optional, this is safe as we know that auto matter will
                    enforce non nulls for the mandatory parameters. We use ofNullable as we don't
                    want the serialization to crash if a mandatory parameter returns null. All empty
                    optionals will get filtered away later.
                     */
                    return invocationResult instanceof Optional
                        ? (Optional) invocationResult
                        : Optional.ofNullable(invocationResult);
                  } catch (Exception e) {
                    return Optional.empty();
                  }
                }))
        .entrySet()
        .stream()
        .filter(entry -> entry.getValue().isPresent())
        // Make it stable.
        .sorted((entry1, entry2) -> entry1.getKey().compareTo(entry2.getKey()))
        .map(entry -> entry.getKey() + "=" + entry.getValue().get())
        .collect(joining("&"));
  }