KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > ModuleDependency


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui.customizer;
21
22 import java.text.Collator JavaDoc;
23 import java.util.Comparator JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
28 import org.netbeans.spi.project.support.ant.PropertyUtils;
29 import org.openide.util.Utilities;
30
31 /**
32  * Represents one module dependency. I.e. <em>&lt;dependency&gt;</em> element
33  * in module's <em>project.xml</em> or one token in the
34  * OpenIDE-Module-Module-Dependencies attribute of module manifest.
35  * <p>
36  * Natural ordering is based sequentially on the code name base of {@link
37  * ModuleEntry} this instance represents, release version, specification
38  * version, implementation dependency and compilation dependency. Two instances
39  * are equals only if all the mentioned are <code>equals</code>.
40  * </p>
41  *
42  * @author Martin Krauskopf
43  */

44 public final class ModuleDependency implements Comparable JavaDoc<ModuleDependency> {
45     
46     // XXX refactor and use SpecificationVersion instead
47
private String JavaDoc releaseVersion;
48     private String JavaDoc specVersion;
49     /**
50      * Defer loading spec version until really needed since it can be expensive.
51      */

52     private static final String JavaDoc SPEC_VERSION_LAZY = "<lazy>"; // NOI18N
53
private boolean implDep;
54     private boolean compileDep;
55     
56     private ModuleEntry me;
57     
58     private Set JavaDoc<String JavaDoc> filterTokensNotFriend;
59     private Set JavaDoc<String JavaDoc> filterTokensFriend;
60     
61     public static final Comparator JavaDoc<ModuleDependency> LOCALIZED_NAME_COMPARATOR;
62     public static final Comparator JavaDoc<ModuleDependency> CNB_COMPARATOR;
63     
64     static {
65         LOCALIZED_NAME_COMPARATOR = new Comparator JavaDoc<ModuleDependency>() {
66             public int compare(ModuleDependency d1, ModuleDependency d2) {
67                 ModuleEntry me1 = d1.getModuleEntry();
68                 ModuleEntry me2 = d2.getModuleEntry();
69                 int result = Collator.getInstance().compare(
70                         me1.getLocalizedName(), me2.getLocalizedName());
71                 return result != 0 ? result :
72                     me1.getCodeNameBase().compareTo(me2.getCodeNameBase());
73             }
74         };
75         CNB_COMPARATOR = new Comparator JavaDoc<ModuleDependency>() {
76             public int compare(ModuleDependency d1, ModuleDependency d2) {
77                 return d1.getCodeNameBase().compareTo(d2.getCodeNameBase());
78             }
79         };
80     }
81     
82     /**
83      * Creates a new instance based on the given entry. The instance will be
84      * initialized with given entry's release and specification versions.
85      * Compile dependency is set to true by default, implementation version to
86      * false.
87      */

88     public ModuleDependency(ModuleEntry me) {
89         this(me, me.getReleaseVersion(), SPEC_VERSION_LAZY, me.getPublicPackages().length > 0, false);
90     }
91     
92     public ModuleDependency(ModuleEntry me, String JavaDoc releaseVersion,
93             String JavaDoc specVersion, boolean compileDep, boolean implDep) {
94         this.me = me;
95         
96         // set versions to null if contain the same value as the given entry
97
this.compileDep = compileDep;
98         this.implDep = implDep;
99         this.releaseVersion = releaseVersion;
100         this.specVersion = specVersion;
101     }
102     
103     /**
104      * Get the <b>major release version</b>.
105      * @return <code>null</code> for none or the version.
106      */

107     public String JavaDoc getReleaseVersion() {
108         return releaseVersion;
109     }
110     
111     public String JavaDoc getSpecificationVersion() {
112         if (specVersion == SPEC_VERSION_LAZY) {
113             specVersion = me.getSpecificationVersion();
114         }
115         return specVersion;
116     }
117     
118     public ModuleEntry getModuleEntry() {
119         return me;
120     }
121     
122     private String JavaDoc getCodeNameBase() {
123         return getModuleEntry().getCodeNameBase();
124     }
125     
126     public int compareTo(ModuleDependency other) {
127         int result = getCodeNameBase().compareTo(other.getCodeNameBase());
128         if (result != 0) { return result; }
129         
130         // XXX this is not exact since we should use SpecificationVersion
131
// instead of String. In this way are not using Dewey-decimal comparison.
132
String JavaDoc relVersion = other.getReleaseVersion();
133         result = getReleaseVersion() == null // release versions may be null
134
? (relVersion == null ? 0 : -1)
135                 : (relVersion == null ? 1 : getReleaseVersion().compareTo(relVersion));
136         if (result != 0) { return result; }
137         
138         // do not force resolution of SPEC_VERSION_LAZY for comparisons, unnecessary
139
if (specVersion != SPEC_VERSION_LAZY || other.specVersion != SPEC_VERSION_LAZY) {
140             String JavaDoc otherSpec = other.getSpecificationVersion();
141             String JavaDoc spec = getSpecificationVersion();
142             result = spec == null // spec versions may be null
143
? (otherSpec == null ? 0 : -1)
144                     : (otherSpec == null ? 1 : spec.compareTo(otherSpec));
145             if (result != 0) { return result; }
146         }
147         
148         result = hasImplementationDepedendency() == other.hasImplementationDepedendency() ? 0 : (implDep ? 1 : -1);
149         if (result != 0) { return result; }
150         
151         result = hasCompileDependency() == other.hasCompileDependency() ? 0 : (compileDep ? 1 : -1);
152         return result;
153     }
154     
155     public boolean equals(Object JavaDoc o) {
156         if (o instanceof ModuleDependency) {
157             ModuleDependency other = (ModuleDependency) o;
158             return getCodeNameBase().equals(other.getCodeNameBase()) &&
159                     Utilities.compareObjects(getReleaseVersion(), other.getReleaseVersion()) &&
160                     ((specVersion == SPEC_VERSION_LAZY && other.specVersion == SPEC_VERSION_LAZY) ||
161                     Utilities.compareObjects(getSpecificationVersion(), other.getSpecificationVersion())) &&
162                     (hasImplementationDepedendency() == other.hasImplementationDepedendency()) &&
163                     (hasCompileDependency() == other.hasCompileDependency());
164         } else {
165             return false;
166         }
167     }
168     
169     public int hashCode() {
170         // specifically do not hash spec version
171
return getCodeNameBase().hashCode();
172     }
173     
174     public boolean hasCompileDependency() {
175         return compileDep;
176     }
177     
178     public boolean hasImplementationDepedendency() {
179         return implDep;
180     }
181     
182     /**
183      * Return a set of tokens that can be used to search for this dependency.
184      * Per UI spec, includes lower-case versions of:
185      * <ol>
186      * <li>the code name base
187      * <li>the localized display name
188      * <li> the full path to the module JAR or any Class-Path extension
189      * <li> the fully-qualified class name (use . for inner classes) of any class
190      * contained in the module JAR or any Class-Path extension which is in an package
191      * which would be made available to the depending module when using a specification version dependency
192      * </ol>
193      * Note that the last item means that this can behave differently according to the depending
194      * module (according to whether or not it would be listed as a friend).
195      * @param dependingModuleCNB the CNB of the module depending on this one
196      */

197     Set JavaDoc<String JavaDoc> getFilterTokens(String JavaDoc dependingModuleCNB) {
198         boolean friend = me.isDeclaredAsFriend(dependingModuleCNB);
199         Set JavaDoc<String JavaDoc> filterTokens = friend ? filterTokensFriend : filterTokensNotFriend;
200         if (filterTokens == null) {
201             filterTokens = new HashSet JavaDoc<String JavaDoc>();
202             filterTokens.add(me.getCodeNameBase());
203             filterTokens.add(me.getLocalizedName());
204             filterTokens.add(me.getJarLocation().getAbsolutePath());
205             String JavaDoc[] cpext = PropertyUtils.tokenizePath(me.getClassPathExtensions());
206             for (int i = 0; i < cpext.length; i++) {
207                 filterTokens.add(cpext[i]);
208             }
209             if (friend) {
210                 Iterator JavaDoc it = me.getPublicClassNames().iterator();
211                 while (it.hasNext()) {
212                     String JavaDoc clazz = (String JavaDoc) it.next();
213                     filterTokens.add(clazz.replace('$', '.'));
214                 }
215             }
216             if (friend) {
217                 filterTokensFriend = filterTokens;
218             } else {
219                 filterTokensNotFriend = filterTokens;
220             }
221         }
222         return filterTokens;
223     }
224     
225     public String JavaDoc toString() {
226         return "ModuleDependency[me: " + getModuleEntry() + // NOI18N
227
", relVer: " + getReleaseVersion() + // NOI18N
228
", specVer: " + getSpecificationVersion() + // NOI18N
229
", implDep: " + hasImplementationDepedendency() + // NOI18N
230
", compDep: " + hasCompileDependency() + // NOI18N
231
"]"; // NOI18N
232
}
233 }
234
Popular Tags