Skip to content

Commit

Permalink
Let Values implement Comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
sherfert committed Feb 21, 2018
1 parent 33d9a15 commit c76f96d
Show file tree
Hide file tree
Showing 27 changed files with 245 additions and 233 deletions.
Expand Up @@ -40,7 +40,7 @@ abstract sealed class ComparablePredicate(val left: Expression, val right: Expre
case (n1: NumberValue, n2: NumberValue) => Some(compare(AnyValues.COMPARATOR.compare(n1, n2)))
case (n1: TextValue, n2: TextValue) => Some(compare(AnyValues.COMPARATOR.compare(n1, n2)))
case (n1: BooleanValue, n2: BooleanValue) => Some(compare(AnyValues.COMPARATOR.compare(n1, n2)))
case (n1: PointValue, n2: PointValue) => Some(compare(n1.compareTo(n2)))
case (n1: PointValue, n2: PointValue) => Some(compare(AnyValues.COMPARATOR.compare(n1, n2)))
case _ => None
}
res
Expand Down
Expand Up @@ -90,9 +90,17 @@ public boolean[] asObject()
return value;
}

public int compareTo( BooleanArray other )
{
return NumberValues.compareBooleanArrays( this, other );
@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof BooleanArray )
{
return NumberValues.compareBooleanArrays( this, (BooleanArray) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}

@Override
Expand Down
Expand Up @@ -30,7 +30,7 @@
public abstract class BooleanValue extends ScalarValue
{

private BooleanValue( )
private BooleanValue()
{
}

Expand All @@ -53,8 +53,6 @@ public ValueGroup valueGroup()

public abstract boolean booleanValue();

public abstract int compareTo( BooleanValue other );

@Override
public NumberType numberType()
{
Expand Down Expand Up @@ -87,8 +85,14 @@ public boolean booleanValue()
return true;
}

public int compareTo( BooleanValue other )
@Override
public int compareTo( Value otherValue )
{
if ( !(otherValue instanceof BooleanValue) )
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
BooleanValue other = (BooleanValue) otherValue;
return other.booleanValue() ? 0 : 1;
}

Expand All @@ -115,7 +119,6 @@ public String toString()
{
return format( "Boolean('%s')", Boolean.toString( true ) );
}

};

public static final BooleanValue FALSE = new BooleanValue()
Expand Down Expand Up @@ -144,8 +147,14 @@ public boolean booleanValue()
return false;
}

public int compareTo( BooleanValue other )
@Override
public int compareTo( Value otherValue )
{
if ( !(otherValue instanceof BooleanValue) )
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
BooleanValue other = (BooleanValue) otherValue;
return !other.booleanValue() ? 0 : -1;
}

Expand All @@ -172,6 +181,5 @@ public String toString()
{
return format( "Boolean('%s')", Boolean.toString( false ) );
}

};
}
Expand Up @@ -24,7 +24,7 @@

import org.neo4j.values.ValueMapper;

public class DateArray extends TemporalArray<LocalDate, DateValue>
public class DateArray extends TemporalArray<LocalDate,DateValue>
{
private final LocalDate[] value;

Expand Down Expand Up @@ -55,18 +55,31 @@ public boolean equals( Value other )
@Override
public boolean equals( LocalDate[] x )
{
return Arrays.equals( value, x);
return Arrays.equals( value, x );
}

@Override
public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
{
writeTo( writer, ValueWriter.ArrayType.DATE ,value );
writeTo( writer, ValueWriter.ArrayType.DATE, value );
}

@Override
public ValueGroup valueGroup()
{
return ValueGroup.DATE_ARRAY;
}

@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof DateArray )
{
return compareToNonPrimitiveArray( (DateArray) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}
}
Expand Up @@ -24,7 +24,7 @@

import org.neo4j.values.ValueMapper;

public class DateTimeArray extends TemporalArray<ZonedDateTime, DateTimeValue>
public class DateTimeArray extends TemporalArray<ZonedDateTime,DateTimeValue>
{
private final ZonedDateTime[] value;

Expand Down Expand Up @@ -55,18 +55,31 @@ public boolean equals( Value other )
@Override
public boolean equals( ZonedDateTime[] x )
{
return Arrays.equals( value, x);
return Arrays.equals( value, x );
}

@Override
public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
{
writeTo( writer, ValueWriter.ArrayType.ZONED_DATE_TIME ,value );
writeTo( writer, ValueWriter.ArrayType.ZONED_DATE_TIME, value );
}

@Override
public ValueGroup valueGroup()
{
return ValueGroup.ZONED_DATE_TIME_ARRAY;
}

@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof DateTimeArray )
{
return compareToNonPrimitiveArray( (DateTimeArray) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}
}
Expand Up @@ -355,9 +355,14 @@ public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
}
}

public int compareTo( DateTimeValue other )
@Override
public int compareTo( Value otherValue )
{

if ( !(otherValue instanceof DateTimeValue) )
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
DateTimeValue other = (DateTimeValue) otherValue;
return value.compareTo( other.value );
}

Expand Down
Expand Up @@ -167,9 +167,14 @@ private DateValue( LocalDate value )
this.value = value;
}

public int compareTo( DateValue other )
@Override
public int compareTo( Value otherValue )
{

if ( !(otherValue instanceof DateValue) )
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
DateValue other = (DateValue) otherValue;
return value.compareTo( other.value );
}

Expand Down
Expand Up @@ -80,4 +80,17 @@ public ValueGroup valueGroup()
{
return ValueGroup.DURATION_ARRAY;
}

@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof DurationArray )
{
return compareToNonPrimitiveArray( (DurationArray) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}
}
Expand Up @@ -60,7 +60,7 @@
* {@link java.time.Duration} only works with seconds, assumes 24H days, and is unable to handle larger units than days.
* {@link java.time.Period} only works with units from days or larger, and does not deal with time.
*/
public final class DurationValue extends ScalarValue implements TemporalAmount, Comparable<DurationValue>
public final class DurationValue extends ScalarValue implements TemporalAmount
{
public static DurationValue duration( Duration value )
{
Expand Down Expand Up @@ -237,8 +237,14 @@ else if ( seconds > 0 && nanos < 0 )
this.nanos = (int) nanos;
}

public int compareTo( DurationValue other )
@Override
public int compareTo( Value otherValue )
{
if ( !(otherValue instanceof DurationValue) )
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
DurationValue other = (DurationValue) otherValue;
return COMPARATOR.compare( this, other );
}

Expand Down
Expand Up @@ -69,4 +69,17 @@ public ValueGroup valueGroup()
{
return ValueGroup.LOCAL_DATE_TIME_ARRAY;
}

@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof LocalDateTimeArray )
{
return compareToNonPrimitiveArray( (LocalDateTimeArray) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}
}
Expand Up @@ -279,9 +279,14 @@ private LocalDateTimeValue( LocalDateTime value )
this.value = value;
}

public int compareTo( LocalDateTimeValue other )
@Override
public int compareTo( Value otherValue )
{

if ( !(otherValue instanceof LocalDateTimeValue) )
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
LocalDateTimeValue other = (LocalDateTimeValue) otherValue;
return value.compareTo( other.value );
}

Expand Down
Expand Up @@ -69,4 +69,17 @@ public ValueGroup valueGroup()
{
return ValueGroup.LOCAL_TIME_ARRAY;
}

@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof LocalTimeArray )
{
return compareToNonPrimitiveArray( (LocalTimeArray) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}
}
Expand Up @@ -137,9 +137,14 @@ private LocalTimeValue( LocalTime value )
this.value = value;
}

public int compareTo( LocalTimeValue other )
@Override
public int compareTo( Value otherValue )
{

if ( !(otherValue instanceof LocalTimeValue) )
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
LocalTimeValue other = (LocalTimeValue) otherValue;
return value.compareTo( other.value );
}

Expand Down
Expand Up @@ -102,4 +102,10 @@ public NumberType numberType()
{
return NumberType.NO_NUMBER;
}

@Override
public int compareTo( Value other )
{
return 0;
}
}
Expand Up @@ -85,7 +85,7 @@ public final NumberType numberType()
return NumberType.NO_NUMBER;
}

public final int compareTo( NonPrimitiveArray<T> other )
protected final int compareToNonPrimitiveArray( NonPrimitiveArray<T> other )
{
int i = 0;
int x = 0;
Expand Down
Expand Up @@ -33,6 +33,23 @@ public abstract class NumberArray extends ArrayValue

abstract int compareTo( FloatingPointArray other );

@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof IntegralArray )
{
return compareTo( (IntegralArray) otherValue );
}
else if ( otherValue instanceof FloatingPointArray )
{
return compareTo( (FloatingPointArray) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}

@Override
public final boolean equals( boolean[] x )
{
Expand Down
Expand Up @@ -29,6 +29,23 @@ public abstract class NumberValue extends ScalarValue

abstract int compareTo( FloatingPointValue other );

@Override
public int compareTo( Value otherValue )
{
if ( otherValue instanceof IntegralValue )
{
return compareTo( (IntegralValue) otherValue );
}
else if ( otherValue instanceof FloatingPointValue )
{
return compareTo( (FloatingPointValue) otherValue );
}
else
{
throw new IllegalArgumentException( "Cannot compare different values" );
}
}

@Override
public abstract Number asObjectCopy();

Expand Down

0 comments on commit c76f96d

Please sign in to comment.