<commit_before>package io.yawp.repository;

import io.yawp.commons.http.HttpVerb;
import io.yawp.repository.actions.ActionKey;
import io.yawp.repository.query.QueryBuilder;

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

public class IdRef<T> implements Comparable<IdRef<T>> {

    private Repository r;

    private Class<T> clazz;

    private ObjectModel model;

    private Long id;

    private String name;

    private IdRef<?> parentId;

    protected IdRef(Repository r, Class<T> clazz, Long id) {
        this.r = r;
        this.clazz = clazz;
        this.id = id;
        this.model = new ObjectModel(clazz);
    }

    public IdRef(Repository r, Class<T> clazz, String name) {
        this.r = r;
        this.clazz = clazz;
        this.name = name;
        this.model = new ObjectModel(clazz);
    }

    public void setParentId(IdRef<?> parentId) {
        this.parentId = parentId;
    }

    public T fetch() {
        return fetch(clazz);
    }

    public <TT> TT fetch(Class<TT> childClazz) {
        return r.query(childClazz).fetch(this);
    }

    public <TT> TT child(Class<TT> childClazz) {
        QueryBuilder<TT> q = r.query(childClazz).from(this);
        return q.only();
    }

    public Long asLong() {
        return id;
    }

    public String asString() {
        return name;
    }

    public Repository getRepository() {
        return r;
    }

    public Class<T> getClazz() {
        return clazz;
    }

    public Class<?> getParentClazz() {
        return model.getParentClazz();
    }

    public ObjectModel getModel() {
        return new ObjectModel(clazz);
    }

    @SuppressWarnings("unchecked")
    public <TT> IdRef<TT> getParentId() {
        return (IdRef<TT>) parentId;
    }

    @SuppressWarnings("unchecked")
    public <TT> IdRef<TT> getAncestorId(int ancestor) {
        IdRef<?> ancestorId = this;
        for (int i = 0; i <= ancestor; i++) {
            ancestorId = ancestorId.getParentId();
        }
        return (IdRef<TT>) ancestorId;
    }

    public <TT> IdRef<TT> createChildId(Class<TT> clazz, Long id) {
        IdRef<TT> idRef = new IdRef<TT>(r, clazz, id);
        idRef.setParentId(this);
        return idRef;
    }

    public <TT> IdRef<TT> createChildId(Class<TT> clazz, String name) {
        IdRef<TT> idRef = new IdRef<TT>(r, clazz, name);
        idRef.setParentId(this);
        return idRef;
    }

    public static <TT> IdRef<TT> create(Repository r, Class<TT> clazz, Long id) {
        return new IdRef<TT>(r, clazz, id);
    }

    public static <TT> IdRef<TT> create(Repository r, Class<TT> clazz, String name) {
        return new IdRef<TT>(r, clazz, name);
    }

    public static <TT> List<IdRef<TT>> create(Repository r, Class<TT> clazz, Long... ids) {
        List<IdRef<TT>> idRefs = new ArrayList<IdRef<TT>>();
        for (int i = 0; i < ids.length; i++) {
            idRefs.add(create(r, clazz, ids[i]));
        }
        return idRefs;
    }

    @SuppressWarnings("unchecked")
    public static <TT> IdRef<TT> parse(Repository r, HttpVerb verb, String path) {
        String[] parts = path.substring(1).split("/");

        if (parts.length < 2) {
            return null;
        }

        IdRef<TT> lastIdRef = null;

        for (int i = 0; i < parts.length; i += 2) {
            if (isActionOrCollection(r, verb, parts, i)) {
                break;
            }

            String endpointPath = "/" + parts[i];

            IdRef<TT> currentIdRef = null;

            if (!isString(parts[i + 1])) {
                Long asLong = Long.valueOf(parts[i + 1]);
                currentIdRef = (IdRef<TT>) create(r, getIdRefClazz(r, endpointPath), asLong);
            } else {
                String asString = parts[i + 1];<commit_msg>IdRef parse on blank path<commit_after>package io.yawp.repository;

import io.yawp.commons.http.HttpVerb;
import io.yawp.repository.actions.ActionKey;
import io.yawp.repository.query.QueryBuilder;

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

import org.apache.commons.lang3.StringUtils;

public class IdRef<T> implements Comparable<IdRef<T>> {

    private Repository r;

    private Class<T> clazz;

    private ObjectModel model;

    private Long id;

    private String name;

    private IdRef<?> parentId;

    protected IdRef(Repository r, Class<T> clazz, Long id) {
        this.r = r;
        this.clazz = clazz;
        this.id = id;
        this.model = new ObjectModel(clazz);
    }

    public IdRef(Repository r, Class<T> clazz, String name) {
        this.r = r;
        this.clazz = clazz;
        this.name = name;
        this.model = new ObjectModel(clazz);
    }

    public void setParentId(IdRef<?> parentId) {
        this.parentId = parentId;
    }

    public T fetch() {
        return fetch(clazz);
    }

    public <TT> TT fetch(Class<TT> childClazz) {
        return r.query(childClazz).fetch(this);
    }

    public <TT> TT child(Class<TT> childClazz) {
        QueryBuilder<TT> q = r.query(childClazz).from(this);
        return q.only();
    }

    public Long asLong() {
        return id;
    }

    public String asString() {
        return name;
    }

    public Repository getRepository() {
        return r;
    }

    public Class<T> getClazz() {
        return clazz;
    }

    public Class<?> getParentClazz() {
        return model.getParentClazz();
    }

    public ObjectModel getModel() {
        return new ObjectModel(clazz);
    }

    @SuppressWarnings("unchecked")
    public <TT> IdRef<TT> getParentId() {
        return (IdRef<TT>) parentId;
    }

    @SuppressWarnings("unchecked")
    public <TT> IdRef<TT> getAncestorId(int ancestor) {
        IdRef<?> ancestorId = this;
        for (int i = 0; i <= ancestor; i++) {
            ancestorId = ancestorId.getParentId();
        }
        return (IdRef<TT>) ancestorId;
    }

    public <TT> IdRef<TT> createChildId(Class<TT> clazz, Long id) {
        IdRef<TT> idRef = new IdRef<TT>(r, clazz, id);
        idRef.setParentId(this);
        return idRef;
    }

    public <TT> IdRef<TT> createChildId(Class<TT> clazz, String name) {
        IdRef<TT> idRef = new IdRef<TT>(r, clazz, name);
        idRef.setParentId(this);
        return idRef;
    }

    public static <TT> IdRef<TT> create(Repository r, Class<TT> clazz, Long id) {
        return new IdRef<TT>(r, clazz, id);
    }

    public static <TT> IdRef<TT> create(Repository r, Class<TT> clazz, String name) {
        return new IdRef<TT>(r, clazz, name);
    }

    public static <TT> List<IdRef<TT>> create(Repository r, Class<TT> clazz, Long... ids) {
        List<IdRef<TT>> idRefs = new ArrayList<IdRef<TT>>();
        for (int i = 0; i < ids.length; i++) {
            idRefs.add(create(r, clazz, ids[i]));
        }
        return idRefs;
    }

    @SuppressWarnings("unchecked")
    public static <TT> IdRef<TT> parse(Repository r, HttpVerb verb, String path) {
		if (StringUtils.isBlank(path)) {
			return null;
		}
		
        String[] parts = path.substring(1).split("/");

        if (parts.length < 2) {
            return null;
        }

        IdRef<TT> lastIdRef = null;

        for (int i = 0; i < parts.length; i += 2) {
            if (isActionOrCollection(r, verb, parts, i)) {
                break;
            }

            String endpointPath = "/" + parts[i];

            IdRef<TT> currentIdRef = null;

            if (!isString(parts[i + 1])) {
                Long asLong = Long.valueOf(parts[i + 1]);
                currentIdRef = (IdRef<TT>) create(r, getIdRefClazz(r, endpointPath), asLong);
            } else {
                String asString = parts[i + 1];
{'fraction_non_alphanumeric': 0.12951807228915663, 'fraction_numerical': 0.00248821372446307, 'mean_word_length': 2.240135765803988, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 109, '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': '6609794', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2483, 'n_words': 615}