/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.lang.Object
 *  java.lang.String
 *  java.util.ArrayList
 *  java.util.HashMap
 *  java.util.Iterator
 *  java.util.List
 *  java.util.Map
 */
package org.dom4j.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.QName;
import org.dom4j.tree.BackedList;
import org.dom4j.tree.DefaultElement;

public class IndexedElement
extends DefaultElement {
    private Map attributeIndex;
    private Map elementIndex;

    public IndexedElement(String string2) {
        super(string2);
    }

    public IndexedElement(QName qName) {
        super(qName);
    }

    public IndexedElement(QName qName, int n) {
        super(qName, n);
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    protected void addNode(Node node) {
        super.addNode(node);
        if (this.elementIndex != null && node instanceof Element) {
            this.addToElementIndex((Element)node);
            return;
        } else {
            if (this.attributeIndex == null || !(node instanceof Attribute)) return;
            {
                this.addToAttributeIndex((Attribute)node);
                return;
            }
        }
    }

    protected void addToAttributeIndex(Object object, Attribute attribute) {
        if (this.attributeIndex.get(object) != null) {
            this.attributeIndex.put(object, (Object)attribute);
        }
    }

    protected void addToAttributeIndex(Attribute attribute) {
        QName qName = attribute.getQName();
        String string2 = qName.getName();
        this.addToAttributeIndex(qName, attribute);
        this.addToAttributeIndex(string2, attribute);
    }

    protected void addToElementIndex(Object object, Element element) {
        Object object2 = this.elementIndex.get(object);
        if (object2 == null) {
            this.elementIndex.put(object, (Object)element);
            return;
        }
        if (object2 instanceof List) {
            ((List)object2).add((Object)element);
            return;
        }
        List list = this.createList();
        list.add(object2);
        list.add((Object)element);
        this.elementIndex.put(object, (Object)list);
    }

    protected void addToElementIndex(Element element) {
        QName qName = element.getQName();
        String string2 = qName.getName();
        this.addToElementIndex(qName, element);
        this.addToElementIndex(string2, element);
    }

    protected Element asElement(Object object) {
        List list;
        if (object instanceof Element) {
            return (Element)object;
        }
        if (object != null && (list = (List)object).size() >= 1) {
            return (Element)list.get(0);
        }
        return null;
    }

    protected Iterator asElementIterator(Object object) {
        return this.asElementList(object).iterator();
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected List asElementList(Object object) {
        if (object instanceof Element) {
            return this.createSingleResultList(object);
        }
        if (object == null) return this.createEmptyList();
        List list = (List)object;
        BackedList backedList = this.createResultList();
        int n = 0;
        int n2 = list.size();
        while (n < n2) {
            backedList.addLocal(list.get(n));
            ++n;
        }
        return backedList;
    }

    @Override
    public Attribute attribute(String string2) {
        return (Attribute)this.attributeIndex().get((Object)string2);
    }

    @Override
    public Attribute attribute(QName qName) {
        return (Attribute)this.attributeIndex().get((Object)qName);
    }

    protected Map attributeIndex() {
        if (this.attributeIndex == null) {
            this.attributeIndex = this.createAttributeIndex();
            Iterator iterator = this.attributeIterator();
            while (iterator.hasNext()) {
                this.addToAttributeIndex((Attribute)iterator.next());
            }
        }
        return this.attributeIndex;
    }

    protected Map createAttributeIndex() {
        return this.createIndex();
    }

    protected Map createElementIndex() {
        return this.createIndex();
    }

    protected Map createIndex() {
        return new HashMap();
    }

    protected List createList() {
        return new ArrayList();
    }

    @Override
    public Element element(String string2) {
        return this.asElement(this.elementIndex().get((Object)string2));
    }

    @Override
    public Element element(QName qName) {
        return this.asElement(this.elementIndex().get((Object)qName));
    }

    protected Map elementIndex() {
        if (this.elementIndex == null) {
            this.elementIndex = this.createElementIndex();
            Iterator iterator = this.elementIterator();
            while (iterator.hasNext()) {
                this.addToElementIndex((Element)iterator.next());
            }
        }
        return this.elementIndex;
    }

    @Override
    public List elements(String string2) {
        return this.asElementList(this.elementIndex().get((Object)string2));
    }

    @Override
    public List elements(QName qName) {
        return this.asElementList(this.elementIndex().get((Object)qName));
    }

    protected void removeFromAttributeIndex(Object object, Attribute attribute) {
        Object object2 = this.attributeIndex.get(object);
        if (object2 != null && object2.equals((Object)attribute)) {
            this.attributeIndex.remove(object);
        }
    }

    protected void removeFromAttributeIndex(Attribute attribute) {
        QName qName = attribute.getQName();
        String string2 = qName.getName();
        this.removeFromAttributeIndex(qName, attribute);
        this.removeFromAttributeIndex(string2, attribute);
    }

    protected void removeFromElementIndex(Object object, Element element) {
        Object object2 = this.elementIndex.get(object);
        if (object2 instanceof List) {
            ((List)object2).remove((Object)element);
            return;
        }
        this.elementIndex.remove(object);
    }

    protected void removeFromElementIndex(Element element) {
        QName qName = element.getQName();
        String string2 = qName.getName();
        this.removeFromElementIndex(qName, element);
        this.removeFromElementIndex(string2, element);
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    protected boolean removeNode(Node node) {
        if (!super.removeNode(node)) return false;
        if (this.elementIndex != null && node instanceof Element) {
            this.removeFromElementIndex((Element)node);
            return true;
        }
        if (this.attributeIndex == null) return true;
        if (!(node instanceof Attribute)) return true;
        this.removeFromAttributeIndex((Attribute)node);
        return true;
    }
}

