<commit_before>/*
 * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.model.util.type;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import java.util.Objects;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;

// TODO: this should be in the API package, as it defines equality for TypeDefinitions
final class TypeDefinitions {
    private TypeDefinitions() {
        throw new UnsupportedOperationException();
    }

    private static int basicHashCode(final TypeDefinition<?> type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null));
    }

    private static <T extends TypeDefinition<T>> T castIfEquals(final Class<T> clazz, final T type, final Object obj) {
        if (!clazz.isInstance(obj)) {
            return null;
        }

        final T other = clazz.cast(obj);
        return Objects.equals(type.getPath(), other.getPath())
                && Objects.equals(type.getBaseType(), other.getBaseType())
                && Objects.equals(type.getDefaultValue(), other.getDefaultValue())
                && Objects.equals(type.getUnknownSchemaNodes(), other.getUnknownSchemaNodes())
                && Objects.equals(type.getUnits(), other.getUnits()) ? other : null;
    }

    private static ToStringHelper toStringHelper(final TypeDefinition<?> type) {
        return MoreObjects.toStringHelper(type).omitNullValues()
                .add("baseType", type.getBaseType())
                .add("default", type.getDefaultValue().orElse(null))
                .add("description", type.getDescription().orElse(null))
                .add("path", type.getPath())
                .add("reference", type.getReference().orElse(null))
                .add("status", type.getStatus())
                .add("units", type.getUnits().orElse(null));
    }

    static int hashCode(final BinaryTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null));
    }

    static int hashCode(final BitsTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getBits());
    }


    static int hashCode(final BooleanTypeDefinition type) {
        return basicHashCode(type);
    }

    static int hashCode(final DecimalTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getFractionDigits(),
            type.getRangeConstraint().orElse(null));
    }

    static int hashCode(final EmptyTypeDefinition type) {
        return basicHashCode(type);
    }

    static int hashCode(final EnumTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null),
            type.getDefaultValue(), type.getValues());
    }

    static int hashCode(final IdentityrefTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getIdentities());
    }

    static int hashCode(final InstanceIdentifierTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.requireInstance());
    }

    static int hashCode(final IntegerTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getRangeConstraint().orElse(null));
    }

    static int hashCode(final LeafrefTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getPathStatement());
    }

    static int hashCode(final StringTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null),
            type.getPatternConstraints());
    }

    static int hashCode(final UnionTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getTypes());
    }

    static int hashCode(final UnsignedIntegerTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
            type.getDefaultValue(), type.getRangeConstraint());
    }

    static boolean equals(final BinaryTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
        return other != null && type.getLengthConstraint().equals(other.getLengthConstraint());
    }

    static boolean equals(final BitsTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
        return other != null && type.getBits().equals(other.getBits());
    }

    static boolean equals(final BooleanTypeDefinition type, final Object obj) {
        return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
    }

    static boolean equals(final DecimalTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
        return other != null && type.getFractionDigits().equals(other.getFractionDigits())
                && type.getRangeConstraint().equals(other.getRangeConstraint());
    }

    static boolean equals(final EmptyTypeDefinition type, final Object obj) {
        return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
    }

    static boolean equals(final EnumTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
        return other != null && type.getValues().equals(other.getValues());
    }

    static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
        return other != null && type.getIdentity().equals(other.getIdentity());
    }

    static boolean equals(final InstanceIdentifierTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final InstanceIdentifierTypeDefinition other = castIfEquals(InstanceIdentifierTypeDefinition.class, type, obj);
        return other != null && type.requireInstance() == other.requireInstance();
    }

    static boolean equals(final IntegerTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final IntegerTypeDefinition other = castIfEquals(IntegerTypeDefinition.class, type, obj);
        return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
    }

    static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }<commit_msg>Fix IdentityrefTypeDefinition comparison<commit_after>/*
 * Copyright (c) 2015 <NAME> s.r.o. 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.model.util.type;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import java.util.Objects;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;

// TODO: this should be in the API package, as it defines equality for TypeDefinitions
final class TypeDefinitions {
    private TypeDefinitions() {
        throw new UnsupportedOperationException();
    }

    private static int basicHashCode(final TypeDefinition<?> type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null));
    }

    private static <T extends TypeDefinition<T>> T castIfEquals(final Class<T> clazz, final T type, final Object obj) {
        if (!clazz.isInstance(obj)) {
            return null;
        }

        final T other = clazz.cast(obj);
        return Objects.equals(type.getPath(), other.getPath())
                && Objects.equals(type.getBaseType(), other.getBaseType())
                && Objects.equals(type.getDefaultValue(), other.getDefaultValue())
                && Objects.equals(type.getUnknownSchemaNodes(), other.getUnknownSchemaNodes())
                && Objects.equals(type.getUnits(), other.getUnits()) ? other : null;
    }

    private static ToStringHelper toStringHelper(final TypeDefinition<?> type) {
        return MoreObjects.toStringHelper(type).omitNullValues()
                .add("baseType", type.getBaseType())
                .add("default", type.getDefaultValue().orElse(null))
                .add("description", type.getDescription().orElse(null))
                .add("path", type.getPath())
                .add("reference", type.getReference().orElse(null))
                .add("status", type.getStatus())
                .add("units", type.getUnits().orElse(null));
    }

    static int hashCode(final BinaryTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null));
    }

    static int hashCode(final BitsTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getBits());
    }


    static int hashCode(final BooleanTypeDefinition type) {
        return basicHashCode(type);
    }

    static int hashCode(final DecimalTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getFractionDigits(),
            type.getRangeConstraint().orElse(null));
    }

    static int hashCode(final EmptyTypeDefinition type) {
        return basicHashCode(type);
    }

    static int hashCode(final EnumTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null),
            type.getDefaultValue(), type.getValues());
    }

    static int hashCode(final IdentityrefTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getIdentities());
    }

    static int hashCode(final InstanceIdentifierTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.requireInstance());
    }

    static int hashCode(final IntegerTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getRangeConstraint().orElse(null));
    }

    static int hashCode(final LeafrefTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getPathStatement());
    }

    static int hashCode(final StringTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null),
            type.getPatternConstraints());
    }

    static int hashCode(final UnionTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
            type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getTypes());
    }

    static int hashCode(final UnsignedIntegerTypeDefinition type) {
        return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
            type.getDefaultValue(), type.getRangeConstraint());
    }

    static boolean equals(final BinaryTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
        return other != null && type.getLengthConstraint().equals(other.getLengthConstraint());
    }

    static boolean equals(final BitsTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
        return other != null && type.getBits().equals(other.getBits());
    }

    static boolean equals(final BooleanTypeDefinition type, final Object obj) {
        return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
    }

    static boolean equals(final DecimalTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
        return other != null && type.getFractionDigits().equals(other.getFractionDigits())
                && type.getRangeConstraint().equals(other.getRangeConstraint());
    }

    static boolean equals(final EmptyTypeDefinition type, final Object obj) {
        return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
    }

    static boolean equals(final EnumTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
        return other != null && type.getValues().equals(other.getValues());
    }

    static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
        return other != null && type.getIdentities().equals(other.getIdentities());
    }

    static boolean equals(final InstanceIdentifierTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final InstanceIdentifierTypeDefinition other = castIfEquals(InstanceIdentifierTypeDefinition.class, type, obj);
        return other != null && type.requireInstance() == other.requireInstance();
    }

    static boolean equals(final IntegerTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }

        final IntegerTypeDefinition other = castIfEquals(IntegerTypeDefinition.class, type, obj);
        return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
    }

    static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
        if (type == obj) {
            return true;
        }
{'fraction_non_alphanumeric': 0.11698073258522126, 'fraction_numerical': 0.0008469193309337286, 'mean_word_length': 3.4833887043189367, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '6005391', 'n_tokens_mistral': 5215, 'n_tokens_neox': 5001, 'n_words': 1043}