All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.socialsignin.spring.data.dynamodb.marshaller.Instant2IsoDynamoDBMarshaller Maven / Gradle / Ivy

Go to download

The primary goal of the Spring® Data project is to make it easier to build Spring-powered applications that use data access technologies. This module deals with enhanced support for a data access layer built on AWS DynamoDB.

The newest version!
/**
 * Copyright © 2018 spring-data-dynamodb (https://github.com/boostchicken/spring-data-dynamodb)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.socialsignin.spring.data.dynamodb.marshaller;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshaller;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;
import org.springframework.util.StringUtils;

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@SuppressWarnings("deprecation")
public class Instant2IsoDynamoDBMarshaller
		implements
			DynamoDBTypeConverter,
			DynamoDBMarshaller {

	private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

	private DateTimeFormatter getDateFormat() {
		return DateTimeFormatter.ofPattern(PATTERN).withZone(ZoneOffset.UTC);
	}

	@Override
	public String convert(Instant object) {
		return marshall(object);
	}

	@Override
	public String marshall(Instant getterReturnResult) {
		if (getterReturnResult == null) {
			return null;
		} else {
			return getDateFormat().format(getterReturnResult);
		}
	}

	@Override
	public Instant unconvert(String object) {
		return unmarshall(Instant.class, object);
	}

	@Override
	public Instant unmarshall(Class clazz, String obj) {
		if (StringUtils.isEmpty(obj)) {
			return null;
		} else {
			return Instant.from(getDateFormat().parse(obj));
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy