<commit_before>package com.tinkerpop.gremlin.compiler.types;

import com.tinkerpop.gremlin.compiler.Tokens;
import com.tinkerpop.gremlin.compiler.context.GremlinScriptContext;
import com.tinkerpop.gremlin.compiler.context.VariableLibrary;
import com.tinkerpop.gremlin.compiler.pipes.GremlinPipesHelper;
import com.tinkerpop.gremlin.compiler.pipes.GremlinRangeFilterPipe;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.Pipeline;
import com.tinkerpop.pipes.SingleIterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author <NAME>
 */
public class GPath implements Iterable, Comparable {

    private final List<Pipe> pipes;
    private final Atom<Object> root;
    private Object persistentRoot = null;

    public GPath(final Atom<Object> root, final List<Pipe> pipes, final GremlinScriptContext context) {
        this.root  = root;
        this.pipes = pipes;
        
        if (!(root instanceof DynamicEntity)) {
            if (root.toString().equals(".") && root.isIdentifier()) {
                final VariableLibrary variables = context.getVariableLibrary();
                this.persistentRoot = ((Atom) variables.get(Tokens.ROOT_VARIABLE)).getValue();
            }
        }
    }
    
    public Iterator iterator() {
        for(Pipe p : this.pipes) {
            if (p instanceof GremlinRangeFilterPipe) {
                ((GremlinRangeFilterPipe) p).reset();
            }
        }
        
        Pipeline pipeline = new Pipeline(this.pipes);
        pipeline.setStarts(this.pipelineRoot());
        return pipeline;
    }

    public List<Object> toList() {
        Iterator current = this.iterator();
        List<Object> results = new ArrayList<Object>();
        
        while(current.hasNext()) {
            results.add(current.next());    
        }

        return results;
    }

    public int compareTo(Object o) {
        List<Object> list = this.toList();

        if (list.size() == 1) {
            Object c = list.get(0);
            if (c instanceof Comparable && o instanceof Comparable)
                return ((Comparable<Object>) c).compareTo(o);
        }

        throw new ClassCastException(this + " and " + o + " are incomparable objects");
    }

    public boolean equals(Object o) {
        return this.toList().equals(convertToList(o));
    }

    protected Iterable convertToList(Object operand) {
        if (operand instanceof GPath) {
            return ((GPath) operand).toList();
        } else if (operand instanceof Iterable) {
            return (Iterable) operand;
        } else {
            List<Object> list = new ArrayList<Object>();
            list.add(operand);
            return list;
        }
    }

    private Iterator pipelineRoot() {
        final Object root = (persistentRoot == null) ? this.root.getValue() : this.persistentRoot;
        
        if (root instanceof Iterable) {
            return ((Iterable) root).iterator();
        } else if (root instanceof Iterator) {
            return (Iterator) root;<commit_msg>compareTo returns -1 if GPath is null.<commit_after>package com.tinkerpop.gremlin.compiler.types;

import com.tinkerpop.gremlin.compiler.Tokens;
import com.tinkerpop.gremlin.compiler.context.GremlinScriptContext;
import com.tinkerpop.gremlin.compiler.context.VariableLibrary;
import com.tinkerpop.gremlin.compiler.pipes.GremlinPipesHelper;
import com.tinkerpop.gremlin.compiler.pipes.GremlinRangeFilterPipe;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.Pipeline;
import com.tinkerpop.pipes.SingleIterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author <NAME>
 */
public class GPath implements Iterable, Comparable {

    private final List<Pipe> pipes;
    private final Atom<Object> root;
    private Object persistentRoot = null;

    public GPath(final Atom<Object> root, final List<Pipe> pipes, final GremlinScriptContext context) {
        this.root  = root;
        this.pipes = pipes;
        
        if (!(root instanceof DynamicEntity)) {
            if (root.toString().equals(".") && root.isIdentifier()) {
                final VariableLibrary variables = context.getVariableLibrary();
                this.persistentRoot = ((Atom) variables.get(Tokens.ROOT_VARIABLE)).getValue();
            }
        }
    }
    
    public Iterator iterator() {
        for(Pipe p : this.pipes) {
            if (p instanceof GremlinRangeFilterPipe) {
                ((GremlinRangeFilterPipe) p).reset();
            }
        }
        
        Pipeline pipeline = new Pipeline(this.pipes);
        pipeline.setStarts(this.pipelineRoot());
        return pipeline;
    }

    public List<Object> toList() {
        Iterator current = this.iterator();
        List<Object> results = new ArrayList<Object>();
        
        while(current.hasNext()) {
            results.add(current.next());    
        }

        return results;
    }

    public int compareTo(Object o) {
        List<Object> list = this.toList();

        if (list.size() == 1) {
            Object c = list.get(0);

            if (c == null) return -1;
            if (c instanceof Comparable && o instanceof Comparable)
                return ((Comparable<Object>) c).compareTo(o);
        }

        throw new ClassCastException(this + " and " + o + " are incomparable objects");
    }

    public boolean equals(Object o) {
        return this.toList().equals(convertToList(o));
    }

    protected Iterable convertToList(Object operand) {
        if (operand instanceof GPath) {
            return ((GPath) operand).toList();
        } else if (operand instanceof Iterable) {
            return (Iterable) operand;
        } else {
            List<Object> list = new ArrayList<Object>();
            list.add(operand);
            return list;
        }
    }

    private Iterator pipelineRoot() {
        final Object root = (persistentRoot == null) ? this.root.getValue() : this.persistentRoot;
        
        if (root instanceof Iterable) {
            return ((Iterable) root).iterator();
        } else if (root instanceof Iterator) {
            return (Iterator) root;
{'fraction_non_alphanumeric': 0.1112908443869298, 'fraction_numerical': 0.0009705596894208993, 'mean_word_length': 2.387945205479452, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5525968', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1714, 'n_words': 398}