KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import fr.jayasoft.ivy.extendable.UnmodifiableExtendableItem;
13 import fr.jayasoft.ivy.util.IvyPatternHelper;
14
15
16 /**
17  * @author x.hanin
18  *
19  */

20 public class ModuleRevisionId extends UnmodifiableExtendableItem {
21     private static final String JavaDoc ENCODE_SEPARATOR = ModuleId.ENCODE_SEPARATOR;
22     private static final String JavaDoc ENCODE_PREFIX = "+";
23     private static final String JavaDoc NULL_ENCODE = "@#:NULL:#@";
24     
25     public static ModuleRevisionId newInstance(String JavaDoc organisation, String JavaDoc name, String JavaDoc revision) {
26         return new ModuleRevisionId(new ModuleId(organisation, name), revision);
27     }
28     public static ModuleRevisionId newInstance(String JavaDoc organisation, String JavaDoc name, String JavaDoc revision, Map JavaDoc extraAttributes) {
29         return new ModuleRevisionId(new ModuleId(organisation, name), revision, extraAttributes);
30     }
31     public static ModuleRevisionId newInstance(String JavaDoc organisation, String JavaDoc name, String JavaDoc branch, String JavaDoc revision) {
32         return new ModuleRevisionId(new ModuleId(organisation, name), branch, revision);
33     }
34     public static ModuleRevisionId newInstance(String JavaDoc organisation, String JavaDoc name, String JavaDoc branch, String JavaDoc revision, Map JavaDoc extraAttributes) {
35         return new ModuleRevisionId(new ModuleId(organisation, name), branch, revision, extraAttributes);
36     }
37     public static ModuleRevisionId newInstance(ModuleRevisionId mrid, String JavaDoc rev) {
38         return new ModuleRevisionId(mrid.getModuleId(), mrid.getBranch(), rev, mrid.getExtraAttributes());
39     }
40     
41     private ModuleId _moduleId;
42     private String JavaDoc _branch;
43     private String JavaDoc _revision;
44     private int _hash;
45     
46     public ModuleRevisionId(ModuleId moduleId, String JavaDoc revision) {
47         this(moduleId, null, revision, null);
48     }
49     public ModuleRevisionId(ModuleId moduleId, String JavaDoc branch, String JavaDoc revision) {
50         this(moduleId, branch, revision, null);
51     }
52     public ModuleRevisionId(ModuleId moduleId, String JavaDoc revision, Map JavaDoc extraAttributes) {
53         this(moduleId, null, revision, extraAttributes);
54     }
55     public ModuleRevisionId(ModuleId moduleId, String JavaDoc branch, String JavaDoc revision, Map JavaDoc extraAttributes) {
56         super(null, extraAttributes);
57         _moduleId = moduleId;
58         _branch = branch == null ? IvyContext.getContext().getIvy().getDefaultBranch(moduleId) : branch;
59         _revision = revision;
60         _hash = _hashCode(); //stored for performance reasons, hashCode is very used in many maps
61
setStandardAttribute(IvyPatternHelper.ORGANISATION_KEY, _moduleId.getOrganisation());
62         setStandardAttribute(IvyPatternHelper.MODULE_KEY, _moduleId.getName());
63         setStandardAttribute(IvyPatternHelper.BRANCH_KEY, _branch);
64         setStandardAttribute(IvyPatternHelper.REVISION_KEY, _revision);
65     }
66     
67     public ModuleId getModuleId() {
68         return _moduleId;
69     }
70     public String JavaDoc getName() {
71         return getModuleId().getName();
72     }
73     public String JavaDoc getOrganisation() {
74         return getModuleId().getOrganisation();
75     }
76     public String JavaDoc getRevision() {
77         return _revision;
78     }
79     
80     public boolean equals(Object JavaDoc obj) {
81         if (! (obj instanceof ModuleRevisionId)) {
82             return false;
83         }
84         ModuleRevisionId other = (ModuleRevisionId)obj;
85         return (other.getRevision() == null ? getRevision() == null : other.getRevision().equals(getRevision()))
86             && (other.getBranch() == null ? getBranch() == null : other.getBranch().equals(getBranch()))
87             && other.getModuleId().equals(getModuleId())
88             && other.getExtraAttributes().equals(getExtraAttributes());
89     }
90     public int hashCode() {
91         return _hash;
92     }
93     public int _hashCode() {
94         int hash = 31;
95         hash = hash * 13 + (getBranch() == null ? 0 : getBranch().hashCode());
96         hash = hash * 13 + (getRevision() == null ? 0 : getRevision().hashCode());
97         hash = hash * 13 + getModuleId().hashCode();
98         hash = hash * 13 + getAttributes().hashCode();
99         return hash;
100     }
101     
102     public String JavaDoc toString() {
103         return "[ "+_moduleId.getOrganisation()+" | "+_moduleId.getName()+(_branch == null || _branch.length() == 0 ?"":" | "+_branch)+" | "+(_revision == null?"NONE":_revision)+" ]";
104     }
105     
106     public String JavaDoc encodeToString() {
107         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
108         Map JavaDoc attributes = getAttributes();
109         for (Iterator JavaDoc iter = attributes.keySet().iterator(); iter.hasNext();) {
110             String JavaDoc attName = (String JavaDoc)iter.next();
111             String JavaDoc value = (String JavaDoc) attributes.get(attName);
112             value = value == null ? NULL_ENCODE : value;
113             buf.append(ENCODE_PREFIX).append(attName).append(ENCODE_SEPARATOR).append(ENCODE_PREFIX).append(value).append(ENCODE_SEPARATOR);
114         }
115         return buf.toString();
116     }
117     
118     public static ModuleRevisionId decode(String JavaDoc encoded) {
119         String JavaDoc[] parts = encoded.split(ENCODE_SEPARATOR);
120         if (parts.length % 2 != 0) {
121             throw new IllegalArgumentException JavaDoc("badly encoded module revision id: '"+encoded+"'");
122         }
123         Map JavaDoc attributes = new HashMap JavaDoc();
124         for (int i = 0; i < parts.length; i+=2) {
125             String JavaDoc attName = parts[i];
126             if (!attName.startsWith(ENCODE_PREFIX)) {
127                 throw new IllegalArgumentException JavaDoc("badly encoded module revision id: '"+encoded+"': "+attName+" doesn't start with "+ENCODE_PREFIX);
128             } else {
129                 attName = attName.substring(1);
130             }
131             String JavaDoc attValue = parts[i+1];
132             if (!attValue.startsWith(ENCODE_PREFIX)) {
133                 throw new IllegalArgumentException JavaDoc("badly encoded module revision id: '"+encoded+"': "+attValue+" doesn't start with "+ENCODE_PREFIX);
134             } else {
135                 attValue = attValue.substring(1);
136             }
137             if (NULL_ENCODE.equals(attValue)) {
138                 attValue = null;
139             }
140             attributes.put(attName, attValue);
141         }
142         String JavaDoc org = (String JavaDoc)attributes.remove(IvyPatternHelper.ORGANISATION_KEY);
143         String JavaDoc mod = (String JavaDoc)attributes.remove(IvyPatternHelper.MODULE_KEY);
144         String JavaDoc rev = (String JavaDoc)attributes.remove(IvyPatternHelper.REVISION_KEY);
145         String JavaDoc branch = (String JavaDoc)attributes.remove(IvyPatternHelper.BRANCH_KEY);
146         if (org == null) {
147             throw new IllegalArgumentException JavaDoc("badly encoded module revision id: '"+encoded+"': no organisation");
148         }
149         if (mod == null) {
150             throw new IllegalArgumentException JavaDoc("badly encoded module revision id: '"+encoded+"': no module name");
151         }
152         if (rev == null) {
153             throw new IllegalArgumentException JavaDoc("badly encoded module revision id: '"+encoded+"': no revision");
154         }
155         return newInstance(org, mod, branch, rev, attributes);
156     }
157     public String JavaDoc getBranch() {
158         return _branch;
159     }
160 }
161
Popular Tags