Skip to content

Commit b3fe164

Browse files
author
Jörg Hälker
committed
1 parent 87b80ee commit b3fe164

File tree

5 files changed

+178
-23
lines changed

5 files changed

+178
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2015-2016 Canoo Engineering AG.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dev.rico.internal.remoting.converters;
17+
18+
19+
import dev.rico.remoting.converter.Converter;
20+
import dev.rico.remoting.converter.ValueConverterException;
21+
22+
import java.time.Instant;
23+
import java.time.format.DateTimeFormatter;
24+
import java.util.Collections;
25+
import java.util.List;
26+
27+
/**
28+
* Created by hendrikebbers on 25.10.16.
29+
*/
30+
public class InstantConverterFactory extends AbstractConverterFactory {
31+
32+
@SuppressWarnings("rawtypes")
33+
private final static Converter CONVERTER = new InstantConverter();
34+
35+
@Override
36+
public boolean supportsType(Class<?> cls) {
37+
return Instant.class.isAssignableFrom(cls);
38+
}
39+
40+
@Override
41+
public List<Class> getSupportedTypes() {
42+
return Collections.singletonList(Instant.class);
43+
}
44+
45+
@Override
46+
public int getTypeIdentifier() {
47+
return 1000;
48+
}
49+
50+
@Override
51+
public Converter getConverterForType(Class<?> cls) {
52+
return CONVERTER;
53+
}
54+
55+
private static class InstantConverter extends AbstractStringConverter<Instant> {
56+
57+
@Override
58+
public Instant convertFromRemoting(String value) throws ValueConverterException {
59+
if (value == null) {
60+
return null;
61+
}
62+
try {
63+
return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value));
64+
} catch (Exception e) {
65+
throw new ValueConverterException("Can not convert to Instant", e);
66+
}
67+
}
68+
69+
@Override
70+
public String convertToRemoting(Instant value) throws ValueConverterException {
71+
if (value == null) {
72+
return null;
73+
}
74+
try {
75+
return value.toString();
76+
} catch (Exception e) {
77+
throw new ValueConverterException("Can not convert from Instant", e);
78+
}
79+
}
80+
}
81+
82+
}

remoting/rico-remoting-common/src/main/java/dev/rico/internal/remoting/converters/LocalDateConverterFactory.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@
2020
import dev.rico.remoting.converter.ValueConverterException;
2121
import org.apiguardian.api.API;
2222

23-
import java.text.DateFormat;
24-
import java.text.SimpleDateFormat;
2523
import java.time.LocalDate;
26-
import java.time.ZoneId;
2724
import java.time.ZoneOffset;
2825
import java.util.*;
2926

30-
import static dev.rico.internal.remoting.RemotingConstants.REMOTING_DATE_FORMAT_PATTERN;
31-
import static dev.rico.internal.core.RicoConstants.TIMEZONE_UTC;
3227
import static org.apiguardian.api.API.Status.INTERNAL;
3328

3429
@API(since = "0.x", status = INTERNAL)
@@ -66,9 +61,9 @@ public LocalDate convertFromRemoting(final String value)
6661
return null;
6762
}
6863
try {
69-
final Calendar result = Calendar.getInstance(TimeZone.getDefault());
64+
final Calendar result = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
7065
result.setTime(getDateFormat().parse(value));
71-
return result.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
66+
return result.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
7267
} catch (final Exception e) {
7368
throw new ValueConverterException(
7469
"Can not convert to LocalDate", e);
@@ -82,8 +77,9 @@ public String convertToRemoting(final LocalDate value)
8277
return null;
8378
}
8479
try {
85-
final Date date = Date.from(value.atStartOfDay().toInstant(ZoneOffset.ofHours(0)));
86-
return getDateFormat().format(date);
80+
final Date date = Date.from(value.atStartOfDay().toInstant(ZoneOffset.UTC));
81+
String format = getDateFormat().format(date);
82+
return format;
8783
} catch (final Exception e) {
8884
throw new ValueConverterException(
8985
"Can not convert from LocalDate", e);

remoting/rico-remoting-common/src/main/java/dev/rico/internal/remoting/converters/LocalDateTimeConverterFactory.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@
2020
import dev.rico.remoting.converter.ValueConverterException;
2121
import org.apiguardian.api.API;
2222

23-
import java.text.DateFormat;
24-
import java.text.SimpleDateFormat;
2523
import java.time.LocalDateTime;
26-
import java.time.OffsetDateTime;
27-
import java.time.ZoneId;
24+
import java.time.ZoneOffset;
2825
import java.util.*;
2926

30-
import static dev.rico.internal.remoting.RemotingConstants.REMOTING_DATE_FORMAT_PATTERN;
3127
import static dev.rico.internal.core.RicoConstants.TIMEZONE_UTC;
3228
import static org.apiguardian.api.API.Status.INTERNAL;
3329

@@ -66,7 +62,7 @@ public LocalDateTime convertFromRemoting(final String value) throws ValueConvert
6662
try {
6763
final Calendar result = Calendar.getInstance(TimeZone.getTimeZone(TIMEZONE_UTC));
6864
result.setTime(getDateFormat().parse(value));
69-
return result.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
65+
return result.toInstant().atZone(ZoneOffset.UTC).toLocalDateTime();
7066
} catch (final Exception e) {
7167
throw new ValueConverterException("Can not convert to LocalDateTime", e);
7268
}
@@ -78,7 +74,7 @@ public String convertToRemoting(final LocalDateTime value) throws ValueConverter
7874
return null;
7975
}
8076
try {
81-
final Date date = Date.from(value.toInstant(OffsetDateTime.now().getOffset()));
77+
final Date date = Date.from(value.toInstant(ZoneOffset.UTC));
8278
return getDateFormat().format(date);
8379
} catch (final Exception e) {
8480
throw new ValueConverterException("Can not convert from LocalDateTime", e);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2015-2016 Canoo Engineering AG.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dev.rico.internal.remoting.converters;
17+
18+
19+
import dev.rico.remoting.converter.Converter;
20+
import dev.rico.remoting.converter.ValueConverterException;
21+
22+
import java.time.LocalTime;
23+
import java.time.format.DateTimeFormatter;
24+
import java.util.Collections;
25+
import java.util.List;
26+
27+
/**
28+
* Created by hendrikebbers on 25.10.16.
29+
*/
30+
public class LocalTimeConverterFactory extends AbstractConverterFactory {
31+
32+
@SuppressWarnings("rawtypes")
33+
private final static Converter CONVERTER = new LocalTimeConverter();
34+
35+
@Override
36+
public boolean supportsType(Class<?> cls) {
37+
return LocalTime.class.isAssignableFrom(cls);
38+
}
39+
40+
@Override
41+
public List<Class> getSupportedTypes() {
42+
return Collections.singletonList(LocalTime.class);
43+
}
44+
45+
@Override
46+
public int getTypeIdentifier() {
47+
return 1200;
48+
}
49+
50+
@Override
51+
public Converter getConverterForType(Class<?> cls) {
52+
return CONVERTER;
53+
}
54+
55+
private static class LocalTimeConverter extends AbstractStringConverter<LocalTime> {
56+
57+
@Override
58+
public LocalTime convertFromRemoting(String value) throws ValueConverterException {
59+
if (value == null) {
60+
return null;
61+
}
62+
try {
63+
return LocalTime.from(DateTimeFormatter.ISO_TIME.parse(value));
64+
} catch (Exception e) {
65+
throw new ValueConverterException("Can not convert to LocalTime", e);
66+
}
67+
}
68+
69+
@Override
70+
public String convertToRemoting(LocalTime value) throws ValueConverterException {
71+
if (value == null) {
72+
return null;
73+
}
74+
try {
75+
return value.toString();
76+
} catch (Exception e) {
77+
throw new ValueConverterException("Can not convert from LocalTime", e);
78+
}
79+
}
80+
}
81+
82+
}

remoting/rico-remoting-common/src/main/java/dev/rico/internal/remoting/converters/ZonedDateTimeConverterFactory.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import dev.rico.remoting.converter.ValueConverterException;
2121
import org.apiguardian.api.API;
2222

23-
import java.text.DateFormat;
24-
import java.text.SimpleDateFormat;
25-
import java.time.ZoneId;
23+
import java.time.ZoneOffset;
2624
import java.time.ZonedDateTime;
27-
import java.util.*;
25+
import java.util.Calendar;
26+
import java.util.Collections;
27+
import java.util.GregorianCalendar;
28+
import java.util.List;
2829

29-
import static dev.rico.internal.remoting.RemotingConstants.REMOTING_DATE_FORMAT_PATTERN;
30-
import static dev.rico.internal.core.RicoConstants.TIMEZONE_UTC;
3130
import static org.apiguardian.api.API.Status.INTERNAL;
3231

3332
@API(since = "0.x", status = INTERNAL)
@@ -65,7 +64,7 @@ public ZonedDateTime convertFromRemoting(final String value) throws ValueConvert
6564
try {
6665
final Calendar result = Calendar.getInstance();
6766
result.setTime(getDateFormat().parse(value));
68-
return result.toInstant().atZone(ZoneId.systemDefault());
67+
return result.toInstant().atZone(ZoneOffset.UTC);
6968
} catch (final Exception e) {
7069
throw new ValueConverterException("Can not convert to ZonedDateTime", e);
7170
}

0 commit comments

Comments
 (0)