View Javadoc
1   package eu.simuline.relana.model;
2   
3   import java.util.List;
4   import java.util.ArrayList;
5   import java.util.Map;
6   import java.util.HashMap;
7   
8   /**
9    * Represents a Package of a model class, 
10   * no matter whether Effect or Component. 
11   *
12   *
13   * Created: Tue Apr 19 13:47:17 2005
14   *
15   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
16   * @version 1.0
17   */
18  public final class Package {
19  
20      /**
21       * The constant for the build-in-package, 
22       * i.e. the package containing the built-in classes. 
23       * Note that it is named <em>_BuiltInPackage</em> 
24       * starting with an underscore 
25       * which distinguishes it from all packages in a library. 
26       */
27      public static final Package BUILD_IN = new Package("_BuiltInPackage", null);
28  
29      static final Package ROOT = new Package("", null);
30  
31  //     static {
32  // 	//PACKAGES = new HashMap();
33  //     } // static 
34  
35      /* -------------------------------------------------------------------- *
36       * attributes.                                                          *
37       * -------------------------------------------------------------------- */
38  
39      /**
40       * The name of this package. 
41       */
42      private final String name;
43  
44      /**
45       * The unique package containing this one. 
46       * This is <code>null</code> 
47       * exactly for {@link #BUILD_IN} and for {@link #ROOT}. 
48       */
49      private final Package superPackage;
50  
51      /**
52       * Maps the names of the subPackages to the subPackages. 
53       */
54      private final Map<String, Package> subPackages;
55  
56      /* -------------------------------------------------------------------- *
57       * constructors.                                                        *
58       * -------------------------------------------------------------------- */
59  
60      private Package(String name, Package superPackage) {
61  	this.name = name;
62  	this.superPackage = superPackage;
63  	this.subPackages = new HashMap<String, Package>();
64      } // Package constructor
65  
66      /* -------------------------------------------------------------------- *
67       * methods.                                                             *
68       * -------------------------------------------------------------------- */
69  
70      /**
71       * Returns the name of this package. 
72       *
73       * @return 
74       *    the name of this package as a <code>String</code>. 
75       */
76      String getName() {
77  	return this.name;
78      }
79  
80      public String getPathName() {
81  	return (getSuperPackage() == null) 
82  	    ? ""
83  	    : getSuperPackage().getPathName() + getName() + ".";
84      }
85  
86      /**
87       * Returns the path of this package. 
88       * This is intended for use within a library hierarchy. 
89       *
90       * @return 
91       *    the path of this package 
92       *    starting with the name of the outermost package. 
93       *    This excludes the names of {@link #BUILD_IN} and for {@link #ROOT}. 
94       *    These are the only packages with an empty path. 
95       *    For all other packages, the path includes the name of this package. 
96       */
97      public List<String> getPath() {
98  	List<String> path = new ArrayList<String>();
99  	addName(path);
100 	return path;
101     }
102 
103     private void addName(List<String> path) {
104 	if (getSuperPackage() != null) {
105 	    getSuperPackage().addName(path);
106 	    path.add(getName());
107 	}
108     }
109 
110     /**
111      * Returns the enclosing package of this package. 
112      *
113      * @return 
114      *    the enclosing package of this package. 
115      *    This is <code>null</code> 
116      *    exactly for {@link #BUILD_IN} and for {@link #ROOT}. 
117      */
118     Package getSuperPackage() {
119 	return this.superPackage;
120     }
121 
122     Package addSubPackage(String name) {
123 	return this.subPackages.put(name, new Package(name, this));
124     }
125 
126     Package subPackage(String name) {
127 	return this.subPackages.get(name);
128     }
129 
130     // 
131     //static Package getPackage(String path) {
132     //}
133 
134     // only sub-packages of ROOT, no BuiltIn's 
135 
136     /**
137      * Returns the <code>Package</code> with the given path. 
138      * Note that this yields only sub-packages of {@link #ROOT} 
139      * no BuiltIn's. 
140      *
141      * @return 
142      *    the <code>Package</code> with the given path. 
143      *    Note that the result is exactly the same 
144      *    with respect to <code>==</code> for equal parameters. 
145      */
146     public static Package getPackage(List<String> path) {
147 	Package knot = ROOT;
148 	Package cand;
149 	//for (int i = 0; i < path.size(); i++) {
150 	for (String elem : path) {
151 	    cand = knot.subPackage(elem);
152 	    if (cand != null) {
153 		knot = cand;
154 		continue;
155 	    }
156 	    cand = knot.addSubPackage(elem);
157 	    assert cand == null;
158 	    knot = knot.subPackage(elem);
159 	    assert knot != null;
160 	}
161 
162 	return knot;
163     }
164 
165     public String toString() {
166 	StringBuffer res = new StringBuffer();
167 	res.append("package ");
168 	res.append(getPath().toString());
169 /*
170 	res.append("<Package name=\"" + this.name);
171 	if (getSuperPackage() != null) {
172 	    res.append("\" superPkg=\"" + getSuperPackage().getName());
173 	}
174 	res.append("\">");
175 	
176 	// **** should be the full name 
177 	for (Package subPkg : this.subPackages.values()) {
178 	    res.append(subPkg.toString());
179 	}
180 	
181 	res.append("</Package>");
182 */
183 	return  res.toString();
184     }
185 } // Package