Skip to content

Commit

Permalink
Use StringValue.EMPTY when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Nov 6, 2017
1 parent bea10f0 commit c3d4493
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
Binary file added community/cypher/benchmark.jfr
Binary file not shown.
Expand Up @@ -39,7 +39,7 @@ public final class UTF8StringValue extends StringValue
private final int offset; private final int offset;
private final int byteLength; private final int byteLength;


UTF8StringValue( byte[] bytes, int offset, int length ) public UTF8StringValue( byte[] bytes, int offset, int length )
{ {
assert bytes != null; assert bytes != null;
this.bytes = bytes; this.bytes = bytes;
Expand Down Expand Up @@ -162,6 +162,10 @@ public TextValue substring( int start, int length )
{ {
throw new IndexOutOfBoundsException( "Cannot handle negative start index nor negative length" ); throw new IndexOutOfBoundsException( "Cannot handle negative start index nor negative length" );
} }
if ( length == 0 )
{
return StringValue.EMTPY;
}


int end = start + length; int end = start + length;
byte[] values = bytes; byte[] values = bytes;
Expand Down
Expand Up @@ -44,7 +44,7 @@ public final class Values
public static final Value MAX_NUMBER = Values.doubleValue( Double.NaN ); public static final Value MAX_NUMBER = Values.doubleValue( Double.NaN );
public static final Value ZERO_FLOAT = Values.doubleValue( 0.0 ); public static final Value ZERO_FLOAT = Values.doubleValue( 0.0 );
public static final Value ZERO_INT = Values.longValue( 0 ); public static final Value ZERO_INT = Values.longValue( 0 );
public static final Value MIN_STRING = Values.stringValue( "" ); public static final Value MIN_STRING = StringValue.EMTPY;
public static final Value MAX_STRING = Values.booleanValue( false ); public static final Value MAX_STRING = Values.booleanValue( false );
public static final BooleanValue TRUE = Values.booleanValue( true ); public static final BooleanValue TRUE = Values.booleanValue( true );
public static final BooleanValue FALSE = Values.booleanValue( false ); public static final BooleanValue FALSE = Values.booleanValue( false );
Expand All @@ -59,7 +59,7 @@ public final class Values
public static final ArrayValue EMPTY_LONG_ARRAY = Values.longArray( new long[0] ); public static final ArrayValue EMPTY_LONG_ARRAY = Values.longArray( new long[0] );
public static final ArrayValue EMPTY_FLOAT_ARRAY = Values.floatArray( new float[0] ); public static final ArrayValue EMPTY_FLOAT_ARRAY = Values.floatArray( new float[0] );
public static final ArrayValue EMPTY_DOUBLE_ARRAY = Values.doubleArray( new double[0] ); public static final ArrayValue EMPTY_DOUBLE_ARRAY = Values.doubleArray( new double[0] );
public static final TextArray EMPTY_TEXT_ARRAY = Values.stringArray( ); public static final TextArray EMPTY_TEXT_ARRAY = Values.stringArray();


private Values() private Values()
{ {
Expand Down Expand Up @@ -108,18 +108,32 @@ public static double coerceToDouble( Value value )


public static final Value NO_VALUE = NoValue.NO_VALUE; public static final Value NO_VALUE = NoValue.NO_VALUE;


public static UTF8StringValue utf8Value( byte[] bytes ) public static TextValue utf8Value( byte[] bytes )
{ {
if ( bytes.length == 0 )
{
return EMPTY_STRING;
}

return utf8Value( bytes, 0, bytes.length ); return utf8Value( bytes, 0, bytes.length );
} }


public static UTF8StringValue utf8Value( byte[] bytes, int offset, int length ) public static TextValue utf8Value( byte[] bytes, int offset, int length )
{ {
if ( length == 0 )
{
return EMPTY_STRING;
}

return new UTF8StringValue( bytes, offset, length ); return new UTF8StringValue( bytes, offset, length );
} }


public static TextValue stringValue( String value ) public static TextValue stringValue( String value )
{ {
if ( value.isEmpty() )
{
return EMPTY_STRING;
}
return new StringWrappingStringValue( value ); return new StringWrappingStringValue( value );
} }


Expand All @@ -131,7 +145,7 @@ public static Value stringOrNoValue( String value )
} }
else else
{ {
return new StringWrappingStringValue( value ); return stringValue( value );
} }
} }


Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.neo4j.values.utils; package org.neo4j.values.utils;


import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.UTF8StringValue; import org.neo4j.values.storable.UTF8StringValue;


import static org.neo4j.values.storable.Values.utf8Value; import static org.neo4j.values.storable.Values.utf8Value;
Expand All @@ -39,14 +40,14 @@ private UTF8Utils()
* @param b value to add * @param b value to add
* @return the value a + b * @return the value a + b
*/ */
public static UTF8StringValue add( UTF8StringValue a, UTF8StringValue b ) public static TextValue add( UTF8StringValue a, UTF8StringValue b )
{ {
byte[] bytesA = a.bytes(); byte[] bytesA = a.bytes();
byte[] bytesB = b.bytes(); byte[] bytesB = b.bytes();


byte[] bytes = new byte[bytesA.length + bytesB.length]; byte[] bytes = new byte[bytesA.length + bytesB.length];
System.arraycopy( bytesA, 0, bytes, 0, bytesA.length ); System.arraycopy( bytesA, 0, bytes, 0, bytesA.length );
System.arraycopy( bytesB, 0, bytes, bytesA.length, bytesB.length ); System.arraycopy( bytesB, 0, bytes, bytesA.length, bytesB.length );
return utf8Value(bytes); return utf8Value( bytes );
} }
} }
Expand Up @@ -153,7 +153,7 @@ private void assertSame( TextValue lhs, TextValue rhs )
public void shouldHandleTooLargeStartPointInSubstring() public void shouldHandleTooLargeStartPointInSubstring()
{ {
// Given // Given
UTF8StringValue value = utf8Value( "hello".getBytes( UTF_8 ) ); TextValue value = utf8Value( "hello".getBytes( UTF_8 ) );


// When // When
TextValue substring = value.substring( 8, 5 ); TextValue substring = value.substring( 8, 5 );
Expand All @@ -166,7 +166,7 @@ public void shouldHandleTooLargeStartPointInSubstring()
public void shouldHandleTooLargeLengthInSubstring() public void shouldHandleTooLargeLengthInSubstring()
{ {
// Given // Given
UTF8StringValue value = utf8Value( "hello".getBytes( UTF_8 ) ); TextValue value = utf8Value( "hello".getBytes( UTF_8 ) );


// When // When
TextValue substring = value.substring( 3, 76 ); TextValue substring = value.substring( 3, 76 );
Expand All @@ -179,7 +179,7 @@ public void shouldHandleTooLargeLengthInSubstring()
public void shouldThrowOnNegativeStart() public void shouldThrowOnNegativeStart()
{ {
// Given // Given
UTF8StringValue value = utf8Value( "hello".getBytes( UTF_8 ) ); TextValue value = utf8Value( "hello".getBytes( UTF_8 ) );


// Expect // Expect
exception.expect( IndexOutOfBoundsException.class ); exception.expect( IndexOutOfBoundsException.class );
Expand All @@ -192,7 +192,7 @@ public void shouldThrowOnNegativeStart()
public void shouldThrowOnNegativeLength() public void shouldThrowOnNegativeLength()
{ {
// Given // Given
UTF8StringValue value = utf8Value( "hello".getBytes( UTF_8 ) ); TextValue value = utf8Value( "hello".getBytes( UTF_8 ) );


// Expect // Expect
exception.expect( IndexOutOfBoundsException.class ); exception.expect( IndexOutOfBoundsException.class );
Expand Down

0 comments on commit c3d4493

Please sign in to comment.