1 /*
2  * Copyright 2013-2020 the original author or authors.
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  *      https://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 org.springframework.data.convert;
17
18 import java.time.Instant;
19 import java.time.ZoneId;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.List;
25
26 import javax.annotation.Nonnull;
27
28 import org.joda.time.DateTime;
29 import org.joda.time.LocalDate;
30 import org.joda.time.LocalDateTime;
31 import org.springframework.core.convert.converter.Converter;
32 import org.springframework.util.ClassUtils;
33
34 /**
35  * Helper class to register JodaTime specific {@link Converter} implementations in case the library is present on the
36  * classpath.
37  *
38  * @author Oliver Gierke
39  * @author Christoph Strobl
40  * @author Jens Schauder
41  * @author Mark Paluch
42  * @deprecated since 2.3, use JSR-310 types as replacement for Joda-Time.
43  */

44 @Deprecated
45 public abstract class JodaTimeConverters {
46
47     private static final boolean JODA_TIME_IS_PRESENT = ClassUtils.isPresent("org.joda.time.LocalDate"null);
48
49     /**
50      * Returns the converters to be registered. Will only return converters in case JodaTime is present on the class.
51      *
52      * @return
53      */

54     public static Collection<Converter<?, ?>> getConvertersToRegister() {
55
56         if (!JODA_TIME_IS_PRESENT) {
57             return Collections.emptySet();
58         }
59
60         List<Converter<?, ?>> converters = new ArrayList<>();
61         converters.add(LocalDateToDateConverter.INSTANCE);
62         converters.add(LocalDateTimeToDateConverter.INSTANCE);
63         converters.add(DateTimeToDateConverter.INSTANCE);
64
65         converters.add(DateToLocalDateConverter.INSTANCE);
66         converters.add(DateToLocalDateTimeConverter.INSTANCE);
67         converters.add(DateToDateTimeConverter.INSTANCE);
68
69         converters.add(LocalDateTimeToJodaLocalDateTime.INSTANCE);
70         converters.add(LocalDateTimeToJodaDateTime.INSTANCE);
71
72         converters.add(InstantToJodaLocalDateTime.INSTANCE);
73         converters.add(JodaLocalDateTimeToInstant.INSTANCE);
74
75         converters.add(LocalDateTimeToJsr310Converter.INSTANCE);
76
77         return converters;
78     }
79
80     @Deprecated
81     public enum LocalDateTimeToJsr310Converter implements Converter<LocalDateTime, java.time.LocalDateTime> {
82
83         INSTANCE;
84
85         @Nonnull
86         @Override
87         public java.time.LocalDateTime convert(LocalDateTime source) {
88             return java.time.LocalDateTime.ofInstant(source.toDate().toInstant(), ZoneId.systemDefault());
89         }
90     }
91
92     @Deprecated
93     public enum LocalDateToDateConverter implements Converter<LocalDate, Date> {
94
95         INSTANCE;
96
97         @Nonnull
98         @Override
99         public Date convert(LocalDate source) {
100             return source.toDate();
101         }
102     }
103
104     @Deprecated
105     public enum LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> {
106
107         INSTANCE;
108
109         @Nonnull
110         @Override
111         public Date convert(LocalDateTime source) {
112             return source.toDate();
113         }
114     }
115
116     @Deprecated
117     public enum DateTimeToDateConverter implements Converter<DateTime, Date> {
118
119         INSTANCE;
120
121         @Nonnull
122         @Override
123         public Date convert(DateTime source) {
124             return source.toDate();
125         }
126     }
127
128     @Deprecated
129     public enum DateToLocalDateConverter implements Converter<Date, LocalDate> {
130
131         INSTANCE;
132
133         @Nonnull
134         @Override
135         public LocalDate convert(Date source) {
136             return new LocalDate(source.getTime());
137         }
138     }
139
140     @Deprecated
141     public enum DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
142
143         INSTANCE;
144
145         @Nonnull
146         @Override
147         public LocalDateTime convert(Date source) {
148             return new LocalDateTime(source.getTime());
149         }
150     }
151
152     @Deprecated
153     public enum DateToDateTimeConverter implements Converter<Date, DateTime> {
154
155         INSTANCE;
156
157         @Nonnull
158         @Override
159         public DateTime convert(Date source) {
160             return new DateTime(source.getTime());
161         }
162     }
163
164     @ReadingConverter
165     @Deprecated
166     public enum LocalDateTimeToJodaLocalDateTime implements Converter<java.time.LocalDateTime, LocalDateTime> {
167
168         INSTANCE;
169
170         @Nonnull
171         @Override
172         public LocalDateTime convert(java.time.LocalDateTime source) {
173             return LocalDateTime.fromDateFields(Jsr310Converters.LocalDateTimeToDateConverter.INSTANCE.convert(source));
174         }
175     }
176
177     @Deprecated
178     public enum InstantToJodaLocalDateTime implements Converter<java.time.Instant, LocalDateTime> {
179
180         INSTANCE;
181
182         @Nonnull
183         @Override
184         public LocalDateTime convert(java.time.Instant source) {
185             return LocalDateTime.fromDateFields(new Date(source.toEpochMilli()));
186         }
187     }
188
189     @Deprecated
190     public enum JodaLocalDateTimeToInstant implements Converter<LocalDateTime, Instant> {
191
192         INSTANCE;
193
194         @Nonnull
195         @Override
196         public Instant convert(LocalDateTime source) {
197             return Instant.ofEpochMilli(source.toDateTime().getMillis());
198         }
199     }
200
201     @Deprecated
202     public enum LocalDateTimeToJodaDateTime implements Converter<java.time.LocalDateTime, DateTime> {
203
204         INSTANCE;
205
206         @Nonnull
207         @Override
208         public DateTime convert(java.time.LocalDateTime source) {
209             return new DateTime(Jsr310Converters.LocalDateTimeToDateConverter.INSTANCE.convert(source));
210         }
211     }
212 }
213