package com.github.simy4.xpath.expr;

import com.github.simy4.xpath.navigator.Node;
import com.github.simy4.xpath.view.NumberView;
import com.github.simy4.xpath.view.ViewContext;

public class NumberExpr implements Expr {

    private final NumberView number;

    public NumberExpr(double number) {
        this.number = new NumberView(number);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <N extends Node> NumberView<N> resolve(ViewContext<N> context) {
        return number;
    }

    @Override
    public boolean test(ViewContext<?> context) {
        double number = resolve(context).toNumber();
        return 0 == Double.compare(number, context.getPosition())
                || context.isGreedy() && !context.hasNext() && number > context.getPosition() && test(context, number);
    }

    private <N extends Node> boolean test(ViewContext<N> context, double number) {
        final N node = context.getCurrent().getNode();
        long numberOfNodesToCreate = (long) number - context.getPosition();
        do {
            context.getNavigator().prependCopy(node);
        } while (--numberOfNodesToCreate > 0);
        return true;
    }

    @Override
    public String toString() {
        return number.toString();
    }

}

@@ -234,26 +234,22 @@
 e final 
-NumberView
+double
  number;
@@ -316,29 +316,13 @@
  = n
-ew N
 umber
-View(number)
 ;%0A  
@@ -344,43 +344,8 @@
 ide%0A
-    @SuppressWarnings(%22unchecked%22)%0A
     
@@ -432,21 +432,40 @@
 return n
+ew N
 umber
+View%3CN%3E(number)
 ;%0A    %7D%0A
@@ -533,61 +533,8 @@
 ) %7B%0A
-        double number = resolve(context).toNumber();%0A
     
@@ -1138,22 +1138,22 @@
  return 
-number
+Double
 .toStrin
@@ -1154,16 +1154,22 @@
 oString(
+number
 );%0A    %7D

Optimise NumberExpr for a more popular case