Skip to content

Commit

Permalink
ValueGroup interfaces
Browse files Browse the repository at this point in the history
There is a need to separate the capabilities of logical values from
their implementation. The ValueGroup class contains these logical
capabilities, so that they can be given to both lazy and direct
implementations without crashing the class hierachies.

Also moves static hash and compare methods to TextValues and
NumberValues classes.

lazy vs. direct equality is still broken for arrays.
  • Loading branch information
fickludd committed May 31, 2017
1 parent eab13c4 commit 3132934
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 134 deletions.
Expand Up @@ -23,7 +23,7 @@

import static java.lang.String.format;

class BooleanArrayValue extends ArrayValue
class BooleanArrayValue extends ArrayValue implements ValueGroup.VBooleanArray
{
private final boolean[] value;

Expand All @@ -34,11 +34,17 @@ class BooleanArrayValue extends ArrayValue
}

@Override
int length()
public int length()
{
return value.length;
}

@Override
public boolean booleanValue( int offset )
{
return value[offset];
}

@Override
public boolean equals( Object other )
{
Expand Down Expand Up @@ -138,14 +144,14 @@ public String toString()
return format( "BooleanArray(%s)", Arrays.toString( value ) );
}

public int compareTo( BooleanArrayValue other )
public int compareTo( ValueGroup.VBooleanArray other )
{
int i = 0;
int x = value.length - other.value.length;
int x = value.length - other.length();

while ( x == 0 && i < value.length )
{
x = Boolean.compare( value[i], other.value[i] );
x = Boolean.compare( value[i], other.booleanValue( i ) );
i++;
}
return x;
Expand Down
Expand Up @@ -25,7 +25,7 @@
* This does not extend AbstractProperty since the JVM can take advantage of the 4 byte initial field alignment if
* we don't extend a class that has fields.
*/
final class BooleanValue extends ScalarValue
final class BooleanValue extends ScalarValue implements ValueGroup.VBoolean
{
private final boolean bool;

Expand Down Expand Up @@ -70,14 +70,16 @@ public int hashCode()
return bool ? -1 : 0;
}

@Override
public boolean booleanValue()
{
return bool;
}

public int compareTo( BooleanValue other )
@Override
public int compareTo( ValueGroup.VBoolean other )
{
return Boolean.compare( bool, other.bool );
return Boolean.compare( bool, other.booleanValue() );
}

@Override
Expand Down
Expand Up @@ -115,12 +115,7 @@ boolean equals( String[] x )
@Override
public int hashCode()
{
return hash( value );
}

static int hash( char[] value )
{
return Arrays.hashCode( value );
return TextValues.hash( value );
}

@Override
Expand Down
Expand Up @@ -21,7 +21,7 @@

import static java.lang.String.format;

final class CharValue extends ScalarValue implements Value.WithStringValue
final class CharValue extends ScalarValue implements ValueGroup.VText
{
final char value;

Expand Down Expand Up @@ -78,6 +78,12 @@ public String stringValue()
return Character.toString( value );
}

@Override
public int compareTo( ValueGroup.VText other )
{
return TextValues.compareCharToString( value, other.stringValue() );
}

@Override
public String toString()
{
Expand Down
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.values;

abstract class FloatingPointArrayValue extends ArrayValue
abstract class FloatingPointArrayValue extends ArrayValue implements ValueGroup.VFloatingPointArray
{
public abstract double doubleValue( int index );

Expand Down Expand Up @@ -50,14 +50,14 @@ public boolean equals( Object other )
@Override
final boolean equals( Value other )
{
if ( other instanceof FloatingPointArrayValue )
if ( other instanceof ValueGroup.VFloatingPointArray )
{
FloatingPointArrayValue that = (FloatingPointArrayValue) other;
ValueGroup.VFloatingPointArray that = (ValueGroup.VFloatingPointArray) other;
return NumberValues.numbersEqual( this, that );
}
else if ( other instanceof IntegralArrayValue )
else if ( other instanceof ValueGroup.VIntegerArray )
{
IntegralArrayValue that = (IntegralArrayValue) other;
ValueGroup.VIntegerArray that = (ValueGroup.VIntegerArray) other;
return NumberValues.numbersEqual( this, that );
}
return false;
Expand All @@ -75,29 +75,7 @@ public final int hashCode()
return result;
}

static int hash( float[] values )
{
int result = 1;
for ( float value : values )
{
int elementHash = NumberValues.hash( value );
result = 31 * result + elementHash;
}
return result;
}

static int hash( double[] values )
{
int result = 1;
for ( double value : values )
{
int elementHash = NumberValues.hash( value );
result = 31 * result + elementHash;
}
return result;
}

public int compareTo( IntegralArrayValue other )
public int compareTo( ValueGroup.VIntegerArray other )
{
int i = 0;
int length = length();
Expand All @@ -111,7 +89,7 @@ public int compareTo( IntegralArrayValue other )
return x;
}

public int compareTo( FloatingPointArrayValue other )
public int compareTo( ValueGroup.VFloatingPointArray other )
{
int i = 0;
int length = length();
Expand Down
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.values;

abstract class FloatingPointNumberValue extends NumberValue
abstract class FloatingPointNumberValue extends ScalarValue implements ValueGroup.VFloatingPoint
{
@Override
public final int hashCode()
Expand Down Expand Up @@ -52,12 +52,12 @@ else if ( other instanceof IntegralNumberValue )
}
}

public int compareTo( IntegralNumberValue other )
public int compareTo( ValueGroup.VInteger other )
{
return NumberValues.compareDoubleAgainstLong( doubleValue(), other.longValue() );
}

public int compareTo( NumberValue other )
public int compareTo( ValueGroup.VFloatingPoint other )
{
return Double.compare( doubleValue(), other.doubleValue() );
}
Expand Down
Expand Up @@ -19,10 +19,8 @@
*/
package org.neo4j.values;

abstract class IntegralArrayValue extends ArrayValue
abstract class IntegralArrayValue extends ArrayValue implements ValueGroup.VIntegerArray
{
public abstract long longValue( int index );

@Override
boolean equals( boolean[] x )
{
Expand Down Expand Up @@ -50,14 +48,14 @@ public boolean equals( Object other )
@Override
final boolean equals( Value other )
{
if ( other instanceof IntegralArrayValue )
if ( other instanceof ValueGroup.VIntegerArray )
{
IntegralArrayValue that = (IntegralArrayValue) other;
ValueGroup.VIntegerArray that = (ValueGroup.VIntegerArray) other;
return NumberValues.numbersEqual( this, that );
}
else if ( other instanceof FloatingPointArrayValue )
else if ( other instanceof ValueGroup.VFloatingPointArray )
{
FloatingPointArrayValue that = (FloatingPointArrayValue) other;
ValueGroup.VFloatingPointArray that = (ValueGroup.VFloatingPointArray) other;
return NumberValues.numbersEqual( that, this );
}
return false;
Expand All @@ -75,51 +73,8 @@ public final int hashCode()
return result;
}

static int hash( byte[] values )
{
int result = 1;
for ( byte value : values )
{
int elementHash = NumberValues.hash( value );
result = 31 * result + elementHash;
}
return result;
}

static int hash( short[] values )
{
int result = 1;
for ( short value : values )
{
int elementHash = NumberValues.hash( value );
result = 31 * result + elementHash;
}
return result;
}

static int hash( int[] values )
{
int result = 1;
for ( int value : values )
{
int elementHash = NumberValues.hash( value );
result = 31 * result + elementHash;
}
return result;
}

static int hash( long[] values )
{
int result = 1;
for ( long value : values )
{
int elementHash = NumberValues.hash( value );
result = 31 * result + elementHash;
}
return result;
}

public int compareTo( IntegralArrayValue other )
@Override
public int compareTo( ValueGroup.VIntegerArray other )
{
int i = 0;
int length = length();
Expand All @@ -133,7 +88,8 @@ public int compareTo( IntegralArrayValue other )
return x;
}

public int compareTo( FloatingPointArrayValue other )
@Override
public int compareTo( ValueGroup.VFloatingPointArray other )
{
int i = 0;
int length = length();
Expand Down
Expand Up @@ -19,15 +19,8 @@
*/
package org.neo4j.values;

abstract class IntegralNumberValue extends NumberValue
abstract class IntegralNumberValue extends ScalarValue implements ValueGroup.VInteger
{
abstract long longValue();

public double doubleValue()
{
return (double) longValue();
}

@Override
public final int hashCode()
{
Expand Down Expand Up @@ -59,12 +52,12 @@ else if ( other instanceof FloatingPointNumberValue )
}
}

public int compareTo( IntegralNumberValue other )
public int compareTo( ValueGroup.VInteger other )
{
return Long.compare( longValue(), other.longValue() );
}

public int compareTo( NumberValue other )
public int compareTo( ValueGroup.VFloatingPoint other )
{
return NumberValues.compareLongAgainstDouble( longValue(), other.doubleValue() );
}
Expand Down

0 comments on commit 3132934

Please sign in to comment.