KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 /**
10  * @author x.hanin
11  *
12  */

13 public class ModuleId {
14     static final String JavaDoc ENCODE_SEPARATOR = ":#@#:";
15     private String JavaDoc _organisation;
16     private String JavaDoc _name;
17     private int _hash;
18
19     public ModuleId(String JavaDoc organisation, String JavaDoc name) {
20         if (name == null) {
21             throw new IllegalArgumentException JavaDoc("null name not allowed");
22         }
23         _organisation = organisation;
24         _name = name;
25         _hash = _hashCode(); //stored for performance reasons, hashCode is very used in many maps
26
}
27
28     public String JavaDoc getName() {
29         return _name;
30     }
31     public String JavaDoc getOrganisation() {
32         return _organisation;
33     }
34     
35     public boolean equals(Object JavaDoc obj) {
36         if (! (obj instanceof ModuleId)) {
37             return false;
38         }
39         ModuleId other = (ModuleId)obj;
40         return other._organisation.equals(_organisation) && other._name.equals(_name);
41     }
42     public int hashCode() {
43         return _hash;
44     }
45     public int _hashCode() {
46         int hash = 31;
47         hash = hash * 13 + _organisation.hashCode();
48         hash = hash * 13 + _name.hashCode();
49         return hash;
50     }
51     public String JavaDoc toString() {
52         return "[ "+_organisation+" | "+_name+" ]";
53     }
54
55     public String JavaDoc encodeToString() {
56         return getOrganisation() + ENCODE_SEPARATOR + getName();
57     }
58     public static ModuleId decode(String JavaDoc encoded) {
59         String JavaDoc[] parts = encoded.split(ENCODE_SEPARATOR);
60         if (parts.length != 2) {
61             throw new IllegalArgumentException JavaDoc("badly encoded module id: '"+encoded+"'");
62         }
63         return new ModuleId(parts[0], parts[1]);
64     }
65 }
66
Popular Tags