1 6 package fr.jayasoft.ivy; 7 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import fr.jayasoft.ivy.extendable.UnmodifiableExtendableItem; 13 import fr.jayasoft.ivy.util.IvyPatternHelper; 14 15 16 20 public class ModuleRevisionId extends UnmodifiableExtendableItem { 21 private static final String ENCODE_SEPARATOR = ModuleId.ENCODE_SEPARATOR; 22 private static final String ENCODE_PREFIX = "+"; 23 private static final String NULL_ENCODE = "@#:NULL:#@"; 24 25 public static ModuleRevisionId newInstance(String organisation, String name, String revision) { 26 return new ModuleRevisionId(new ModuleId(organisation, name), revision); 27 } 28 public static ModuleRevisionId newInstance(String organisation, String name, String revision, Map extraAttributes) { 29 return new ModuleRevisionId(new ModuleId(organisation, name), revision, extraAttributes); 30 } 31 public static ModuleRevisionId newInstance(String organisation, String name, String branch, String revision) { 32 return new ModuleRevisionId(new ModuleId(organisation, name), branch, revision); 33 } 34 public static ModuleRevisionId newInstance(String organisation, String name, String branch, String revision, Map extraAttributes) { 35 return new ModuleRevisionId(new ModuleId(organisation, name), branch, revision, extraAttributes); 36 } 37 public static ModuleRevisionId newInstance(ModuleRevisionId mrid, String rev) { 38 return new ModuleRevisionId(mrid.getModuleId(), mrid.getBranch(), rev, mrid.getExtraAttributes()); 39 } 40 41 private ModuleId _moduleId; 42 private String _branch; 43 private String _revision; 44 private int _hash; 45 46 public ModuleRevisionId(ModuleId moduleId, String revision) { 47 this(moduleId, null, revision, null); 48 } 49 public ModuleRevisionId(ModuleId moduleId, String branch, String revision) { 50 this(moduleId, branch, revision, null); 51 } 52 public ModuleRevisionId(ModuleId moduleId, String revision, Map extraAttributes) { 53 this(moduleId, null, revision, extraAttributes); 54 } 55 public ModuleRevisionId(ModuleId moduleId, String branch, String revision, Map extraAttributes) { 56 super(null, extraAttributes); 57 _moduleId = moduleId; 58 _branch = branch == null ? IvyContext.getContext().getIvy().getDefaultBranch(moduleId) : branch; 59 _revision = revision; 60 _hash = _hashCode(); 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 getName() { 71 return getModuleId().getName(); 72 } 73 public String getOrganisation() { 74 return getModuleId().getOrganisation(); 75 } 76 public String getRevision() { 77 return _revision; 78 } 79 80 public boolean equals(Object 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 toString() { 103 return "[ "+_moduleId.getOrganisation()+" | "+_moduleId.getName()+(_branch == null || _branch.length() == 0 ?"":" | "+_branch)+" | "+(_revision == null?"NONE":_revision)+" ]"; 104 } 105 106 public String encodeToString() { 107 StringBuffer buf = new StringBuffer (); 108 Map attributes = getAttributes(); 109 for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) { 110 String attName = (String )iter.next(); 111 String value = (String ) 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 encoded) { 119 String [] parts = encoded.split(ENCODE_SEPARATOR); 120 if (parts.length % 2 != 0) { 121 throw new IllegalArgumentException ("badly encoded module revision id: '"+encoded+"'"); 122 } 123 Map attributes = new HashMap (); 124 for (int i = 0; i < parts.length; i+=2) { 125 String attName = parts[i]; 126 if (!attName.startsWith(ENCODE_PREFIX)) { 127 throw new IllegalArgumentException ("badly encoded module revision id: '"+encoded+"': "+attName+" doesn't start with "+ENCODE_PREFIX); 128 } else { 129 attName = attName.substring(1); 130 } 131 String attValue = parts[i+1]; 132 if (!attValue.startsWith(ENCODE_PREFIX)) { 133 throw new IllegalArgumentException ("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 org = (String )attributes.remove(IvyPatternHelper.ORGANISATION_KEY); 143 String mod = (String )attributes.remove(IvyPatternHelper.MODULE_KEY); 144 String rev = (String )attributes.remove(IvyPatternHelper.REVISION_KEY); 145 String branch = (String )attributes.remove(IvyPatternHelper.BRANCH_KEY); 146 if (org == null) { 147 throw new IllegalArgumentException ("badly encoded module revision id: '"+encoded+"': no organisation"); 148 } 149 if (mod == null) { 150 throw new IllegalArgumentException ("badly encoded module revision id: '"+encoded+"': no module name"); 151 } 152 if (rev == null) { 153 throw new IllegalArgumentException ("badly encoded module revision id: '"+encoded+"': no revision"); 154 } 155 return newInstance(org, mod, branch, rev, attributes); 156 } 157 public String getBranch() { 158 return _branch; 159 } 160 } 161 | Popular Tags |