/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 * Other names may be trademarks of their respective owners.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Contributor(s):
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 * Microsystems, Inc. All Rights Reserved.
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 */

package org.netbeans.modules.dbschema.jdbcimpl;

import java.util.*;

import org.netbeans.modules.dbschema.*;

/** Support class that manages set of objects and fires events
 * about its changes.
 */
public class DBElementsCollection implements DBElementProperties {

    /** Object to fire info about changes to */
    DBElementImpl owner;

    DBElement[] _elms;

    /** Array template for typed returns */
    private Object[] _template;

    //workaround for bug #4396371
    //http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
    protected static transient HashSet instances = new HashSet();
    
	public DBElementsCollection () {
        this(null, null);
    }

    /**
     * @param owner owner of this array at which to fire changes
     */
    DBElementsCollection (DBElementImpl owner, Object[] template) {
        this.owner = owner;
        _template = template;
    }

    public DBElement[] getElements () {
        if (_elms != null)
            return _elms;
        else
            return (DBElement[]) Arrays.asList(_template).toArray(new DBElement[_template.length]);

    }
    
    public void changeElements(DBElement[] elements, int action) {
            changeElements(Arrays.asList(elements), action);
    }

    public void changeElements(List elems, int action) {
        boolean changed = false;
        DBElement[] oldElements = getElements();
        int oldLength = (oldElements == null) ? 0 : oldElements.length;
        int newLength = (elems == null) ? 0 : elems.size();
        List list = null;
            
        switch (action) {
            case DBElement.Impl.ADD:
                if (newLength > 0) {
                    list = ((oldLength == 0) ? new ArrayList() : new ArrayList(Arrays.asList(oldElements)));
                    list.addAll(elems);
                    changed = true;
                }
                break;
            case DBElement.Impl.REMOVE:
                break;
            case DBElement.Impl.SET:
                list = elems;
                changed = true;
                break;
        }
        if (changed)
            _elms = (DBElement[]) list.toArray(_template);//getEmptyArray());

    }

		/** Find method that looks in member elements
		 * @param id the identifier (or null)
		 * @param types array of types to test (or null)
		 * @return the element or null
		 */
        public DBElement find(DBIdentifier id) {
            DBElement[] me = getElements();

            if (me == null)            
                return null;
            
            for (int i = 0; i < me.length; i++)
                if (id.compareTo(me[i].getName(), false))
                    return me[i];

            return null;
        }

	//=============== extra methods needed for xml archiver ==============

	/** Returns the owner of this collection.  This method should only 
	 * be used internally and for cloning and archiving.
	 * @return the owner of this collection
	 */
	DBElementImpl getOwner () {
            return owner;
        }

	/** Set the owner of this collection to the supplied implementation.  
	 * This method should only be used internally and for cloning and 
	 * archiving.
	 * @param owner the owner of this collection
	 */
	void setOwner (DBElementImpl owner) {
            this.owner = owner;
	}

	/** Set the collection of elements maintained by this holder to the 
	 * supplied array.  This method should only be used internally and for 
	 * cloning and archiving.
	 * @param elements the collection of elements maintained by this holder
	 */
	public void setElements (DBElement[] elements)
	{
		_elms = elements;
	}
        
       /** Returns the template for the array of this collection.  This method
         * should only be used internally and for cloning and archiving.
         * @return the typed template of this collection
         */
        public Object[] getTemplate () { return _template; }

        /** Set the template for the array of this collection to the supplied
         * array.  This template is used so the array returned by getElements is
         * properly typed.  This method should only be used internally and
         * for cloning and archiving.
         * @param template the typed template of this collection
         */
        public void setTemplate (Object[] template) { _template = template; }        
}
