Search in sources :

Example 1 with CypherTypeException

use of org.neo4j.exceptions.CypherTypeException in project neo4j by neo4j.

the class CypherFunctions method last.

@CalledFromGeneratedCode
public static AnyValue last(AnyValue container) {
    assert container != NO_VALUE : "NO_VALUE checks need to happen outside this call";
    if (container instanceof SequenceValue) {
        SequenceValue sequence = (SequenceValue) container;
        int length = sequence.length();
        if (length == 0) {
            return NO_VALUE;
        }
        return sequence.value(length - 1);
    } else {
        throw new CypherTypeException(format("Invalid input for function 'last()': Expected %s to be a list", container));
    }
}
Also used : SequenceValue(org.neo4j.values.SequenceValue) CypherTypeException(org.neo4j.exceptions.CypherTypeException) CalledFromGeneratedCode(org.neo4j.util.CalledFromGeneratedCode)

Example 2 with CypherTypeException

use of org.neo4j.exceptions.CypherTypeException in project neo4j by neo4j.

the class CypherFunctions method listAccess.

private static AnyValue listAccess(SequenceValue container, AnyValue index) {
    NumberValue number = asNumberValue(index, () -> "Cannot access a list '" + container.toString() + "' using a non-number index, got " + index.toString());
    if (!(number instanceof IntegralValue)) {
        throw new CypherTypeException(format("Cannot access a list using an non-integer number index, got %s", number), null);
    }
    long idx = number.longValue();
    if (idx > Integer.MAX_VALUE || idx < Integer.MIN_VALUE) {
        throw new InvalidArgumentException(format("Cannot index a list using a value greater than %d or lesser than %d, got %d", Integer.MAX_VALUE, Integer.MIN_VALUE, idx));
    }
    if (idx < 0) {
        idx = container.length() + idx;
    }
    if (idx >= container.length() || idx < 0) {
        return NO_VALUE;
    }
    return container.value((int) idx);
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) NumberValue(org.neo4j.values.storable.NumberValue) CypherTypeException(org.neo4j.exceptions.CypherTypeException) IntegralValue(org.neo4j.values.storable.IntegralValue)

Aggregations

CypherTypeException (org.neo4j.exceptions.CypherTypeException)2 InvalidArgumentException (org.neo4j.exceptions.InvalidArgumentException)1 CalledFromGeneratedCode (org.neo4j.util.CalledFromGeneratedCode)1 SequenceValue (org.neo4j.values.SequenceValue)1 IntegralValue (org.neo4j.values.storable.IntegralValue)1 NumberValue (org.neo4j.values.storable.NumberValue)1