|
| 1 | +package graphql.scalars.datetime; |
| 2 | + |
| 3 | +import graphql.language.StringValue; |
| 4 | +import graphql.schema.Coercing; |
| 5 | +import graphql.schema.CoercingParseLiteralException; |
| 6 | +import graphql.schema.CoercingParseValueException; |
| 7 | +import graphql.schema.CoercingSerializeException; |
| 8 | + |
| 9 | +import java.time.DateTimeException; |
| 10 | +import java.time.LocalTime; |
| 11 | +import java.time.format.DateTimeFormatter; |
| 12 | +import java.time.format.DateTimeParseException; |
| 13 | +import java.time.temporal.TemporalAccessor; |
| 14 | +import java.util.function.Function; |
| 15 | + |
| 16 | +import static graphql.scalars.util.Kit.typeName; |
| 17 | + |
| 18 | +public class LocalTimeCoercing implements Coercing<LocalTime, String> { |
| 19 | + |
| 20 | + private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_TIME; |
| 21 | + |
| 22 | + @Override |
| 23 | + public String serialize(final Object input) throws CoercingSerializeException { |
| 24 | + TemporalAccessor temporalAccessor; |
| 25 | + if (input instanceof TemporalAccessor) { |
| 26 | + temporalAccessor = (TemporalAccessor) input; |
| 27 | + } else if (input instanceof String) { |
| 28 | + temporalAccessor = parseTime(input.toString(), CoercingSerializeException::new); |
| 29 | + } else { |
| 30 | + throw new CoercingSerializeException( |
| 31 | + "Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'." |
| 32 | + ); |
| 33 | + } |
| 34 | + try { |
| 35 | + return dateFormatter.format(temporalAccessor); |
| 36 | + } catch (DateTimeException e) { |
| 37 | + throw new CoercingSerializeException( |
| 38 | + "Unable to turn TemporalAccessor into full time because of : '" + e.getMessage() + "'." |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public LocalTime parseValue(final Object input) throws CoercingParseValueException { |
| 45 | + TemporalAccessor temporalAccessor; |
| 46 | + if (input instanceof TemporalAccessor) { |
| 47 | + temporalAccessor = (TemporalAccessor) input; |
| 48 | + } else if (input instanceof String) { |
| 49 | + temporalAccessor = parseTime(input.toString(), CoercingParseValueException::new); |
| 50 | + } else { |
| 51 | + throw new CoercingParseValueException( |
| 52 | + "Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'." |
| 53 | + ); |
| 54 | + } |
| 55 | + try { |
| 56 | + return LocalTime.from(temporalAccessor); |
| 57 | + } catch (DateTimeException e) { |
| 58 | + throw new CoercingParseValueException( |
| 59 | + "Unable to turn TemporalAccessor into full time because of : '" + e.getMessage() + "'." |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public LocalTime parseLiteral(final Object input) throws CoercingParseLiteralException { |
| 66 | + if (!(input instanceof StringValue)) { |
| 67 | + throw new CoercingParseLiteralException( |
| 68 | + "Expected AST type 'StringValue' but was '" + typeName(input) + "'." |
| 69 | + ); |
| 70 | + } |
| 71 | + return parseTime(((StringValue) input).getValue(), CoercingParseLiteralException::new); |
| 72 | + } |
| 73 | + |
| 74 | + private static LocalTime parseTime(String s, Function<String, RuntimeException> exceptionMaker) { |
| 75 | + try { |
| 76 | + TemporalAccessor temporalAccessor = dateFormatter.parse(s); |
| 77 | + return LocalTime.from(temporalAccessor); |
| 78 | + } catch (DateTimeParseException e) { |
| 79 | + throw exceptionMaker.apply("Invalid local time value : '" + s + "'. because of : '" + e.getMessage() + "'"); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments