package org.bouncycastle.oer;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bouncycastle.asn1.ASN1Encodable;

/* JADX INFO: loaded from: classes.dex */
public class Element {
    private final Switch aSwitch;
    private final OERDefinition.BaseType baseType;
    private final int block;
    private final List<Element> children;
    private final ASN1Encodable defaultValue;
    private final boolean defaultValuesInChildren;
    private final ElementSupplier elementSupplier;
    private final BigInteger enumValue;
    private final boolean explicit;
    private final boolean extensionsInDefinition;
    private final String label;
    private final BigInteger lowerBound;
    private final boolean mayRecurse;
    private List<Element> optionalChildrenInOrder;
    private final int optionals;
    private Element parent;
    private final Map<String, ElementSupplier> supplierMap;
    private final String typeName;
    private final BigInteger upperBound;
    private List<ASN1Encodable> validSwitchValues;

    public Element(Element element, Element element2) {
        this.baseType = element.baseType;
        this.children = new ArrayList(element.children);
        this.explicit = element.explicit;
        this.label = element.label;
        this.lowerBound = element.lowerBound;
        this.upperBound = element.upperBound;
        this.extensionsInDefinition = element.extensionsInDefinition;
        this.enumValue = element.enumValue;
        this.defaultValue = element.defaultValue;
        this.aSwitch = element.aSwitch;
        this.validSwitchValues = element.validSwitchValues;
        this.elementSupplier = element.elementSupplier;
        this.mayRecurse = element.mayRecurse;
        this.typeName = element.typeName;
        this.supplierMap = element.supplierMap;
        this.parent = element2;
        this.block = element.block;
        this.optionals = element.optionals;
        this.defaultValuesInChildren = element.defaultValuesInChildren;
        Iterator<Element> it = this.children.iterator();
        while (it.hasNext()) {
            it.next().parent = this;
        }
    }

    public Element(OERDefinition.BaseType baseType, List<Element> list, boolean z, String str, BigInteger bigInteger, BigInteger bigInteger2, boolean z2, BigInteger bigInteger3, ASN1Encodable aSN1Encodable, Switch r11, List<ASN1Encodable> list2, ElementSupplier elementSupplier, boolean z3, String str2, Map<String, ElementSupplier> map, int i, int i2, boolean z4) {
        this.baseType = baseType;
        this.children = list;
        this.explicit = z;
        this.label = str;
        this.lowerBound = bigInteger;
        this.upperBound = bigInteger2;
        this.extensionsInDefinition = z2;
        this.enumValue = bigInteger3;
        this.defaultValue = aSN1Encodable;
        this.aSwitch = r11;
        this.validSwitchValues = list2 != null ? Collections.unmodifiableList(list2) : null;
        this.elementSupplier = elementSupplier;
        this.mayRecurse = z3;
        this.typeName = str2;
        this.block = i;
        this.optionals = i2;
        this.defaultValuesInChildren = z4;
        if (map == null) {
            this.supplierMap = Collections.emptyMap();
        } else {
            this.supplierMap = map;
        }
        Iterator<Element> it = list.iterator();
        while (it.hasNext()) {
            it.next().parent = this;
        }
    }

    public static Element expandDeferredDefinition(Element element, Element element2) {
        if (element.elementSupplier == null) {
            return element;
        }
        Element elementBuild = element.elementSupplier.build();
        return elementBuild.getParent() != element2 ? new Element(elementBuild, element2) : elementBuild;
    }

    public String appendLabel(String str) {
        return "[" + (getLabel() == null ? "" : getLabel()) + (isExplicit() ? " (E)" : "") + "] " + str;
    }

    public boolean canBeNegative() {
        return getLowerBound() != null && BigInteger.ZERO.compareTo(getLowerBound()) > 0;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Element element = (Element) obj;
        if (this.explicit != element.explicit || this.extensionsInDefinition != element.extensionsInDefinition || this.defaultValuesInChildren != element.defaultValuesInChildren || this.mayRecurse != element.mayRecurse || this.optionals != element.optionals || this.block != element.block || this.baseType != element.baseType) {
            return false;
        }
        if (this.children == null ? element.children != null : !this.children.equals(element.children)) {
            return false;
        }
        if (this.label == null ? element.label != null : !this.label.equals(element.label)) {
            return false;
        }
        if (this.lowerBound == null ? element.lowerBound != null : !this.lowerBound.equals(element.lowerBound)) {
            return false;
        }
        if (this.upperBound == null ? element.upperBound != null : !this.upperBound.equals(element.upperBound)) {
            return false;
        }
        if (this.enumValue == null ? element.enumValue != null : !this.enumValue.equals(element.enumValue)) {
            return false;
        }
        if (this.defaultValue == null ? element.defaultValue != null : !this.defaultValue.equals(element.defaultValue)) {
            return false;
        }
        if (this.aSwitch == null ? element.aSwitch != null : !this.aSwitch.equals(element.aSwitch)) {
            return false;
        }
        if (this.optionalChildrenInOrder == null ? element.optionalChildrenInOrder != null : !this.optionalChildrenInOrder.equals(element.optionalChildrenInOrder)) {
            return false;
        }
        if (this.validSwitchValues == null ? element.validSwitchValues != null : !this.validSwitchValues.equals(element.validSwitchValues)) {
            return false;
        }
        if (this.elementSupplier == null ? element.elementSupplier != null : !this.elementSupplier.equals(element.elementSupplier)) {
            return false;
        }
        if (this.typeName == null ? element.typeName != null : !this.typeName.equals(element.typeName)) {
            return false;
        }
        if (this.supplierMap != null) {
            if (!this.supplierMap.equals(element.supplierMap)) {
                return true;
            }
        } else if (element.supplierMap != null) {
            return true;
        }
        return false;
    }

    public OERDefinition.BaseType getBaseType() {
        return this.baseType;
    }

    public int getBlock() {
        return this.block;
    }

    public List<Element> getChildren() {
        return this.children;
    }

    public ASN1Encodable getDefaultValue() {
        return this.defaultValue;
    }

    public String getDerivedTypeName() {
        return this.typeName != null ? this.typeName : this.baseType.name();
    }

    public ElementSupplier getElementSupplier() {
        return this.elementSupplier;
    }

    public BigInteger getEnumValue() {
        return this.enumValue;
    }

    public Element getFirstChid() {
        return getChildren().get(0);
    }

    public String getLabel() {
        return this.label;
    }

    public BigInteger getLowerBound() {
        return this.lowerBound;
    }

    public List<Element> getOptionalChildrenInOrder() {
        return this.optionalChildrenInOrder;
    }

    public int getOptionals() {
        return this.optionals;
    }

    public Element getParent() {
        return this.parent;
    }

    public String getTypeName() {
        return this.typeName;
    }

    public BigInteger getUpperBound() {
        return this.upperBound;
    }

    public List<ASN1Encodable> getValidSwitchValues() {
        return this.validSwitchValues;
    }

    public Switch getaSwitch() {
        return this.aSwitch;
    }

    public boolean hasDefaultChildren() {
        return this.defaultValuesInChildren;
    }

    public boolean hasPopulatedExtension() {
        return this.extensionsInDefinition;
    }

    public int hashCode() {
        return ((((((((((((((((((((((((((((((((((((this.baseType != null ? this.baseType.hashCode() : 0) * 31) + (this.children != null ? this.children.hashCode() : 0)) * 31) + (this.explicit ? 1 : 0)) * 31) + (this.label != null ? this.label.hashCode() : 0)) * 31) + (this.lowerBound != null ? this.lowerBound.hashCode() : 0)) * 31) + (this.upperBound != null ? this.upperBound.hashCode() : 0)) * 31) + (this.extensionsInDefinition ? 1 : 0)) * 31) + (this.enumValue != null ? this.enumValue.hashCode() : 0)) * 31) + (this.defaultValue != null ? this.defaultValue.hashCode() : 0)) * 31) + (this.aSwitch != null ? this.aSwitch.hashCode() : 0)) * 31) + (this.defaultValuesInChildren ? 1 : 0)) * 31) + (this.optionalChildrenInOrder != null ? this.optionalChildrenInOrder.hashCode() : 0)) * 31) + (this.validSwitchValues != null ? this.validSwitchValues.hashCode() : 0)) * 31) + (this.elementSupplier != null ? this.elementSupplier.hashCode() : 0)) * 31) + (this.mayRecurse ? 1 : 0)) * 31) + (this.typeName != null ? this.typeName.hashCode() : 0)) * 31) + (this.supplierMap != null ? this.supplierMap.hashCode() : 0)) * 31) + this.optionals) * 31) + this.block;
    }

    public int intBytesForRange() {
        if (getLowerBound() != null && getUpperBound() != null) {
            int i = 1;
            if (BigInteger.ZERO.equals(getLowerBound())) {
                int i2 = 0;
                while (i2 < OERDefinition.uIntMax.length) {
                    if (getUpperBound().compareTo(OERDefinition.uIntMax[i2]) < 0) {
                        return i;
                    }
                    i2++;
                    i *= 2;
                }
            } else {
                int i3 = 0;
                int i4 = 1;
                while (i3 < OERDefinition.sIntRange.length) {
                    if (getLowerBound().compareTo(OERDefinition.sIntRange[i3][0]) >= 0 && getUpperBound().compareTo(OERDefinition.sIntRange[i3][1]) < 0) {
                        return -i4;
                    }
                    i3++;
                    i4 *= 2;
                }
            }
        }
        return 0;
    }

    public boolean isExplicit() {
        return this.explicit;
    }

    public boolean isExtensionsInDefinition() {
        return this.extensionsInDefinition;
    }

    public boolean isFixedLength() {
        return getLowerBound() != null && getLowerBound().equals(getUpperBound());
    }

    public boolean isLowerRangeZero() {
        return BigInteger.ZERO.equals(getLowerBound());
    }

    public boolean isMayRecurse() {
        return this.mayRecurse;
    }

    public boolean isUnbounded() {
        return getUpperBound() == null && getLowerBound() == null;
    }

    public boolean isUnsignedWithRange() {
        return isLowerRangeZero() && getUpperBound() != null && BigInteger.ZERO.compareTo(getUpperBound()) < 0;
    }

    public List<Element> optionalOrDefaultChildrenInOrder() {
        List<Element> optionalChildrenInOrder;
        synchronized (this) {
            if (getOptionalChildrenInOrder() == null) {
                ArrayList arrayList = new ArrayList();
                for (Element element : getChildren()) {
                    if (!element.isExplicit() || element.getDefaultValue() != null) {
                        arrayList.add(element);
                    }
                }
                this.optionalChildrenInOrder = Collections.unmodifiableList(arrayList);
            }
            optionalChildrenInOrder = getOptionalChildrenInOrder();
        }
        return optionalChildrenInOrder;
    }

    public String rangeExpression() {
        return "(" + (getLowerBound() != null ? getLowerBound().toString() : "MIN") + " ... " + (getUpperBound() != null ? getUpperBound().toString() : "MAX") + ")";
    }

    public ElementSupplier resolveSupplier() {
        if (this.supplierMap.containsKey(this.label)) {
            return this.supplierMap.get(this.label);
        }
        if (this.parent != null) {
            return this.parent.resolveSupplier(this.label);
        }
        throw new IllegalStateException("unable to resolve: " + this.label);
    }

    protected ElementSupplier resolveSupplier(String str) {
        String str2 = this.label + "." + str;
        if (this.supplierMap.containsKey(str2)) {
            return this.supplierMap.get(str2);
        }
        if (this.parent != null) {
            return this.parent.resolveSupplier(str2);
        }
        throw new IllegalStateException("unable to resolve: " + str2);
    }

    public String toString() {
        return "[" + this.typeName + " " + this.baseType.name() + " '" + getLabel() + "']";
    }
}
