Skip to content

Commit

Permalink
Extract common code and change error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
sherfert authored and Lojjs committed Mar 5, 2018
1 parent 2e80854 commit ae93ceb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
Expand Up @@ -156,27 +156,12 @@ public static DateTimeValue truncate(
MapValue fields,
Supplier<ZoneId> defaultZone )
{
if ( unit.isTimeBased() && !(input instanceof DateTimeValue || input instanceof LocalDateTimeValue) )
{
throw new IllegalArgumentException( "Cannot truncate " + input + " to date time with a time based unit." );
}
LocalDate localDate = input.getDatePart();
LocalTime localTime = input.hasTime() ? input.getLocalTimePart() : LocalTimeValue.DEFAULT_LOCAL_TIME;
ZoneId zoneId = input.hasTimeZone() ? input.getZoneId( defaultZone ) : defaultZone.get();
Pair<LocalDate,LocalTime> pair = getTruncatedDateAndTime( unit, input, "date time" );

LocalTime truncatedTime;
LocalDate truncatedDate;
if ( unit.isDateBased() )
{
truncatedDate = DateValue.truncateTo( localDate, unit );
truncatedTime = LocalTimeValue.DEFAULT_LOCAL_TIME;
}
else
{
truncatedDate = localDate;
truncatedTime = localTime.truncatedTo( unit );
}
LocalDate truncatedDate = pair.first();
LocalTime truncatedTime = pair.other();

ZoneId zoneId = input.hasTimeZone() ? input.getZoneId( defaultZone ) : defaultZone.get();
ZonedDateTime truncatedZDT = ZonedDateTime.of( truncatedDate, truncatedTime, zoneId );

if ( fields.size() == 0 )
Expand All @@ -200,7 +185,6 @@ public static DateTimeValue truncate(
}

truncatedZDT = updateFieldMapWithConflictingSubseconds( updatedFields, unit, truncatedZDT );

if ( updatedFields.size() == 0 )
{
return datetime( truncatedZDT );
Expand Down
Expand Up @@ -119,27 +119,13 @@ public static LocalDateTimeValue truncate(
MapValue fields,
Supplier<ZoneId> defaultZone )
{
if ( unit.isTimeBased() && !(input instanceof DateTimeValue || input instanceof LocalDateTimeValue) )
{
throw new IllegalArgumentException( "Cannot truncate " + input + " to local date time with a time based unit." );
}
LocalTime localTime = input.hasTime() ? input.getLocalTimePart() : LocalTimeValue.DEFAULT_LOCAL_TIME;
LocalDate localDate = input.getDatePart();
Pair<LocalDate,LocalTime> pair = getTruncatedDateAndTime( unit, input, "local date time" );

LocalTime truncatedTime;
LocalDate truncatedDate;
if ( unit.isDateBased() )
{
truncatedDate = DateValue.truncateTo( localDate, unit );
truncatedTime = LocalTimeValue.DEFAULT_LOCAL_TIME;
}
else
{
truncatedDate = localDate;
truncatedTime = localTime.truncatedTo( unit );
}
LocalDate truncatedDate = pair.first();
LocalTime truncatedTime = pair.other();

LocalDateTime truncatedLDT = LocalDateTime.of( truncatedDate, truncatedTime );

if ( fields.size() == 0 )
{
return localDateTime( truncatedLDT );
Expand Down
Expand Up @@ -35,9 +35,8 @@
import static java.time.temporal.ChronoUnit.MILLENNIA;
import static java.time.temporal.ChronoUnit.YEARS;

public enum Neo4JTemporalField implements TemporalField
enum Neo4JTemporalField implements TemporalField
{

YEAR_OF_DECADE( "Year of decade", YEARS, DECADES, 10 ),
YEAR_OF_CENTURY( "Year of century", YEARS, CENTURIES, 100 ),
YEAR_OF_MILLENNIUM( "Millennium", YEARS, MILLENNIA, 1000 );
Expand Down Expand Up @@ -109,7 +108,7 @@ public ValueRange rangeRefinedBy( TemporalAccessor temporal )
@Override
public long getFrom( TemporalAccessor temporal )
{
throw new UnsupportedTemporalTypeException( "We don't support anything." );
throw new UnsupportedOperationException( "Getting a " + this.name + " from temporal values is not supported." );
}

@SuppressWarnings( "unchecked" )
Expand Down
Expand Up @@ -1144,4 +1144,28 @@ else if ( conflictingMicroSeconds )
}
return truncated;
}

static Pair<LocalDate,LocalTime> getTruncatedDateAndTime( TemporalUnit unit, TemporalValue input, String type )
{
if ( unit.isTimeBased() && !(input instanceof DateTimeValue || input instanceof LocalDateTimeValue) )
{
throw new IllegalArgumentException( String.format( "Cannot truncate %s to %s with a time based unit.", input, type ) );
}
LocalDate localDate = input.getDatePart();
LocalTime localTime = input.hasTime() ? input.getLocalTimePart() : LocalTimeValue.DEFAULT_LOCAL_TIME;

LocalTime truncatedTime;
LocalDate truncatedDate;
if ( unit.isDateBased() )
{
truncatedDate = DateValue.truncateTo( localDate, unit );
truncatedTime = LocalTimeValue.DEFAULT_LOCAL_TIME;
}
else
{
truncatedDate = localDate;
truncatedTime = localTime.truncatedTo( unit );
}
return Pair.of( truncatedDate, truncatedTime );
}
}

0 comments on commit ae93ceb

Please sign in to comment.