<commit_before>/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;

import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder;
import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.PatternEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.TypeDefinitionEffectiveBuilder;
import org.opendaylight.yangtools.yang.parser.util.TypeConstraints;

public class ExtendedTypeEffectiveStatementImpl extends EffectiveStatementBase<String, TypeStatement> implements
        TypeDefinition<TypeDefinition<?>>, TypeDefinitionEffectiveBuilder {

    private static final Splitter COLON_SPLITTER = Splitter.on(':').trimResults();

    private final QName qName;
    private final SchemaPath path;

    private final TypeDefinition<?> baseType;

    private final String defaultValue = null;
    private final String units = null;

    private final String description = null;
    private final String reference = null;

    private final Status status = null;

    private final List<RangeConstraint> ranges;
    private final List<LengthConstraint> lengths;
    private final List<PatternConstraint> patterns;
    private final Integer fractionDigits;

    private ExtendedType extendedType = null;
    private final boolean isExtended;

    public ExtendedTypeEffectiveStatementImpl(
            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx, final boolean isExtended) {
        super(ctx);

        this.isExtended = isExtended;
        qName = initQName(ctx, isExtended);

        final StmtContext<?, TypedefStatement, EffectiveStatement<QName, TypedefStatement>> typeStmt =
                ctx.getFromNamespace(TypeNamespace.class, qName);
        if (typeStmt == null) {
            path = Utils.getSchemaPath(ctx);
        } else {
            path = Utils.getSchemaPath(ctx.getFromNamespace(TypeNamespace.class, qName));
        }

        ranges = initRanges();
        lengths = initLengths();
        patterns = initPatterns();
        fractionDigits = initFractionDigits();

        baseType = parseBaseTypeFromCtx(ctx);
        validateTypeConstraints(ctx);
    }

    private static QName initQName(final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
            final boolean isExtended) {

        QName qName;

        if (isExtended) {
            final Splitter colonSplitter = Splitter.on(":").trimResults();
            final List<String> nameTokens = colonSplitter.splitToList(ctx.getStatementArgument());

            switch (nameTokens.size()) {
            case 1:
                qName = QName.create(Utils.getRootModuleQName(ctx), nameTokens.get(0));
                break;
            case 2:
                qName = QName.create(Utils.getRootModuleQName(ctx), nameTokens.get(1));
                break;
            default:
                throw new IllegalArgumentException(String.format(<commit_msg>Use shared Splitter instance<commit_after>/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;

import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder;
import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.PatternEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.TypeDefinitionEffectiveBuilder;
import org.opendaylight.yangtools.yang.parser.util.TypeConstraints;

public class ExtendedTypeEffectiveStatementImpl extends EffectiveStatementBase<String, TypeStatement> implements
        TypeDefinition<TypeDefinition<?>>, TypeDefinitionEffectiveBuilder {

    private static final Splitter COLON_SPLITTER = Splitter.on(':').trimResults();

    private final QName qName;
    private final SchemaPath path;

    private final TypeDefinition<?> baseType;

    private final String defaultValue = null;
    private final String units = null;

    private final String description = null;
    private final String reference = null;

    private final Status status = null;

    private final List<RangeConstraint> ranges;
    private final List<LengthConstraint> lengths;
    private final List<PatternConstraint> patterns;
    private final Integer fractionDigits;

    private ExtendedType extendedType = null;
    private final boolean isExtended;

    public ExtendedTypeEffectiveStatementImpl(
            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx, final boolean isExtended) {
        super(ctx);

        this.isExtended = isExtended;
        qName = initQName(ctx, isExtended);

        final StmtContext<?, TypedefStatement, EffectiveStatement<QName, TypedefStatement>> typeStmt =
                ctx.getFromNamespace(TypeNamespace.class, qName);
        if (typeStmt == null) {
            path = Utils.getSchemaPath(ctx);
        } else {
            path = Utils.getSchemaPath(ctx.getFromNamespace(TypeNamespace.class, qName));
        }

        ranges = initRanges();
        lengths = initLengths();
        patterns = initPatterns();
        fractionDigits = initFractionDigits();

        baseType = parseBaseTypeFromCtx(ctx);
        validateTypeConstraints(ctx);
    }

    private static QName initQName(final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
            final boolean isExtended) {

        QName qName;

        if (isExtended) {
            final List<String> nameTokens = COLON_SPLITTER.splitToList(ctx.getStatementArgument());

            switch (nameTokens.size()) {
            case 1:
                qName = QName.create(Utils.getRootModuleQName(ctx), nameTokens.get(0));
                break;
            case 2:
                qName = QName.create(Utils.getRootModuleQName(ctx), nameTokens.get(1));
                break;
            default:
                throw new IllegalArgumentException(String.format(
{'fraction_non_alphanumeric': 0.09282825293479333, 'fraction_numerical': 0.00789188122718753, 'mean_word_length': 4.169811320754717, 'pattern_counts': {'":': 1, '<': 31, '<?xml version=': 0, '>': 31, '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': '4160655', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2854, 'n_words': 534}