Skip to content

Commit

Permalink
Move take to ListValue
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 27, 2018
1 parent 9a6894c commit 5175a1d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
Expand Up @@ -23,7 +23,7 @@ import org.neo4j.cypher.internal.runtime.interpreted.pipes.QueryState
import org.neo4j.cypher.internal.runtime.interpreted.{CastSupport, ExecutionContext, ListSupport} import org.neo4j.cypher.internal.runtime.interpreted.{CastSupport, ExecutionContext, ListSupport}
import org.neo4j.values.AnyValue import org.neo4j.values.AnyValue
import org.neo4j.values.storable.{NumberValue, Values} import org.neo4j.values.storable.{NumberValue, Values}
import org.neo4j.values.virtual.{ListValue, VirtualValues} import org.neo4j.values.virtual.ListValue


case class ListSlice(collection: Expression, from: Option[Expression], to: Option[Expression]) case class ListSlice(collection: Expression, from: Option[Expression], to: Option[Expression])
extends NullInNullOutExpression(collection) with ListSupport { extends NullInNullOutExpression(collection) with ListSupport {
Expand Down Expand Up @@ -78,10 +78,10 @@ case class ListSlice(collection: Expression, from: Option[Expression], to: Optio
toValue match { toValue match {
case None => Values.NO_VALUE case None => Values.NO_VALUE
case Some(value) if value >= 0 => case Some(value) if value >= 0 =>
VirtualValues.take(collectionValue, value) collectionValue.take(value)
case Some(value) => case Some(value) =>
val end = collectionValue.size + value val end = collectionValue.size + value
VirtualValues.take(collectionValue, end) collectionValue.take(end)
} }
} }


Expand Down
Expand Up @@ -918,4 +918,10 @@ public ListValue drop( int n )
int start = Math.max( 0, Math.min( n, size ) ); int start = Math.max( 0, Math.min( n, size ) );
return new ListSlice( this, start, size ); return new ListSlice( this, start, size );
} }

public ListValue take( int n )
{
int end = Math.max( 0, Math.min( n, size() ) );
return new ListSlice( this, 0, end );
}
} }
Expand Up @@ -63,12 +63,6 @@ public static ListValue fromArray( ArrayValue arrayValue )
return new ListValue.ArrayValueListValue( arrayValue ); return new ListValue.ArrayValueListValue( arrayValue );
} }


public static ListValue take( ListValue list, int n )
{
int end = Math.max( 0, Math.min( n, list.size() ) );
return new ListValue.ListSlice( list, 0, end );
}

/* /*
TOMBSTONE: TransformedListValue & FilteredListValue TOMBSTONE: TransformedListValue & FilteredListValue
Expand Down
Expand Up @@ -26,7 +26,6 @@
import static org.neo4j.values.storable.Values.longValue; import static org.neo4j.values.storable.Values.longValue;
import static org.neo4j.values.virtual.VirtualValues.EMPTY_LIST; import static org.neo4j.values.virtual.VirtualValues.EMPTY_LIST;
import static org.neo4j.values.virtual.VirtualValues.list; import static org.neo4j.values.virtual.VirtualValues.list;
import static org.neo4j.values.virtual.VirtualValues.take;


public class ListSliceTest public class ListSliceTest
{ {
Expand Down Expand Up @@ -120,7 +119,7 @@ public void shouldBeAbleToTakeFromList()
longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) );


// When // When
ListValue take = take( inner, 3 ); ListValue take = inner.take( 3 );


// Then // Then
ListValue expected = list( longValue( 5L ), longValue( 6L ), longValue( 7L ) ); ListValue expected = list( longValue( 5L ), longValue( 6L ), longValue( 7L ) );
Expand Down
Expand Up @@ -227,7 +227,7 @@ public void shouldRecurseAndCoerce()
NO_VALUE ).dropNoValues(), NO_VALUE ).dropNoValues(),
list( -2L, 1L, 4L, 7L, 10L ).slice( 1, 4 ), list( -2L, 1L, 4L, 7L, 10L ).slice( 1, 4 ),
list( -2L, 1L, 4L, 7L ).drop( 1 ), list( -2L, 1L, 4L, 7L ).drop( 1 ),
VirtualValues.take( list( 1L, 4L, 7L, 10L, 13L ), 3 ), list( 1L, 4L, 7L, 10L, 13L ).take( 3 ),
VirtualValues.reverse( list( 7L, 4L, 1L ) ), VirtualValues.reverse( list( 7L, 4L, 1L ) ),
VirtualValues.concat( list( 1L, 4L ), list( 7L ) ) VirtualValues.concat( list( 1L, 4L ), list( 7L ) )
}; };
Expand All @@ -245,7 +245,7 @@ public void shouldRecurseAndCoerce()
NO_VALUE ).dropNoValues(), NO_VALUE ).dropNoValues(),
list( -2L, 1L, 5L, 8L, 11L ).slice( 1, 4 ), list( -2L, 1L, 5L, 8L, 11L ).slice( 1, 4 ),
list( -2L, 6L, 9L, 12L ).drop( 1 ), list( -2L, 6L, 9L, 12L ).drop( 1 ),
VirtualValues.take( list( 7L, 10L, 13L, 10L, 13L ), 3 ), list( 7L, 10L, 13L, 10L, 13L ).take( 3 ),
VirtualValues.reverse( list( 15L, 12L, 9L ) ), VirtualValues.reverse( list( 15L, 12L, 9L ) ),
VirtualValues.concat( list( 10L, 13L ), list( 16L ) ) VirtualValues.concat( list( 10L, 13L ), list( 16L ) )
}; };
Expand Down

0 comments on commit 5175a1d

Please sign in to comment.