KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > Configuration


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy;
7
8 import java.util.LinkedHashSet JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import fr.jayasoft.ivy.extendable.DefaultExtendableItem;
12
13
14 /**
15  * Represents a module configuration
16  */

17 public class Configuration extends DefaultExtendableItem {
18     public static class Visibility {
19         public static Visibility PUBLIC = new Visibility("public");
20         public static Visibility PRIVATE = new Visibility("private");
21         public static Visibility getVisibility(String JavaDoc name) {
22             if ("private".equals(name)) {
23                 return PRIVATE;
24             } else if ("public".equals(name)) {
25                 return PUBLIC;
26             } else {
27                 throw new IllegalArgumentException JavaDoc("unknwon visibility "+name);
28             }
29         }
30         private String JavaDoc _name;
31         private Visibility(String JavaDoc name) {
32             _name = name;
33         }
34         public String JavaDoc toString() {
35             return _name;
36         }
37     }
38     
39     private String JavaDoc _name;
40     private String JavaDoc _description;
41     private String JavaDoc[] _extends;
42     private Visibility _visibility;
43     private boolean _transitive = true;
44     
45     /**
46      * @param name
47      * @param visibility
48      * @param description
49      * @param ext
50      */

51     public Configuration(String JavaDoc name, Visibility visibility,
52             String JavaDoc description, String JavaDoc[] ext) {
53         this(name, visibility, description, ext, true);
54     }
55     
56     /**
57      * @param name
58      * @param visibility
59      * @param description
60      * @param ext
61      * @param transitive
62      */

63     public Configuration(String JavaDoc name, Visibility visibility,
64             String JavaDoc description, String JavaDoc[] ext, boolean transitive) {
65         if (name == null) {
66             throw new NullPointerException JavaDoc("null configuration name not allowed");
67         }
68         if (visibility == null) {
69             throw new NullPointerException JavaDoc("null visibility not allowed");
70         }
71         _name = name;
72         _visibility = visibility;
73         _description = description;
74         if (ext == null) {
75             _extends = new String JavaDoc[0];
76         } else {
77             _extends = new String JavaDoc[ext.length];
78             for (int i = 0; i < ext.length; i++) {
79                 _extends[i] = ext[i].trim();
80             }
81         }
82         _transitive=transitive;
83     }
84     
85     /**
86      * @param name
87      */

88     public Configuration(String JavaDoc name) {
89         this(name, Visibility.PUBLIC, null, null);
90     }
91     
92     /**
93      * @return Returns the description. It may be null.
94      */

95     public String JavaDoc getDescription() {
96         return _description;
97     }
98     /**
99      * @return Returns the extends. May be empty, but never null.
100      */

101     public String JavaDoc[] getExtends() {
102         return _extends;
103     }
104     /**
105      * @return Returns the name. Never null;
106      */

107     public String JavaDoc getName() {
108         return _name;
109     }
110     /**
111      * @return Returns the visibility. Never null.
112      */

113     public Visibility getVisibility() {
114         return _visibility;
115     }
116     
117     /**
118      * @return Returns the transitive.
119      */

120     public final boolean isTransitive() {
121         return _transitive;
122     }
123    
124     public String JavaDoc toString() {
125         return _name;
126     }
127     
128     public boolean equals(Object JavaDoc obj) {
129         if (! (obj instanceof Configuration)) {
130             return false;
131         }
132         return ((Configuration)obj).getName().equals(getName());
133     }
134     
135     public int hashCode() {
136         return getName().hashCode();
137     }
138     
139     public void replaceWildcards(ModuleDescriptor md) {
140         if (this != md.getConfiguration(_name)) {
141             throw new IllegalArgumentException JavaDoc(
142             "The given ModuleDescriptor doesn't own this configuration!");
143         }
144         
145         Configuration[] configs = md.getConfigurations();
146         
147         Set JavaDoc newExtends = new LinkedHashSet JavaDoc();
148         for (int j = 0; j < _extends.length; j++) {
149             if ("*".equals(_extends[j])) {
150                 addOther(configs, null, newExtends);
151             } else if ("*(public)".equals(_extends[j])) {
152                 addOther(configs, Visibility.PUBLIC, newExtends);
153             } else if ("*(private)".equals(_extends[j])) {
154                 addOther(configs, Visibility.PRIVATE, newExtends);
155             } else {
156                 newExtends.add(_extends[j]);
157             }
158         }
159         
160         this._extends = (String JavaDoc[]) newExtends.toArray(new String JavaDoc[newExtends.size()]);
161     }
162     
163     private void addOther(Configuration[] allConfigs, Visibility visibility, Set JavaDoc configs) {
164         for (int i = 0; i < allConfigs.length; i++) {
165             String JavaDoc currentName = allConfigs[i].getName();
166             if (!_name.equals(currentName)
167                     && ((visibility == null) || visibility.equals(allConfigs[i].getVisibility()))) {
168                 configs.add(currentName);
169             }
170         }
171     }
172     
173 }
174
Popular Tags