All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.ext.transmodelapi.model.scalars.DoubleFunctionScalarFactory Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.ext.transmodelapi.model.scalars;

import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.GraphQLScalarType;
import org.opentripplanner.routing.api.request.RequestFunctions;

import java.util.function.DoubleFunction;

public class DoubleFunctionScalarFactory {

  private static final String DOCUMENTATION =
      "A linear function to calculate a value(y) based on a parameter (x): "
          + "`y = f(x) = a + bx`. It allows setting both a constant(a) and a coefficient(b) and "
          + "the use those in the computation. Format: `a + b x`. Example: `1800 + 2.0 x`";

  private DoubleFunctionScalarFactory() {
  }

  public static GraphQLScalarType createDoubleFunctionScalar() {
    return GraphQLScalarType
        .newScalar()
        .name("DoubleFunction")
        .description(DOCUMENTATION)
        .coercing(new Coercing, String>() {
          @Override
          public String serialize(Object dataFetcherResult) {
            return RequestFunctions.serialize(dataFetcherResult);
          }

          @Override
          public DoubleFunction parseValue(Object input) throws CoercingParseValueException {
            try {
              return RequestFunctions.parse((String) input);
            }
            catch (IllegalArgumentException e) {
              throw new CoercingParseValueException(e.getMessage(), e);
            }
          }

          @Override
          public DoubleFunction parseLiteral(Object input)
              throws CoercingParseLiteralException {
            if (input instanceof StringValue) {
              return parseValue(((StringValue) input).getValue());
            }
            return null;
          }
        })
        .build();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy