Skip to content

Temporal types #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions driver/src/main/java/org/neo4j/driver/internal/InternalDuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* 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.neo4j.driver.internal;

import java.time.temporal.Temporal;
import java.time.temporal.TemporalUnit;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.List;
import java.util.Objects;

import org.neo4j.driver.v1.types.Duration;

import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.MONTHS;
import static java.time.temporal.ChronoUnit.NANOS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;

public class InternalDuration implements Duration
{
private static final List<TemporalUnit> SUPPORTED_UNITS = unmodifiableList( asList( MONTHS, DAYS, SECONDS, NANOS ) );

private final long months;
private final long days;
private final long seconds;
private final long nanoseconds;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of having months, days, seconds, nanoseconds 4 field, we could have Period period and Duration duration? Then the calculation such as add and minus would be easier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We receive months and days as longs from the database but java.time.Period only allows ints


public InternalDuration( long months, long days, long seconds, long nanoseconds )
{
this.months = months;
this.days = days;
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}

@Override
public long months()
{
return months;
}

@Override
public long days()
{
return days;
}

@Override
public long seconds()
{
return seconds;
}

@Override
public long nanoseconds()
{
return nanoseconds;
}

@Override
public long get( TemporalUnit unit )
{
if ( unit == MONTHS )
{
return months;
}
else if ( unit == DAYS )
{
return days;
}
else if ( unit == SECONDS )
{
return seconds;
}
else if ( unit == NANOS )
{
return nanoseconds;
}
else
{
throw new UnsupportedTemporalTypeException( "Unsupported unit: " + unit );
}
}

@Override
public List<TemporalUnit> getUnits()
{
return SUPPORTED_UNITS;
}

@Override
public Temporal addTo( Temporal temporal )
{
if ( months != 0 )
{
temporal = temporal.plus( months, MONTHS );
}
if ( days != 0 )
{
temporal = temporal.plus( days, DAYS );
}
if ( seconds != 0 )
{
temporal = temporal.plus( seconds, SECONDS );
}
if ( nanoseconds != 0 )
{
temporal = temporal.plus( nanoseconds, NANOS );
}
return temporal;
}

@Override
public Temporal subtractFrom( Temporal temporal )
{
if ( months != 0 )
{
temporal = temporal.minus( months, MONTHS );
}
if ( days != 0 )
{
temporal = temporal.minus( days, DAYS );
}
if ( seconds != 0 )
{
temporal = temporal.minus( seconds, SECONDS );
}
if ( nanoseconds != 0 )
{
temporal = temporal.minus( nanoseconds, NANOS );
}
return temporal;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
InternalDuration that = (InternalDuration) o;
return months == that.months &&
days == that.days &&
seconds == that.seconds &&
nanoseconds == that.nanoseconds;
}

@Override
public int hashCode()
{
return Objects.hash( months, days, seconds, nanoseconds );
}

@Override
public String toString()
{
return "Duration{" +
"months=" + months +
", days=" + days +
", seconds=" + seconds +
", nanoseconds=" + nanoseconds +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import java.util.Map;
import java.util.NoSuchElementException;

import org.neo4j.driver.internal.util.Extract;
import org.neo4j.driver.internal.value.InternalValue;
import org.neo4j.driver.internal.types.InternalMapAccessorWithDefaultValue;
import org.neo4j.driver.internal.util.Extract;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.Values;
Expand Down Expand Up @@ -129,7 +128,7 @@ public <T> Map<String,T> asMap( Function<Value,T> mapper )
@Override
public String toString()
{
return format( "Record<%s>", formatPairs( InternalValue.Format.VALUE_ONLY, asMap( ofValue() ) ) );
return format( "Record<%s>", formatPairs( asMap( ofValue() ) ) );
}

@Override
Expand Down
Loading