<commit_before>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library 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;
 *    version 2.1 of the License.
 *
 *    This library 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.
 */
package org.geotools.xml.impl;

import org.apache.commons.collections.OrderedMap;
import org.apache.commons.collections.map.ListOrderedMap;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDImport;
import org.eclipse.xsd.XSDInclude;
import org.eclipse.xsd.XSDNamedComponent;
import org.eclipse.xsd.XSDPackage;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.util.XSDSchemaBuildingTools;
import org.eclipse.xsd.util.XSDUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;

import org.geotools.util.SoftValueHashMap;
import org.geotools.xml.SchemaIndex;
import org.geotools.xml.Schemas;


/**
 * 
 *
 * @source $URL$
 */
public class SchemaIndexImpl implements SchemaIndex {
    /**
     * The schemas
     */
    XSDSchema[] schemas;

    /**
     * Indexes
     */
    HashMap elementIndex;
    HashMap attributeIndex;
    HashMap attributeGroupIndex;
    HashMap complexTypeIndex;
    HashMap simpleTypeIndex;

    /**
     * Cache of elements to children
     */
    SoftValueHashMap /*<XSDElementDeclaration,OrderedMap>*/ element2children = new SoftValueHashMap(1000);

    /**
     * Cache of elemnets to attributes
     */
    HashMap /*<XSDElementDeclaratoin,List>*/ element2attributes = new HashMap();

    /**
     * Adapter for tracking changes to schemas.
     */
    SchemaAdapter adapter;
    
    public SchemaIndexImpl(XSDSchema[] schemas) {
        this.schemas = new XSDSchema[schemas.length + 1];
        adapter = new SchemaAdapter();
        
        //set the schemas passed in
        for (int i = 0; i < schemas.length; i++) {
            this.schemas[i] = schemas[i];
            synchronized(this.schemas[i].eAdapters()) {
                this.schemas[i].eAdapters().add(adapter);
            }
        }

        //add the schema for xml schema itself
        this.schemas[schemas.length] = schemas[0].getSchemaForSchema();
    }
    
    public void destroy() {
        //remove the adapter from the schemas
        for (int i = 0; i < schemas.length; i++) {
            synchronized(this.schemas[i].eAdapters()) {
                this.schemas[i].eAdapters().remove(adapter);
            }
        }
        schemas = null;
    }

    public XSDSchema[] getSchemas() {
        return schemas;
    }<commit_msg>GEOT-5889: Fix NPE in destroy method (#1756)<commit_after>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library 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;
 *    version 2.1 of the License.
 *
 *    This library 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.
 */
package org.geotools.xml.impl;

import org.apache.commons.collections.OrderedMap;
import org.apache.commons.collections.map.ListOrderedMap;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDImport;
import org.eclipse.xsd.XSDInclude;
import org.eclipse.xsd.XSDNamedComponent;
import org.eclipse.xsd.XSDPackage;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.util.XSDSchemaBuildingTools;
import org.eclipse.xsd.util.XSDUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;

import org.geotools.util.SoftValueHashMap;
import org.geotools.xml.SchemaIndex;
import org.geotools.xml.Schemas;


/**
 * 
 *
 * @source $URL$
 */
public class SchemaIndexImpl implements SchemaIndex {
    /**
     * The schemas
     */
    XSDSchema[] schemas;

    /**
     * Indexes
     */
    HashMap elementIndex;
    HashMap attributeIndex;
    HashMap attributeGroupIndex;
    HashMap complexTypeIndex;
    HashMap simpleTypeIndex;

    /**
     * Cache of elements to children
     */
    SoftValueHashMap /*<XSDElementDeclaration,OrderedMap>*/ element2children = new SoftValueHashMap(1000);

    /**
     * Cache of elemnets to attributes
     */
    HashMap /*<XSDElementDeclaratoin,List>*/ element2attributes = new HashMap();

    /**
     * Adapter for tracking changes to schemas.
     */
    SchemaAdapter adapter;
    
    public SchemaIndexImpl(XSDSchema[] schemas) {
        this.schemas = new XSDSchema[schemas.length + 1];
        adapter = new SchemaAdapter();
        
        //set the schemas passed in
        for (int i = 0; i < schemas.length; i++) {
            this.schemas[i] = schemas[i];
            synchronized(this.schemas[i].eAdapters()) {
                this.schemas[i].eAdapters().add(adapter);
            }
        }

        //add the schema for xml schema itself
        this.schemas[schemas.length] = schemas[0].getSchemaForSchema();
    }
    
    public void destroy() {
        //remove the adapter from the schemas
        if(schemas==null) {
            return;
        }
        for (int i = 0; i < schemas.length; i++) {
            synchronized(this.schemas[i].eAdapters()) {
                this.schemas[i].eAdapters().remove(adapter);
            }
        }
        schemas = null;
    }

    public XSDSchema[] getSchemas() {
        return schemas;
    }
{'fraction_non_alphanumeric': 0.10239865338757188, 'fraction_numerical': 0.0067330621405526725, 'mean_word_length': 2.9876957494407157, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '6082353', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2093, 'n_words': 530}