<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.dna.spi.graph.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import net.jcip.annotations.Immutable;
import org.jboss.dna.common.CommonI18n;
import org.jboss.dna.common.text.Inflector;
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.common.util.ArgCheck;
import org.jboss.dna.spi.SpiI18n;
import org.jboss.dna.spi.graph.InvalidPathException;
import org.jboss.dna.spi.graph.NamespaceRegistry;
import org.jboss.dna.spi.graph.Path;
import org.jboss.dna.spi.graph.PathNotFoundException;

/**
 * A basic implementation of {@link Path}.
 * 
 * @author <NAME>
 * @author <NAME>
 */
@Immutable
public class BasicPath implements Path {

	/**
     */
    private static final long serialVersionUID = 8488295345524209746L;

    private static final List<Segment> EMPTY_SEGMENTS = Collections.emptyList();

    public static final Path ROOT = new BasicPath(EMPTY_SEGMENTS, true);

    protected static final Path SELF_PATH = new BasicPath(Collections.singletonList(Path.SELF_SEGMENT), false);

    private final List<Segment> segments;
    private final boolean absolute;
    private final boolean normalized;
    private transient String path;

    /**
     * @param segments the segments
     * @param absolute true if this path is absolute, or false otherwise
     */
    public BasicPath( List<Segment> segments,
                      boolean absolute ) {
        ArgCheck.isNotNull(segments, "segments");
        this.segments = segments.isEmpty() ? EMPTY_SEGMENTS : Collections.unmodifiableList(segments);
        this.absolute = absolute;
        this.normalized = isNormalized(this.segments);
    }

    protected boolean isNormalized( List<Segment> segments ) {
        for (Segment segment : segments) {
            if (segment.isSelfReference() || segment.isParentReference()) return false;
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public Path getAncestor() {
        if (this.isRoot()) return this;
        if (this.segments.size() == 1) return ROOT;
        return subpath(0, this.segments.size() - 1);
    }

    /**
     * {@inheritDoc}
     */
    public Path getAncestor( int degree ) {
        ArgCheck.isNonNegative(degree, "degree");
        if (this.isRoot()) return this;
        if (degree == 0) return this;
        int endIndex = this.segments.size() - degree;
        if (endIndex < 0) {
            String msg = SpiI18n.pathAncestorDegreeIsInvalid.text(this.getString(), Inflector.getInstance().ordinalize(degree));
            throw new PathNotFoundException(msg);
        }
        return subpath(0, endIndex);
    }

    /**
     * {@inheritDoc}
     */
    public Path getCanonicalPath() {
        if (!this.isAbsolute()) {
            String msg = SpiI18n.pathIsNotAbsolute.text(this);<commit_msg>DNA-115 - Create federation service  http://jira.jboss.com/jira/browse/DNA-115<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.dna.spi.graph.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import net.jcip.annotations.Immutable;
import org.jboss.dna.common.CommonI18n;
import org.jboss.dna.common.text.Inflector;
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.common.util.ArgCheck;
import org.jboss.dna.spi.SpiI18n;
import org.jboss.dna.spi.graph.InvalidPathException;
import org.jboss.dna.spi.graph.NamespaceRegistry;
import org.jboss.dna.spi.graph.Path;
import org.jboss.dna.spi.graph.PathNotFoundException;

/**
 * A basic implementation of {@link Path}.
 * 
 * @author <NAME>
 * @author <NAME>
 */
@Immutable
public class BasicPath implements Path {

    /**
     */
    private static final long serialVersionUID = 8488295345524209746L;

    private static final List<Segment> EMPTY_SEGMENTS = Collections.emptyList();

    public static final Path ROOT = new BasicPath(EMPTY_SEGMENTS, true);

    protected static final Path SELF_PATH = new BasicPath(Collections.singletonList(Path.SELF_SEGMENT), false);

    private final List<Segment> segments;
    private final boolean absolute;
    private final boolean normalized;
    private transient String path;

    /**
     * @param segments the segments
     * @param absolute true if this path is absolute, or false otherwise
     */
    public BasicPath( List<Segment> segments,
                      boolean absolute ) {
        ArgCheck.isNotNull(segments, "segments");
        this.segments = segments.isEmpty() ? EMPTY_SEGMENTS : Collections.unmodifiableList(segments);
        this.absolute = absolute;
        this.normalized = isNormalized(this.segments);
    }

    protected boolean isNormalized( List<Segment> segments ) {
        for (Segment segment : segments) {
            if (segment.isSelfReference() || segment.isParentReference()) return false;
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public Path getAncestor() {
        if (this.isRoot()) return this;
        if (this.segments.size() == 1) return ROOT;
        return subpath(0, this.segments.size() - 1);
    }

    /**
     * {@inheritDoc}
     */
    public Path getAncestor( int degree ) {
        ArgCheck.isNonNegative(degree, "degree");
        if (this.isRoot()) return this;
        if (degree == 0) return this;
        int endIndex = this.segments.size() - degree;
        if (endIndex < 0) {
            String msg = SpiI18n.pathAncestorDegreeIsInvalid.text(this.getString(), Inflector.getInstance().ordinalize(degree));
            throw new PathNotFoundException(this, msg);
        }
        return subpath(0, endIndex);
    }

    /**
     * {@inheritDoc}
     */
    public Path getCanonicalPath() {
        if (!this.isAbsolute()) {
            String msg = SpiI18n.pathIsNotAbsolute.text(this);
{'fraction_non_alphanumeric': 0.09969788519637462, 'fraction_numerical': 0.013343403826787512, 'mean_word_length': 3.166229680125852, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6614411', 'n_tokens_mistral': 2386, 'n_tokens_neox': 2261, 'n_words': 716}