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

generator.server.springboot.database.cassandra.CassandraJSR310DateConverters.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package {{ packageName }}.wire.cassandra.infrastructure.secondary;

import com.datastax.oss.driver.api.core.data.TupleValue;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.TupleType;
import jakarta.annotation.Nonnull;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;

public final class CassandraJSR310DateConverters {

  private CassandraJSR310DateConverters() {}

  @ReadingConverter
  public static final class TupleToZonedDateTimeConverter implements Converter {

    public static final TupleToZonedDateTimeConverter INSTANCE = new TupleToZonedDateTimeConverter();

    private TupleToZonedDateTimeConverter() {}

    @Override
    public ZonedDateTime convert(TupleValue source) {
      Instant instant = source.getInstant(0);
      ZoneId zoneId = ZoneId.of(source.getString(1));
      return instant.atZone(zoneId);
    }
  }

  @WritingConverter
  public static final class ZonedDateTimeToTupleConverter implements Converter {

    public static final ZonedDateTimeToTupleConverter INSTANCE = new ZonedDateTimeToTupleConverter();

    private final TupleType type = DataTypes.tupleOf(DataTypes.TIMESTAMP, DataTypes.TEXT);

    private ZonedDateTimeToTupleConverter() {}

    @Override
    public TupleValue convert(@Nonnull ZonedDateTime source) {
      TupleValue tupleValue = type.newValue();
      tupleValue.setInstant(0, source.toInstant());
      tupleValue.setString(1, source.getZone().toString());
      return tupleValue;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy