Skip to content

Commit

Permalink
Collect all valid properties for temporal, duration and points in enum
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Sep 10, 2018
1 parent 3e66cfe commit b0b756d
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 135 deletions.
Expand Up @@ -21,8 +21,6 @@

import java.time.Clock;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalUnit;
import java.util.Arrays;
import java.util.List;
Expand All @@ -42,6 +40,7 @@
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.procedure.Description;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.TemporalProperties;
import org.neo4j.values.storable.TemporalValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Values;
Expand Down Expand Up @@ -284,51 +283,15 @@ public T apply( Context ctx, AnyValue[] args ) throws ProcedureException
if ( unit instanceof TextValue && input instanceof TemporalValue && fields instanceof MapValue )
{
return function.truncate(
unit( ((TextValue) unit).stringValue() ),
(TemporalValue)input,
TemporalProperties.fromName( ((TextValue) unit).stringValue() ).unit,
(TemporalValue) input,
(MapValue) fields,
function.defaultZone );
}
}
throw new ProcedureException( Status.Procedure.ProcedureCallFailed, "Invalid call signature for " + getClass().getSimpleName() +
throw new ProcedureException( Status.Procedure.ProcedureCallFailed,
"Invalid call signature for " + getClass().getSimpleName() +
": Provided input was " + Arrays.toString( args ) );
}

private static TemporalUnit unit( String unit )
{
switch ( unit )
{
case "millennium":
return ChronoUnit.MILLENNIA;
case "century":
return ChronoUnit.CENTURIES;
case "decade":
return ChronoUnit.DECADES;
case "year":
return ChronoUnit.YEARS;
case "weekYear":
return IsoFields.WEEK_BASED_YEARS;
case "quarter":
return IsoFields.QUARTER_YEARS;
case "month":
return ChronoUnit.MONTHS;
case "week":
return ChronoUnit.WEEKS;
case "day":
return ChronoUnit.DAYS;
case "hour":
return ChronoUnit.HOURS;
case "minute":
return ChronoUnit.MINUTES;
case "second":
return ChronoUnit.SECONDS;
case "millisecond":
return ChronoUnit.MILLIS;
case "microsecond":
return ChronoUnit.MICROS;
default:
throw new IllegalArgumentException( "Unsupported unit: " + unit );
}
}
}
}
@@ -0,0 +1,247 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.values.storable;

import org.neo4j.values.utils.UnsupportedTemporalUnitException;

import static org.neo4j.values.utils.TemporalUtil.NANOS_PER_SECOND;

public enum DurationProperties
{
YEARS( "years" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return months / 12;
}
},
MONTHS( "months" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return months;
}
},
MONTHS_OF_YEAR( "monthsofyear" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return months % 12;
}
},
MONTHS_OF_QUARTER( "monthsofquarter" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return months % 3;
}
},
QUARTERS( "quarters" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return months / 3;
}
},
QUARTERS_OF_YEAR( "quartersofyear" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return (months / 3) % 4;
}
},
WEEKS( "weeks" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return days / 7;
}
},
DAYS_OF_WEEK( "daysofweek" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return days % 7;
}
},
DAYS( "days" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return days;
}
},
HOURS( "hours" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return seconds / 3600;
}
},
MINUTES_OF_HOUR( "minutesofhour" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return (seconds / 60) % 60;
}
},
MINUTES( "minutes" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return seconds / 60;
}
},
SECONDS_OF_MINUTE( "secondsofminute" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return seconds % 60;
}
},
SECONDS( "seconds" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return seconds;
}
},
MILLISECONDS_OF_SECOND( "millisecondsofsecond" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return nanos / 1000_000;
}
},
MILLISECONDS( "milliseconds" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return seconds * 1000 + nanos / 1000_000;
}
},
MICROSECONDS_OF_SECOND( "microsecondsofsecond" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return nanos / 1000;
}
},
MICROSECONDS( "microseconds" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return seconds * 1000_000 + nanos / 1000;
}
},
NANOSECONDS_OF_SECOND( "nanosecondsofsecond" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return nanos;
}
},
NANOSECONDS( "nanoseconds" )
{
@Override
public long asTimeStamp( long months, long days, long seconds, long nanos )
{
return seconds * NANOS_PER_SECOND + nanos;
}
};


public String propertyKey;

DurationProperties( String propertyKey )
{
this.propertyKey = propertyKey;
}

public abstract long asTimeStamp( long months, long days, long seconds, long nanos );

public static DurationProperties fromName( String fieldName )
{
switch ( fieldName.toLowerCase() )
{
case "years":
return YEARS;
case "months":
return MONTHS;
case "monthsofyear":
return MONTHS_OF_YEAR;
case "monthsofquarter":
return MONTHS_OF_QUARTER;
case "quarters":
return QUARTERS;
case "quartersofyear":
return QUARTERS_OF_YEAR;
case "weeks":
return WEEKS;
case "daysofweek":
return DAYS_OF_WEEK;
case "days":
return DAYS;
case "hours":
return HOURS;
case "minutesofhour":
return MINUTES_OF_HOUR;
case "minutes":
return MINUTES;
case "secondsofminute":
return SECONDS_OF_MINUTE;
case "seconds":
return SECONDS;
case "millisecondsofsecond":
return MICROSECONDS_OF_SECOND;
case "milliseconds":
return MILLISECONDS;
case "microsecondsofsecond":
return MICROSECONDS_OF_SECOND;
case "microseconds":
return MICROSECONDS;
case "nanosecondsofsecond":
return NANOSECONDS_OF_SECOND;
case "nanoseconds":
return NANOSECONDS;
default:
throw new UnsupportedTemporalUnitException( "No such field: " + fieldName );
}
}
}
Expand Up @@ -755,73 +755,7 @@ public long get( TemporalUnit unit )
*/
public LongValue get( String fieldName )
{
long val;
switch ( fieldName.toLowerCase() )
{
case "years":
val = months / 12;
break;
case "months":
val = months;
break;
case "monthsofyear":
val = months % 12;
break;
case "monthsofquarter":
val = months % 3;
break;
case "quarters":
val = months / 3;
break;
case "quartersofyear":
val = (months / 3) % 4;
break;
case "weeks":
val = days / 7;
break;
case "daysofweek":
val = days % 7;
break;
case "days":
val = days;
break;
case "hours":
val = seconds / 3600;
break;
case "minutesofhour":
val = (seconds / 60) % 60;
break;
case "minutes":
val = seconds / 60;
break;
case "secondsofminute":
val = seconds % 60;
break;
case "seconds":
val = seconds;
break;
case "millisecondsofsecond":
val = nanos / 1000_000;
break;
case "milliseconds":
val = seconds * 1000 + nanos / 1000_000;
break;
case "microsecondsofsecond":
val = nanos / 1000;
break;
case "microseconds":
val = seconds * 1000_000 + nanos / 1000;
break;
case "nanosecondsofsecond":
val = nanos;
break;
case "nanoseconds":
val = seconds * NANOS_PER_SECOND + nanos;
break;
default:
throw new UnsupportedTemporalUnitException( "No such field: " + fieldName );
}

long val = DurationProperties.fromName( fieldName ).asTimeStamp( months, days, seconds, nanos );
return Values.longValue( val );
}

Expand Down

0 comments on commit b0b756d

Please sign in to comment.