1 package eu.simuline.relana.model;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7
8 /**
9 * Locates a class within a library.
10 *
11 *
12 * Created: Thu Apr 14 23:42:03 2005
13 *
14 * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
15 * @version 1.0
16 */
17 public final class InstanceLocator {
18
19 /* -------------------------------------------------------------------- *
20 * attributes. *
21 * -------------------------------------------------------------------- */
22
23 /**
24 * The name of the class to be located.
25 * Declared final to ensure that {@link #hashCode()} remains unchanged.
26 * In addition this list is unmodifyable.
27 */
28 private final List<String> path;
29
30 /* -------------------------------------------------------------------- *
31 * constructors. *
32 * -------------------------------------------------------------------- */
33
34 public InstanceLocator(List<String> path) {
35 if (path.isEmpty()) {
36 throw new IllegalArgumentException
37 ("Found empty location: []. ");
38 }
39 for (String name : path) {
40 if (name.length() == 0) {
41 throw new IllegalArgumentException
42 ("Found empty name in locator: " + path + ". ");
43 }
44 }
45
46 this.path = Collections.unmodifiableList(path);
47 } // InstanceLocator constructor
48
49 /* -------------------------------------------------------------------- *
50 * methods. *
51 * -------------------------------------------------------------------- */
52
53 public static InstanceLocator getLocator(List<String> path) {
54 return new InstanceLocator(path);
55 }
56
57 public static InstanceLocator getLocator(String path) {
58 return getLocator
59 (new ArrayList<String>(Arrays.asList(path.split("[.]", -1))));
60 }
61
62 /**
63 * Returns the wrapped path as an unmodifiable list.
64 */
65 public List<String> getPath() {
66 return this.path;
67 }
68
69 public boolean equals(Object other) {
70 if (!(other instanceof InstanceLocator)) {
71 return false;
72 }
73 return this.path.equals(((InstanceLocator) other).getPath());
74 }
75
76 // immutable to allow InstanceLocators as elements of hashsets.
77 public int hashCode() {
78 return getPath().hashCode();
79 }
80
81 public String toString() {
82 return "<InstanceLocator path=\"" + this.path + "\"/>";
83 }
84
85 } // InstanceLocator