-
Notifications
You must be signed in to change notification settings - Fork 155
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
Temporal types #474
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e395cb5
Simplified `Value#toString()` implementations
lutovich a3270eb
Support for Date Cypher type
lutovich 09d5808
Removed experimental annotation from Point2D and Point3D
lutovich 24e466d
Better name for spatial types integration test
lutovich 5459155
Support for Time Cypher type
lutovich 5ac8ae9
Support for LocalTime Cypher type
lutovich 20e3c01
Support for LocalDateTime Cypher type
lutovich 9e709e4
Support for DateTime Cypher type
lutovich 9965b62
Support for Duration Cypher type
lutovich 8f656b2
Improve naming in TypeConstructor enum
lutovich 82c539e
Use factory methods to construct values
lutovich 971c6c0
Allow value creation from points and duration
lutovich 22a685d
Removed code duplication in TemporalTypesIT
lutovich 740e4a4
Add ITs for DateTime Cypher type
lutovich 5d3b32d
Randomized ITs for temporal types
lutovich df92ed6
Added EOF line breaks for new files
lutovich 85c9e0d
Simplified implementation of `Values#ofXXX()`
lutovich 3e58227
Javadocs for points and temporal types
lutovich bb31fc0
Renamed custom Duration to IsoDuration
lutovich a355fc4
Create IsoDuration from Period and Duration
lutovich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
driver/src/main/java/org/neo4j/driver/internal/InternalDuration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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 + | ||
'}'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 havePeriod period
andDuration duration
? Then the calculation such as add and minus would be easier?There was a problem hiding this comment.
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
long
s from the database butjava.time.Period
only allowsint
s