Skip to content

Commit

Permalink
Selection for localdatetime and datetime.
Browse files Browse the repository at this point in the history
  • Loading branch information
sherfert committed Feb 26, 2018
1 parent c595081 commit 7c6c995
Show file tree
Hide file tree
Showing 16 changed files with 418 additions and 170 deletions.
Expand Up @@ -76,7 +76,7 @@ protected DateTimeValue build( MapValue map, Supplier<ZoneId> defaultZone )
@Override @Override
protected DateTimeValue select( AnyValue from, Supplier<ZoneId> defaultZone ) protected DateTimeValue select( AnyValue from, Supplier<ZoneId> defaultZone )
{ {
throw new UnsupportedOperationException( ); return DateTimeValue.select( from, defaultZone );
} }


@Override @Override
Expand Down
Expand Up @@ -38,7 +38,7 @@ static <T> T build( StructureBuilder<AnyValue,T> builder, Iterable<Map.Entry<Str
{ {
for ( Map.Entry<String,AnyValue> entry : entries ) for ( Map.Entry<String,AnyValue> entry : entries )
{ {
builder.add( entry.getKey(), entry.getValue() ); builder = builder.add( entry.getKey(), entry.getValue() );
} }
return builder.build(); return builder.build();
} }
Expand Down
Expand Up @@ -141,6 +141,11 @@ public static DateTimeValue build( MapValue map, Supplier<ZoneId> defaultZone )
return StructureBuilder.build( builder( defaultZone ), map ); return StructureBuilder.build( builder( defaultZone ), map );
} }


public static DateTimeValue select( AnyValue from, Supplier<ZoneId> defaultZone )
{
return builder( defaultZone ).selectDateTime( from );
}

public static DateTimeValue truncate( public static DateTimeValue truncate(
TemporalUnit unit, TemporalUnit unit,
TemporalValue input, TemporalValue input,
Expand All @@ -150,7 +155,7 @@ public static DateTimeValue truncate(
throw new UnsupportedOperationException( "not implemented" ); throw new UnsupportedOperationException( "not implemented" );
} }


static StructureBuilder<AnyValue,DateTimeValue> builder( Supplier<ZoneId> defaultZone ) static DateTimeBuilder<DateTimeValue> builder( Supplier<ZoneId> defaultZone )
{ {
return new DateTimeBuilder<DateTimeValue>( defaultZone ) return new DateTimeBuilder<DateTimeValue>( defaultZone )
{ {
Expand All @@ -161,34 +166,81 @@ static StructureBuilder<AnyValue,DateTimeValue> builder( Supplier<ZoneId> defaul
@Override @Override
public DateTimeValue buildInternal() public DateTimeValue buildInternal()
{ {
boolean selectingDate = fields.containsKey( Field.date );
boolean selectingTime = fields.containsKey( Field.time );
boolean selectingDateTime = fields.containsKey( Field.datetime );
ZonedDateTime result; ZonedDateTime result;
if ( fields.containsKey( Field.time ) || fields.containsKey( Field.date ) || fields.containsKey( Field.datetime ) ) if ( selectingDateTime )
{ {
// AnyValue time = fields.get( Field.time ); AnyValue dtField = fields.get( Field.datetime );
// if ( !(time instanceof TemporalValue) ) if ( !(dtField instanceof TemporalValue) )
// { {
// throw new IllegalArgumentException( String.format( "Cannot construct local date time from: %s", time ) ); throw new IllegalArgumentException( String.format( "Cannot construct date time from: %s", dtField ) );
// } }
// TODO select date, time, or both TemporalValue dt = (TemporalValue) dtField;
throw new UnsupportedOperationException(); OffsetTime timePart = dt.getTimePart( defaultZone );
result = ZonedDateTime.of( dt.getDatePart(), timePart.toLocalTime(), timePart.getOffset() );
} }
else if ( fields.containsKey( Field.week ) ) else if ( selectingTime || selectingDate )
{ {
// Be sure to be in the start of the week based year (which can be later than 1st Jan) OffsetTime time;
result = defaulZonedDateTime.with( IsoFields.WEEK_BASED_YEAR, if ( selectingTime )
safeCastIntegral( Field.year.name(), fields.get( Field.year ), Field.year.defaultValue ) ).with( IsoFields.WEEK_OF_WEEK_BASED_YEAR, {
1 ).with( ChronoField.DAY_OF_WEEK, 1 ); AnyValue timeField = fields.get( Field.time );
if ( !(timeField instanceof TemporalValue) )
{
throw new IllegalArgumentException( String.format( "Cannot construct local time from: %s", timeField ) );
}
TemporalValue t = (TemporalValue) timeField;
time = t.getTimePart( defaultZone );
}
else
{
time = TimeValue.defaultTime( timezone() );
}
LocalDate date;
if ( selectingDate )
{
AnyValue dateField = fields.get( Field.date );
if ( !(dateField instanceof TemporalValue) )
{
throw new IllegalArgumentException( String.format( "Cannot construct date from: %s", dateField ) );
}
TemporalValue t = (TemporalValue) dateField;
date = t.getDatePart();
}
else
{
date = DateValue.DEFAULT_CALENDER_DATE;
}
result = ZonedDateTime.of( date, time.toLocalTime(), time.getOffset() );
} }
else else
{ {
result = defaulZonedDateTime; result = defaulZonedDateTime;
} }


if ( fields.containsKey( Field.week ) && !selectingDate && !selectingDateTime )
{
// Be sure to be in the start of the week based year (which can be later than 1st Jan)
result = result
.with( IsoFields.WEEK_BASED_YEAR, safeCastIntegral( Field.year.name(), fields.get( Field.year ),
Field.year.defaultValue ) )
.with( IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1 )
.with( ChronoField.DAY_OF_WEEK, 1 );
}

result = assignAllFields( result ); result = assignAllFields( result );
if ( timezone != null ) if ( timezone != null )
{ {
// TODO this will be different when selecting if ( selectingTime || selectingDateTime )
result = result.withZoneSameLocal( timezone() ); {
result = result.withZoneSameInstant( timezone() );
}
else
{
result = result.withZoneSameLocal( timezone() );
}
} }
return datetime( result ); return datetime( result );
} }
Expand Down
Expand Up @@ -377,6 +377,8 @@ private static LocalDate localQuarterDate( int year, int quarter, int dayOfQuart
.with( IsoFields.DAY_OF_QUARTER, dayOfQuarter ); .with( IsoFields.DAY_OF_QUARTER, dayOfQuarter );
} }


static final LocalDate DEFAULT_CALENDER_DATE = LocalDate.of( Field.year.defaultValue, Field.month.defaultValue, Field.day.defaultValue );

private static class DateBuilder extends Builder<DateValue> private static class DateBuilder extends Builder<DateValue>
{ {
DateBuilder( Supplier<ZoneId> defaultZone ) DateBuilder( Supplier<ZoneId> defaultZone )
Expand Down Expand Up @@ -406,8 +408,6 @@ private LocalDate getDateOf( org.neo4j.values.AnyValue temporal )
throw new IllegalArgumentException( String.format( "Cannot construct date from: %s", temporal ) ); throw new IllegalArgumentException( String.format( "Cannot construct date from: %s", temporal ) );
} }


private final LocalDate defaulCalenderDate = LocalDate.of( Field.year.defaultValue, Field.month.defaultValue, Field.day.defaultValue );

@Override @Override
public DateValue buildInternal() public DateValue buildInternal()
{ {
Expand All @@ -419,15 +419,15 @@ public DateValue buildInternal()
else if ( fields.containsKey( Field.week ) ) else if ( fields.containsKey( Field.week ) )
{ {
// Be sure to be in the start of the week based year (which can be later than 1st Jan) // Be sure to be in the start of the week based year (which can be later than 1st Jan)
result = defaulCalenderDate result = DEFAULT_CALENDER_DATE
.with( IsoFields.WEEK_BASED_YEAR, safeCastIntegral( Field.year.name(), fields.get( Field.year ), .with( IsoFields.WEEK_BASED_YEAR, safeCastIntegral( Field.year.name(), fields.get( Field.year ),
Field.year.defaultValue ) ) Field.year.defaultValue ) )
.with( IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1 ) .with( IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1 )
.with( ChronoField.DAY_OF_WEEK, 1 ); .with( ChronoField.DAY_OF_WEEK, 1 );
} }
else else
{ {
result = defaulCalenderDate; result = DEFAULT_CALENDER_DATE;
} }
result = assignAllFields( result ); result = assignAllFields( result );
return date( result ); return date( result );
Expand Down
Expand Up @@ -119,41 +119,80 @@ public static LocalDateTimeValue truncate(
throw new UnsupportedOperationException( "not implemented" ); throw new UnsupportedOperationException( "not implemented" );
} }


static final LocalDateTime DEFAULT_LOCAL_DATE_TIME =
LocalDateTime.of( Field.year.defaultValue, Field.month.defaultValue, Field.day.defaultValue, Field.hour.defaultValue,
Field.minute.defaultValue );

static DateTimeValue.DateTimeBuilder<LocalDateTimeValue> builder( Supplier<ZoneId> defaultZone ) static DateTimeValue.DateTimeBuilder<LocalDateTimeValue> builder( Supplier<ZoneId> defaultZone )
{ {
return new DateTimeValue.DateTimeBuilder<LocalDateTimeValue>( defaultZone ) return new DateTimeValue.DateTimeBuilder<LocalDateTimeValue>( defaultZone )
{ {
private final LocalDateTime defaulLocalDateTime =
LocalDateTime.of( Field.year.defaultValue, Field.month.defaultValue, Field.day.defaultValue, Field.hour.defaultValue,
Field.minute.defaultValue );

@Override @Override
public LocalDateTimeValue buildInternal() public LocalDateTimeValue buildInternal()
{ {
boolean selectingDate = fields.containsKey( Field.date );
boolean selectingTime = fields.containsKey( Field.time );
boolean selectingDateTime = fields.containsKey( Field.datetime );
LocalDateTime result; LocalDateTime result;
if ( fields.containsKey( Field.time ) || fields.containsKey( Field.date ) || fields.containsKey( Field.datetime ) ) if ( selectingDateTime )
{
AnyValue dtField = fields.get( Field.datetime );
if ( !(dtField instanceof TemporalValue) )
{
throw new IllegalArgumentException( String.format( "Cannot construct local date time from: %s", dtField ) );
}
TemporalValue dt = (TemporalValue) dtField;
result = LocalDateTime.of( dt.getDatePart(), dt.getLocalTimePart() );
}
else if ( selectingTime || selectingDate )
{ {
// AnyValue time = fields.get( Field.time ); LocalTime time;
// if ( !(time instanceof TemporalValue) ) if ( selectingTime )
// { {
// throw new IllegalArgumentException( String.format( "Cannot construct local date time from: %s", time ) ); AnyValue timeField = fields.get( Field.time );
// } if ( !(timeField instanceof TemporalValue) )
// TODO select date, time, or both {
throw new UnsupportedOperationException( ); throw new IllegalArgumentException( String.format( "Cannot construct local time from: %s", timeField ) );
}
TemporalValue t = (TemporalValue) timeField;
time = t.getLocalTimePart();
}
else
{
time = LocalTimeValue.DEFAULT_LOCAL_TIME;
}
LocalDate date;
if ( selectingDate )
{
AnyValue dateField = fields.get( Field.date );
if ( !(dateField instanceof TemporalValue) )
{
throw new IllegalArgumentException( String.format( "Cannot construct date from: %s", dateField ) );
}
TemporalValue t = (TemporalValue) dateField;
date = t.getDatePart();
}
else
{
date = DateValue.DEFAULT_CALENDER_DATE;
}

result = LocalDateTime.of( date, time );
} }
else if ( fields.containsKey( Field.week ) ) else
{
result = DEFAULT_LOCAL_DATE_TIME;
}

if ( fields.containsKey( Field.week ) && !selectingDate && !selectingDateTime )
{ {
// Be sure to be in the start of the week based year (which can be later than 1st Jan) // Be sure to be in the start of the week based year (which can be later than 1st Jan)
result = defaulLocalDateTime result = result
.with( IsoFields.WEEK_BASED_YEAR, safeCastIntegral( Field.year.name(), fields.get( Field.year ), .with( IsoFields.WEEK_BASED_YEAR, safeCastIntegral( Field.year.name(), fields.get( Field.year ),
Field.year.defaultValue ) ) Field.year.defaultValue ) )
.with( IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1 ) .with( IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1 )
.with( ChronoField.DAY_OF_WEEK, 1 ); .with( ChronoField.DAY_OF_WEEK, 1 );
} }
else
{
result = defaulLocalDateTime;
}


result = assignAllFields( result ); result = assignAllFields( result );
return localDateTime( result ); return localDateTime( result );
Expand Down
Expand Up @@ -105,13 +105,12 @@ public static LocalTimeValue truncate(
throw new UnsupportedOperationException( "not implemented" ); throw new UnsupportedOperationException( "not implemented" );
} }


static final LocalTime DEFAULT_LOCAL_TIME = LocalTime.of( Field.hour.defaultValue, Field.minute.defaultValue );

static TimeValue.TimeBuilder<LocalTimeValue> builder( Supplier<ZoneId> defaultZone ) static TimeValue.TimeBuilder<LocalTimeValue> builder( Supplier<ZoneId> defaultZone )
{ {
return new TimeValue.TimeBuilder<LocalTimeValue>( defaultZone ) return new TimeValue.TimeBuilder<LocalTimeValue>( defaultZone )
{ {

private final LocalTime defaulLocalTime = LocalTime.of( Field.hour.defaultValue, Field.minute.defaultValue );

@Override @Override
public LocalTimeValue buildInternal() public LocalTimeValue buildInternal()
{ {
Expand All @@ -127,7 +126,7 @@ public LocalTimeValue buildInternal()
} }
else else
{ {
result = defaulLocalTime; result = DEFAULT_LOCAL_TIME;
} }


result = assignAllFields( result ); result = assignAllFields( result );
Expand Down

0 comments on commit 7c6c995

Please sign in to comment.