View Javadoc

1   /**
2    * Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
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 org.socialsignin.spring.data.dynamodb.marshaller;
17  
18  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshaller;
19  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;
20  import org.springframework.util.StringUtils;
21  
22  import java.text.DateFormat;
23  import java.text.ParseException;
24  import java.util.Date;
25  
26  @SuppressWarnings("deprecation")
27  public abstract class DateDynamoDBMarshaller implements DynamoDBTypeConverter<String, Date>, DynamoDBMarshaller<Date> {
28  
29  	public abstract DateFormat getDateFormat();
30  
31  	@Override
32  	public String convert(Date object) {
33  		return marshall(object);
34  	}
35  
36  	@Override
37  	public String marshall(Date getterReturnResult) {
38  		if (getterReturnResult == null) {
39  			return null;
40  		} else {
41  			return getDateFormat().format(getterReturnResult);
42  		}
43  	}
44  
45  	@Override
46  	public Date unconvert(String object) {
47  		return unmarshall(Date.class, object);
48  	}
49  
50  	@Override
51  	public Date unmarshall(Class<Date> clazz, String obj) {
52  		if (StringUtils.isEmpty(obj)) {
53  			return null;
54  		} else {
55  			try {
56  				return getDateFormat().parse(obj);
57  			} catch (ParseException e) {
58  				throw new RuntimeException(e);
59  			}
60  		}
61  	}
62  
63  }