Skip to content

Commit

Permalink
Introduce all remaining temporal functions
Browse files Browse the repository at this point in the history
Also a slight simpligying refactoring to StructureBuilder.
  • Loading branch information
thobe committed Jan 31, 2018
1 parent 472c7b0 commit ddccbb6
Show file tree
Hide file tree
Showing 21 changed files with 682 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.proc.temporal;

import java.time.Clock;
import java.time.ZoneId;
import java.time.temporal.TemporalUnit;
import java.util.function.Supplier;

import org.neo4j.procedure.Description;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.DateValue;
import org.neo4j.values.storable.TemporalValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.virtual.MapValue;

import static org.neo4j.kernel.api.proc.Neo4jTypes.NTDate;

@Description( "Create a Date instant." )
class DateFunction extends TemporalFunction<DateValue>
{
DateFunction()
{
super( NTDate );
}

@Override
protected DateValue now( Clock clock, String timezone )
{
return timezone == null ? DateValue.now( clock ) : DateValue.now( clock, timezone );
}

@Override
protected DateValue parse( TextValue value, Supplier<ZoneId> defaultZone )
{
return DateValue.parse( value );
}

@Override
protected DateValue build( MapValue map, Supplier<ZoneId> defaultZone )
{
return DateValue.build( map, defaultZone );
}

@Override
protected DateValue positionalCreate( AnyValue[] input )
{
if ( input.length != 3 )
{
throw new IllegalArgumentException( "expected 3 arguments" );
}
return DateValue.date(
anInt( "year", input[0] ),
anInt( "month", input[1] ),
anInt( "day", input[2] ) );
}

@Override
protected DateValue truncate( TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone )
{
return DateValue.truncate( unit, input, fields, defaultZone );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
import org.neo4j.procedure.Description;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.DateTimeValue;
import org.neo4j.values.storable.TemporalValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.virtual.MapValue;

import static org.neo4j.kernel.api.proc.Neo4jTypes.NTDateTime;

@Description( "Construct a DateTime instant." )
@Description( "Create a DateTime instant." )
class DateTimeFunction extends TemporalFunction<DateTimeValue>
{
DateTimeFunction()
{
super( "datetime", NTDateTime );
super( NTDateTime );
}

@Override
@Description( "Get the current DateTime instant." )
protected DateTimeValue now( Clock clock, String timezone )
{
return timezone == null ? DateTimeValue.now( clock ) : DateTimeValue.now( clock, timezone );
Expand All @@ -56,7 +56,7 @@ protected DateTimeValue parse( TextValue value, Supplier<ZoneId> defaultZone )
@Override
protected DateTimeValue build( MapValue map, Supplier<ZoneId> defaultZone )
{
return DateTimeValue.build( map );
return DateTimeValue.build( map, defaultZone );
}

@Override
Expand All @@ -78,8 +78,7 @@ protected DateTimeValue positionalCreate( AnyValue[] input )
}

@Override
@Description( "Truncate to a DateTime instant." )
protected DateTimeValue truncate( TemporalUnit unit, AnyValue input, MapValue fields, Supplier<ZoneId> defaultZone )
protected DateTimeValue truncate( TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone )
{
return DateTimeValue.truncate( unit, input, fields, defaultZone );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public DurationValue apply( Context ctx, AnyValue[] input ) throws ProcedureExce
if ( input[0] instanceof MapValue )
{
MapValue map = (MapValue) input[0];
return DurationValue.build(map);
return DurationValue.build( map );
}
}
throw new ProcedureException( Status.Procedure.ProcedureCallFailed, "Invalid call signature" );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.proc.temporal;

import java.time.Clock;
import java.time.ZoneId;
import java.time.temporal.TemporalUnit;
import java.util.function.Supplier;

import org.neo4j.procedure.Description;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.LocalDateTimeValue;
import org.neo4j.values.storable.TemporalValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.virtual.MapValue;

import static org.neo4j.kernel.api.proc.Neo4jTypes.NTLocalDateTime;

@Description( "Create a LocalDateTime instant." )
class LocalDateTimeFunction extends TemporalFunction<LocalDateTimeValue>
{
LocalDateTimeFunction()
{
super( NTLocalDateTime );
}

@Override
protected LocalDateTimeValue now( Clock clock, String timezone )
{
return timezone == null ? LocalDateTimeValue.now( clock ) : LocalDateTimeValue.now( clock, timezone );
}

@Override
protected LocalDateTimeValue parse( TextValue value, Supplier<ZoneId> defaultZone )
{
return LocalDateTimeValue.parse( value );
}

@Override
protected LocalDateTimeValue build( MapValue map, Supplier<ZoneId> defaultZone )
{
return LocalDateTimeValue.build( map, defaultZone );
}

@Override
protected LocalDateTimeValue positionalCreate( AnyValue[] input )
{
if ( input.length != 7 )
{
throw new IllegalArgumentException( "expected 7 arguments" );
}
return LocalDateTimeValue.localDateTime(
anInt( "year", input[0] ),
anInt( "month", input[1] ),
anInt( "day", input[2] ),
anInt( "hour", input[3] ),
anInt( "minute", input[4] ),
anInt( "second", input[5] ),
anInt( "nanos", input[6] ) );
}

@Override
protected LocalDateTimeValue truncate( TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone )
{
return LocalDateTimeValue.truncate( unit, input, fields, defaultZone );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.proc.temporal;

import java.time.Clock;
import java.time.ZoneId;
import java.time.temporal.TemporalUnit;
import java.util.function.Supplier;

import org.neo4j.procedure.Description;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.TemporalValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.LocalTimeValue;
import org.neo4j.values.virtual.MapValue;

import static org.neo4j.kernel.api.proc.Neo4jTypes.NTLocalTime;

@Description( "Create a LocalTime instant." )
class LocalTimeFunction extends TemporalFunction<LocalTimeValue>
{
LocalTimeFunction()
{
super( NTLocalTime );
}

@Override
protected LocalTimeValue now( Clock clock, String timezone )
{
return timezone == null ? LocalTimeValue.now( clock ) : LocalTimeValue.now( clock, timezone );
}

@Override
protected LocalTimeValue parse( TextValue value, Supplier<ZoneId> defaultZone )
{
return LocalTimeValue.parse( value );
}

@Override
protected LocalTimeValue build( MapValue map, Supplier<ZoneId> defaultZone )
{
return LocalTimeValue.build( map, defaultZone );
}

@Override
protected LocalTimeValue positionalCreate( AnyValue[] input )
{
if ( input.length != 4 )
{
throw new IllegalArgumentException( "expected 4 arguments" );
}
return LocalTimeValue.localTime(
anInt( "hour", input[0] ),
anInt( "minute", input[1] ),
anInt( "second", input[2] ),
anInt( "nanos", input[3] ) );
}

@Override
protected LocalTimeValue truncate( TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone )
{
return LocalTimeValue.truncate( unit, input, fields, defaultZone );
}
}

0 comments on commit ddccbb6

Please sign in to comment.