KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmiimpl > mof > model > ModelElementImpl


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 package org.netbeans.jmiimpl.mof.model;
20
21 import org.netbeans.mdr.handlers.InstanceHandler;
22 import org.netbeans.mdr.storagemodel.StorableObject;
23 import javax.jmi.model.*;
24 import java.util.*;
25
26 /**
27  * Implements MOF operations for ModelElement class
28  *
29  * @author Martin Matula
30  */

31 public abstract class ModelElementImpl extends InstanceHandler implements ModelElement {
32     // correct constant names
33
public static final java.lang.String JavaDoc CONTAINER_DEP = "container"; //NOI18N
34
public static final java.lang.String JavaDoc CONTENTS_DEP = "contents"; //NOI18N
35
public static final java.lang.String JavaDoc SIGNATURE_DEP = "signature"; //NOI18N
36
public static final java.lang.String JavaDoc CONSTRAINT_DEP = "constraint"; //NOI18N
37
public static final java.lang.String JavaDoc CONSTRAINED_ELEMENTS_DEP = "constrained elements"; //NOI18N
38
public static final java.lang.String JavaDoc SPECIALIZATION_DEP = "specialization"; //NOI18N
39
public static final java.lang.String JavaDoc IMPORT_DEP = "import"; //NOI18N
40
public static final java.lang.String JavaDoc TYPE_DEFINITION_DEP = "type definition"; //NOI18N
41
public static final java.lang.String JavaDoc REFERENCED_ENDS_DEP = "referenced ends"; //NOI18N
42
public static final java.lang.String JavaDoc TAGGED_ELEMENTS_DEP = "tagged elements"; //NOI18N
43
public static final java.lang.String JavaDoc INDIRECT_DEP = "indirect"; //NOI18N
44
public static final java.lang.String JavaDoc ALL_DEP = "all"; //NOI18N
45

46     static final HashSet ALL_KINDS = new HashSet();
47     
48     static {
49         ALL_KINDS.add(CONTAINER_DEP);
50         ALL_KINDS.add(CONTENTS_DEP);
51         ALL_KINDS.add(SIGNATURE_DEP);
52         ALL_KINDS.add(CONSTRAINT_DEP);
53         ALL_KINDS.add(CONSTRAINED_ELEMENTS_DEP);
54         ALL_KINDS.add(SPECIALIZATION_DEP);
55         ALL_KINDS.add(IMPORT_DEP);
56         ALL_KINDS.add(TYPE_DEFINITION_DEP);
57         ALL_KINDS.add(REFERENCED_ENDS_DEP);
58         ALL_KINDS.add(TAGGED_ELEMENTS_DEP);
59     }
60     
61     protected ModelElementImpl(StorableObject storable) {
62         super(storable);
63     }
64
65     private void recursiveFindDeps(Set kinds, Set result) {
66         Set temp = new HashSet();
67         for (Iterator m = kinds.iterator(); m.hasNext();) {
68             findDepsOfKind((String JavaDoc) m.next(), temp);
69         }
70         for (Iterator it = temp.iterator(); it.hasNext();) {
71             ModelElementImpl me = (ModelElementImpl) it.next();
72             if (result.add(me)) {
73                 me.recursiveFindDeps(kinds, result);
74             }
75         }
76     }
77
78     private void findDepsOfKind(String JavaDoc kind, Set result) {
79         if (kind.equals(CONSTRAINT_DEP)) { // constraint
80
result.addAll(this.getConstraints());
81         } else if (kind.equals(CONTAINER_DEP)) { //container
82
result.add(this.getContainer());
83         } else if (kind.equals(CONSTRAINED_ELEMENTS_DEP) && (this instanceof Constraint)) { //constrained elements
84
result.addAll(((Constraint) this).getConstrainedElements());
85         } else if (kind.equals(SPECIALIZATION_DEP) && (this instanceof GeneralizableElement)) { //specialization
86
result.addAll(((GeneralizableElement) this).getSupertypes());
87         } else if (kind.equals(IMPORT_DEP) && (this instanceof Import)) { //import
88
result.add(((Import) this).getImportedNamespace());
89         } else if (kind.equals(CONTENTS_DEP) && (this instanceof Namespace)) { //contents
90
result.addAll(((Namespace) this).getContents());
91         } else if (kind.equals(SIGNATURE_DEP) && (this instanceof Operation)) { //signature
92
result.addAll(((Operation) this).getExceptions());
93         } else if (kind.equals(TAGGED_ELEMENTS_DEP) && (this instanceof Tag)) { //tagged elements
94
result.addAll(((Tag) this).getElements());
95         } else if (kind.equals(TYPE_DEFINITION_DEP) && (this instanceof TypedElement)) { //type definition
96
result.add(((TypedElement) this).getType());
97         } else if (kind.equals(REFERENCED_ENDS_DEP) && (this instanceof Reference)) { //referenced ends
98
result.add(((Reference) this).getReferencedEnd());
99             result.add(((Reference) this).getExposedEnd());
100         }
101
102         result.remove(null);
103     }
104
105     private boolean isDepOfKind(String JavaDoc kind, ModelElement otherElement) {
106         HashSet result = new HashSet();
107         findDepsOfKind(kind, result);
108         return result.contains(otherElement);
109     }
110
111     // --- operations
112

113     public boolean isFrozen() {
114         // [PENDING] has to be implemented to return true if there already exists an instance of this element
115
return false;
116     }
117
118     public Collection findRequiredElements(Collection kinds, boolean recursive) {
119         _lock(false);
120         try {
121             if (kinds.contains(ALL_DEP)) {
122                 return findRequiredElements(ALL_KINDS, recursive);
123             } else {
124                 Set newKinds = (kinds == ALL_KINDS ? ALL_KINDS : new HashSet(kinds));
125                 Set result = new HashSet();
126                 if (recursive) {
127                     recursiveFindDeps(newKinds, result);
128                 } else {
129                     String JavaDoc kind;
130                     for (Iterator it = kinds.iterator(); it.hasNext();) {
131                         kind = (String JavaDoc) it.next();
132                         findDepsOfKind(kind, result);
133                     }
134                 }
135                 return result;
136             }
137         } finally {
138             _unlock();
139         }
140     }
141     
142     public boolean isVisible(ModelElement otherElement) {
143         return true;
144     }
145
146     public boolean isRequiredBecause(ModelElement otherElement, String JavaDoc[] reason) {
147         _lock(false);
148         try {
149             for (Iterator it = ALL_KINDS.iterator(); it.hasNext();) {
150                 reason[0] = (String JavaDoc) it.next();
151                 if (isDepOfKind(reason[0], otherElement)) {
152                     return true;
153                 }
154             }
155
156             if (findRequiredElements(ALL_KINDS, true).size() > 0) {
157                 reason[0] = INDIRECT_DEP;
158             } else {
159                 reason[0] = "";
160                 return false;
161             }
162
163             return true;
164         } finally {
165             _unlock();
166         }
167     }
168
169     // --- derived attributes
170

171     public List getQualifiedName() {
172         List result;
173         Namespace container = getContainer();
174
175         if (container == null) {
176             result = new ArrayList();
177         } else {
178             result = new ArrayList(container.getQualifiedName());
179         }
180         result.add(getName());
181         return result;
182     }
183 }
184
Popular Tags