Skip to content

Commit 9ceef16

Browse files
committed
Use Java 17 features.
1 parent 30e4a80 commit 9ceef16

File tree

7 files changed

+125
-105
lines changed

7 files changed

+125
-105
lines changed

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlDateCoercing.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,14 @@ public GraphqlDateCoercing(Collection<DateTimeFormatter> formatters) {
5151
initBasicFormatters();
5252
}
5353

54-
private void initBasicFormatters() {
55-
formatters.add(DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC));
56-
formatters.add(DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneOffset.UTC));
57-
formatters.add(DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZoneOffset.UTC));
58-
}
59-
60-
private Date convertImpl(Object input) {
61-
if (input instanceof String string) {
62-
LocalDateTime localDateTime = parseDate(string);
63-
64-
if (localDateTime != null) {
65-
return DateTimeHelper.toDate(localDateTime);
66-
}
67-
} else if (input instanceof Date date) {
68-
return date;
69-
}
70-
return null;
71-
}
72-
7354
@Override
7455
public String serialize(Object input) {
7556
if (input instanceof Date date) {
7657
return DateTimeHelper.toISOString(date);
7758
} else {
7859
Date result = convertImpl(input);
7960
if (result == null) {
80-
throw new CoercingSerializeException("Invalid value '" + input + "' for Date");
61+
throw new CoercingSerializeException(getErrorMessage(input));
8162
}
8263
return DateTimeHelper.toISOString(result);
8364
}
@@ -87,7 +68,7 @@ public String serialize(Object input) {
8768
public Date parseValue(Object input) {
8869
Date result = convertImpl(input);
8970
if (result == null) {
90-
throw new CoercingParseValueException("Invalid value '" + input + "' for Date");
71+
throw new CoercingParseValueException(getErrorMessage(input));
9172
}
9273
return result;
9374
}
@@ -101,12 +82,31 @@ public Date parseLiteral(Object input) {
10182
String value = ((StringValue) input).getValue();
10283
Date result = convertImpl(value);
10384
if (result == null) {
104-
throw new CoercingParseLiteralException("Invalid value '" + input + "' for Date");
85+
throw new CoercingParseLiteralException(getErrorMessage(input));
10586
}
10687

10788
return result;
10889
}
10990

91+
private void initBasicFormatters() {
92+
formatters.add(DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC));
93+
formatters.add(DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneOffset.UTC));
94+
formatters.add(DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZoneOffset.UTC));
95+
}
96+
97+
private Date convertImpl(Object input) {
98+
if (input instanceof String string) {
99+
LocalDateTime localDateTime = parseDate(string);
100+
101+
if (localDateTime != null) {
102+
return DateTimeHelper.toDate(localDateTime);
103+
}
104+
} else if (input instanceof Date date) {
105+
return date;
106+
}
107+
return null;
108+
}
109+
110110
private LocalDateTime parseDate(String date) {
111111
Objects.requireNonNull(date, "date");
112112

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlDurationCoercing.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,14 @@
3131
@Internal
3232
public class GraphqlDurationCoercing implements Coercing<Duration, String> {
3333

34-
/**
35-
* @param input - the input string with an ISO 8601 Duration, e.g. P3Y6M4DT12H30M5S or PT1H30M
36-
* @return - a java.time.Duration object
37-
*/
38-
private Duration convertImpl(Object input) {
39-
if (input instanceof String) {
40-
try {
41-
return Duration.parse((String) input);
42-
} catch (DateTimeParseException ignored) {
43-
// nothing to-do
44-
}
45-
} else if (input instanceof Duration) {
46-
return (Duration) input;
47-
}
48-
return null;
49-
}
50-
5134
@Override
5235
public String serialize(Object input) {
53-
if (input instanceof Duration) {
54-
return ((Duration) input).toString();
36+
if (input instanceof Duration duration) {
37+
return duration.toString();
5538
} else {
5639
Duration result = convertImpl(input);
5740
if (result == null) {
58-
throw new CoercingSerializeException("Invalid value '" + input + "' for Duration");
41+
throw new CoercingSerializeException(getErrorMessage(input));
5942
}
6043
return result.toString();
6144
}
@@ -65,7 +48,7 @@ public String serialize(Object input) {
6548
public Duration parseValue(Object input) {
6649
Duration result = convertImpl(input);
6750
if (result == null) {
68-
throw new CoercingParseValueException("Invalid value '" + input + "' for Duration");
51+
throw new CoercingParseValueException(getErrorMessage(input));
6952
}
7053
return result;
7154
}
@@ -79,10 +62,31 @@ public Duration parseLiteral(Object input) {
7962
String value = ((StringValue) input).getValue();
8063
Duration result = convertImpl(value);
8164
if (result == null) {
82-
throw new CoercingParseLiteralException("Invalid value '" + input + "' for Duration");
65+
throw new CoercingParseLiteralException(getErrorMessage(input));
8366
}
8467

8568
return result;
8669
}
8770

71+
/**
72+
* @param input - the input string with an ISO 8601 Duration, e.g. P3Y6M4DT12H30M5S or PT1H30M
73+
* @return - a java.time.Duration object
74+
*/
75+
private Duration convertImpl(Object input) {
76+
if (input instanceof String string) {
77+
try {
78+
return Duration.parse(string);
79+
} catch (DateTimeParseException ignored) {
80+
// nothing to-do
81+
}
82+
} else if (input instanceof Duration duration) {
83+
return duration;
84+
}
85+
return null;
86+
}
87+
88+
private String getErrorMessage(Object input) {
89+
return "Invalid value '" + input + "' for Duration";
90+
}
91+
8892
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlLocalDateCoercing.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,14 @@ public GraphqlLocalDateCoercing(boolean zoneConversionEnabled, DateTimeFormatter
4040
this.converter = new LocalDateTimeConverter(zoneConversionEnabled, formatter);
4141
}
4242

43-
private LocalDate convertImpl(Object input) {
44-
if (input instanceof String) {
45-
LocalDateTime localDateTime = converter.parseDate((String) input);
46-
47-
if (localDateTime != null) {
48-
return localDateTime.toLocalDate();
49-
}
50-
} else if (input instanceof LocalDate) {
51-
return (LocalDate) input;
52-
}
53-
return null;
54-
}
55-
5643
@Override
5744
public String serialize(Object input) {
58-
if (input instanceof LocalDate) {
59-
return converter.formatDate((LocalDate) input, formatter);
45+
if (input instanceof LocalDate localDate) {
46+
return converter.formatDate(localDate, formatter);
6047
} else {
6148
LocalDate result = convertImpl(input);
6249
if (result == null) {
63-
throw new CoercingSerializeException("Invalid value '" + input + "' for LocalDate");
50+
throw new CoercingSerializeException(getErrorMessage(input));
6451
}
6552
return converter.formatDate(result, formatter);
6653
}
@@ -70,7 +57,7 @@ public String serialize(Object input) {
7057
public LocalDate parseValue(Object input) {
7158
LocalDate result = convertImpl(input);
7259
if (result == null) {
73-
throw new CoercingParseValueException("Invalid value '" + input + "' for LocalDate");
60+
throw new CoercingParseValueException(getErrorMessage(input));
7461
}
7562
return result;
7663
}
@@ -84,10 +71,27 @@ public LocalDate parseLiteral(Object input) {
8471
String value = ((StringValue) input).getValue();
8572
LocalDate result = convertImpl(value);
8673
if (result == null) {
87-
throw new CoercingParseLiteralException("Invalid value '" + input + "' for LocalDate");
74+
throw new CoercingParseLiteralException(getErrorMessage(input));
8875
}
8976

9077
return result;
9178
}
9279

80+
private LocalDate convertImpl(Object input) {
81+
if (input instanceof String string) {
82+
LocalDateTime localDateTime = converter.parseDate(string);
83+
84+
if (localDateTime != null) {
85+
return localDateTime.toLocalDate();
86+
}
87+
} else if (input instanceof LocalDate localDate) {
88+
return localDate;
89+
}
90+
return null;
91+
}
92+
93+
private String getErrorMessage(Object input) {
94+
return "Invalid value '" + input + "' for LocalDate";
95+
}
96+
9397
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlLocalDateTimeCoercing.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ public GraphqlLocalDateTimeCoercing(boolean zoneConversionEnabled, DateTimeForma
4040
}
4141

4242
private LocalDateTime convertImpl(Object input) {
43-
if (input instanceof String) {
44-
LocalDateTime localDateTime = converter.parseDate((String) input);
43+
if (input instanceof String string) {
44+
LocalDateTime localDateTime = converter.parseDate(string);
4545

4646
if (localDateTime != null) {
4747
return localDateTime;
4848
}
49-
} else if (input instanceof LocalDateTime) {
50-
return (LocalDateTime) input;
49+
} else if (input instanceof LocalDateTime localDateTime) {
50+
return localDateTime;
5151
}
5252
return null;
5353
}
5454

5555
@Override
5656
public String serialize(Object input) {
57-
if (input instanceof LocalDateTime) {
58-
return converter.formatDate((LocalDateTime) input, formatter);
57+
if (input instanceof LocalDateTime localDateTime) {
58+
return converter.formatDate(localDateTime, formatter);
5959
} else {
6060
LocalDateTime result = convertImpl(input);
6161
if (result == null) {
62-
throw new CoercingSerializeException("Invalid value '" + input + "' for LocalDateTime");
62+
throw new CoercingSerializeException(getErrorMessage(input));
6363
}
6464
return converter.formatDate(result, formatter);
6565
}
@@ -69,7 +69,7 @@ public String serialize(Object input) {
6969
public LocalDateTime parseValue(Object input) {
7070
LocalDateTime result = convertImpl(input);
7171
if (result == null) {
72-
throw new CoercingParseValueException("Invalid value '" + input + "' for LocalDateTime");
72+
throw new CoercingParseValueException(getErrorMessage(input));
7373
}
7474
return result;
7575
}
@@ -83,10 +83,14 @@ public LocalDateTime parseLiteral(Object input) {
8383
String value = ((StringValue) input).getValue();
8484
LocalDateTime result = convertImpl(value);
8585
if (result == null) {
86-
throw new CoercingParseLiteralException("Invalid value '" + input + "' for LocalDateTime");
86+
throw new CoercingParseLiteralException(getErrorMessage(input));
8787
}
8888

8989
return result;
9090
}
9191

92+
private String getErrorMessage(Object input) {
93+
return "Invalid value '" + input + "' for LocalDateTime";
94+
}
95+
9296
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlLocalTimeCoercing.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,26 @@ public class GraphqlLocalTimeCoercing implements Coercing<LocalTime, String> {
3636
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_TIME.withZone(ZoneOffset.UTC);
3737

3838
private LocalTime convertImpl(Object input) {
39-
if (input instanceof String) {
39+
if (input instanceof String string) {
4040
try {
41-
return LocalTime.parse((String) input, FORMATTER);
41+
return LocalTime.parse(string, FORMATTER);
4242
} catch (DateTimeParseException ignored) {
4343
// nothing to-do
4444
}
45-
} else if (input instanceof LocalTime) {
46-
return (LocalTime) input;
45+
} else if (input instanceof LocalTime localTime) {
46+
return localTime;
4747
}
4848
return null;
4949
}
5050

5151
@Override
5252
public String serialize(Object input) {
53-
if (input instanceof LocalTime) {
54-
return DateTimeHelper.toISOString((LocalTime) input);
53+
if (input instanceof LocalTime localTime) {
54+
return DateTimeHelper.toISOString(localTime);
5555
} else {
5656
LocalTime result = convertImpl(input);
5757
if (result == null) {
58-
throw new CoercingSerializeException("Invalid value '" + input + "' for LocalTime");
58+
throw new CoercingSerializeException(getErrorMessage(input));
5959
}
6060
return DateTimeHelper.toISOString(result);
6161
}
@@ -65,7 +65,7 @@ public String serialize(Object input) {
6565
public LocalTime parseValue(Object input) {
6666
LocalTime result = convertImpl(input);
6767
if (result == null) {
68-
throw new CoercingParseValueException("Invalid value '" + input + "' for LocalTime");
68+
throw new CoercingParseValueException(getErrorMessage(input));
6969
}
7070
return result;
7171
}
@@ -79,10 +79,14 @@ public LocalTime parseLiteral(Object input) {
7979
String value = ((StringValue) input).getValue();
8080
LocalTime result = convertImpl(value);
8181
if (result == null) {
82-
throw new CoercingParseLiteralException("Invalid value '" + input + "' for LocalTime");
82+
throw new CoercingParseLiteralException(getErrorMessage(input));
8383
}
8484

8585
return result;
8686
}
8787

88+
private String getErrorMessage(Object input) {
89+
return "Invalid value '" + input + "' for LocalTime";
90+
}
91+
8892
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlOffsetDateTimeCoercing.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@
3333
public class GraphqlOffsetDateTimeCoercing implements Coercing<OffsetDateTime, String> {
3434

3535
private OffsetDateTime convertImpl(Object input) {
36-
if (input instanceof String) {
36+
if (input instanceof String string) {
3737
try {
38-
return OffsetDateTime.parse((String) input, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
38+
return OffsetDateTime.parse(string, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
3939
} catch (DateTimeParseException ignored) {
4040
// nothing to-do
4141
}
42-
} else if (input instanceof OffsetDateTime) {
43-
return (OffsetDateTime) input;
42+
} else if (input instanceof OffsetDateTime offsetDateTime) {
43+
return offsetDateTime;
4444
}
4545
return null;
4646
}
4747

4848
@Override
4949
public String serialize(Object input) {
50-
if (input instanceof OffsetDateTime) {
51-
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format((OffsetDateTime) input);
50+
if (input instanceof OffsetDateTime offsetDateTime) {
51+
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
5252
} else {
5353
OffsetDateTime result = convertImpl(input);
5454
if (result == null) {
55-
throw new CoercingSerializeException("Invalid value '" + input + "' for OffsetDateTime");
55+
throw new CoercingSerializeException(getErrorMessage(input));
5656
}
5757
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(result);
5858
}
@@ -62,7 +62,7 @@ public String serialize(Object input) {
6262
public OffsetDateTime parseValue(Object input) {
6363
OffsetDateTime result = convertImpl(input);
6464
if (result == null) {
65-
throw new CoercingParseValueException("Invalid value '" + input + "' for OffsetDateTime");
65+
throw new CoercingParseValueException(getErrorMessage(input));
6666
}
6767
return result;
6868
}
@@ -76,10 +76,14 @@ public OffsetDateTime parseLiteral(Object input) {
7676
String value = ((StringValue) input).getValue();
7777
OffsetDateTime result = convertImpl(value);
7878
if (result == null) {
79-
throw new CoercingParseLiteralException("Invalid value '" + input + "' for OffsetDateTime");
79+
throw new CoercingParseLiteralException(getErrorMessage(input));
8080
}
8181

8282
return result;
8383
}
8484

85+
private String getErrorMessage(Object input) {
86+
return "Invalid value '" + input + "' for OffsetDateTime";
87+
}
88+
8589
}

0 commit comments

Comments
 (0)