View Javadoc
1   package eu.simuline.relana.model;
2   
3   import java.util.List;
4   import java.util.ArrayList;
5   import java.util.Arrays;
6   
7   /**
8    * Locates a class within a library. 
9    *
10   *
11   * Created: Thu Apr 14 23:42:03 2005
12   *
13   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
14   * @version 1.0
15   */
16  public final class ClassLocator {
17  
18      /* -------------------------------------------------------------------- *
19       * attributes.                                                          *
20       * -------------------------------------------------------------------- */
21  
22      /**
23       * The name of the class to be located. 
24       */
25      // must be final to guarantee a valid {@link #hashCode()}. 
26      private final String name;
27  
28      /**
29       * The package of the class to be located. 
30       */
31      private Package pkg;
32  
33      /* -------------------------------------------------------------------- *
34       * constructors.                                                        *
35       * -------------------------------------------------------------------- */
36  
37      public ClassLocator(String name, Package pkg) {
38  	this.name = name;
39  	this.pkg = pkg;
40      } // ClassLocator constructor
41  
42      public ClassLocator(ClassLocator other) {
43  	this(other.name, other.pkg);
44      } // ClassLocator constructor
45  
46      /* -------------------------------------------------------------------- *
47       * methods.                                                             *
48       * -------------------------------------------------------------------- */
49  
50      public static ClassLocator getLocator(List<String> path) {
51  	int idxLast = path.size() - 1;
52  	String name = path.get(idxLast); // ****
53  	return new ClassLocator(name,
54  				Package.getPackage(path.subList(0, idxLast)));
55      }
56  
57      public static ClassLocator getLocator(String path) {
58  	return getLocator
59  	    (new ArrayList<String>(Arrays.asList(path.split("[.]", -1))));
60      }
61  
62      public String getName() {
63  	return this.name;
64      }
65  
66      public Package getPackage() {
67  	return this.pkg;
68      }
69  
70      public List<String> getPath() {
71  	List<String> path = getPackage().getPath();
72  	path.add(getName());
73  	return path;
74      }
75  
76      public boolean equals(Object other) {
77  	if (!(other instanceof ClassLocator)) {
78  	    return false;
79  	}
80  	return getName().equals(((ClassLocator) other).getName());
81      }
82  
83      public int hashCode() {
84  	return getName().hashCode();
85      }
86  
87      public String toString() {
88  	return "<ClassLocator name=\"" + this.name + 
89  	    "\" package=\"" + this.pkg + "\"/>";
90      }
91  
92  } // ClassLocator