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

com.datastax.driver.extras.codecs.date.SimpleDateCodec Maven / Gradle / Ivy

There is a newer version: 4.15.102
Show newest version
/*
 * Copyright DataStax, Inc.
 *
 * This software can be used solely with DataStax Enterprise. Please consult the license at
 * http://www.datastax.com/terms/datastax-dse-driver-license-terms
 */
package com.datastax.driver.extras.codecs.date;

import static com.datastax.driver.core.CodecUtils.*;
import static com.datastax.driver.core.ParseUtils.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import com.datastax.driver.core.*;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.util.Date;

/**
 * A {@link TypeCodec} that maps CQL dates to Java primitive ints, representing the number of days
 * since the Epoch.
 *
 * 

This codec can serve as a replacement for the driver's built-in {@link TypeCodec#date() date} * codec, when application code prefers to deal with raw days than with {@link LocalDate} instances. * *

Important: this codec cannot work with {@link SimpleStatement}s! If you try * to insert CQL date values as {@code int}s in a {@link SimpleStatement}, the insertion would * succeed but the values wouldn't be properly encoded. Always use {@link PreparedStatement}s when * using this codec. */ public class SimpleDateCodec extends TypeCodec.PrimitiveIntCodec { public static final SimpleDateCodec instance = new SimpleDateCodec(); private static final String pattern = "yyyy-MM-dd"; public SimpleDateCodec() { super(DataType.date()); } @Override public ByteBuffer serializeNoBoxing(int value, ProtocolVersion protocolVersion) { return cint().serializeNoBoxing(fromSignedToUnsignedInt(value), protocolVersion); } @Override public int deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) return 0; return fromUnsignedToSignedInt(cint().deserializeNoBoxing(bytes, protocolVersion)); } @Override public Integer parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // single quotes are optional for long literals, mandatory for date patterns // strip enclosing single quotes, if any if (isQuoted(value)) value = unquote(value); if (isLongLiteral(value)) { long unsigned; try { unsigned = Long.parseLong(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse date value from \"%s\"", value), e); } try { return fromCqlDateToDaysSinceEpoch(unsigned); } catch (IllegalArgumentException e) { throw new InvalidTypeException( String.format("Cannot parse date value from \"%s\"", value), e); } } try { Date date = parseDate(value, pattern); return (int) MILLISECONDS.toDays(date.getTime()); } catch (ParseException e) { throw new InvalidTypeException( String.format("Cannot parse date value from \"%s\"", value), e); } } @Override public String format(Integer value) { if (value == null) return "NULL"; long raw = fromDaysSinceEpochToCqlDate(value); return quote(Long.toString(raw)); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy