KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > jdbcimpl > DBElementsCollection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.jdbcimpl;
21
22 import java.util.*;
23
24 import org.netbeans.modules.dbschema.*;
25
26 /** Support class that manages set of objects and fires events
27  * about its changes.
28  */

29 public class DBElementsCollection implements DBElementProperties {
30
31     /** Object to fire info about changes to */
32     DBElementImpl owner;
33
34     DBElement[] _elms;
35
36     /** Array template for typed returns */
37     private Object JavaDoc[] _template;
38
39     //workaround for bug #4396371
40
//http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
41
protected static transient HashSet instances = new HashSet();
42     
43     public DBElementsCollection () {
44         this(null, null);
45     }
46
47     /**
48      * @param owner owner of this array at which to fire changes
49      */

50     public DBElementsCollection (DBElementImpl owner, Object JavaDoc[] template) {
51         this.owner = owner;
52         _template = template;
53     }
54
55     public DBElement[] getElements () {
56         if (_elms != null)
57             return _elms;
58         else
59             return (DBElement[]) Arrays.asList(_template).toArray(new DBElement[_template.length]);
60
61     }
62     
63     public void changeElements(DBElement[] elements, int action) {
64             changeElements(Arrays.asList(elements), action);
65     }
66
67     public void changeElements(List elems, int action) {
68         boolean changed = false;
69         DBElement[] oldElements = getElements();
70         int oldLength = (oldElements == null) ? 0 : oldElements.length;
71         int newLength = (elems == null) ? 0 : elems.size();
72         List list = null;
73             
74         switch (action) {
75             case DBElement.Impl.ADD:
76                 if (newLength > 0) {
77                     list = ((oldLength == 0) ? new ArrayList() : new ArrayList(Arrays.asList(oldElements)));
78                     list.addAll(elems);
79                     changed = true;
80                 }
81                 break;
82             case DBElement.Impl.REMOVE:
83                 break;
84             case DBElement.Impl.SET:
85                 list = elems;
86                 changed = true;
87                 break;
88         }
89         if (changed)
90             _elms = (DBElement[]) list.toArray(_template);//getEmptyArray());
91

92     }
93
94         /** Find method that looks in member elements
95          * @param id the identifier (or null)
96          * @param types array of types to test (or null)
97          * @return the element or null
98          */

99         public DBElement find(DBIdentifier id) {
100             DBElement[] me = (DBElement[]) getElements();
101
102             if (me == null)
103                 return null;
104             
105             for (int i = 0; i < me.length; i++)
106                 if (id.compareTo(me[i].getName(), false))
107                     return me[i];
108
109             return null;
110         }
111
112     //=============== extra methods needed for xml archiver ==============
113

114     /** Returns the owner of this collection. This method should only
115      * be used internally and for cloning and archiving.
116      * @return the owner of this collection
117      */

118     public DBElementImpl getOwner () {
119             return owner;
120         }
121
122     /** Set the owner of this collection to the supplied implementation.
123      * This method should only be used internally and for cloning and
124      * archiving.
125      * @param owner the owner of this collection
126      */

127     public void setOwner (DBElementImpl owner) {
128             this.owner = owner;
129     }
130
131     /** Set the collection of elements maintained by this holder to the
132      * supplied array. This method should only be used internally and for
133      * cloning and archiving.
134      * @param elements the collection of elements maintained by this holder
135      */

136     public void setElements (DBElement[] elements)
137     {
138         _elms = elements;
139     }
140         
141        /** Returns the template for the array of this collection. This method
142          * should only be used internally and for cloning and archiving.
143          * @return the typed template of this collection
144          */

145         public Object JavaDoc[] getTemplate () { return _template; }
146
147         /** Set the template for the array of this collection to the supplied
148          * array. This template is used so the array returned by getElements is
149          * properly typed. This method should only be used internally and
150          * for cloning and archiving.
151          * @param template the typed template of this collection
152          */

153         public void setTemplate (Object JavaDoc[] template) { _template = template; }
154 }
155
Popular Tags