1 31 32 package org.opencms.module; 33 34 import org.opencms.main.CmsIllegalArgumentException; 35 import org.opencms.main.CmsRuntimeException; 36 import org.opencms.util.CmsStringUtil; 37 38 61 public class CmsModuleVersion implements Comparable { 62 63 64 public static final String DEFAULT_VERSION = "0.1"; 65 66 67 private int m_dots; 68 69 70 private long m_number; 71 72 73 private boolean m_updated; 74 75 76 private String m_version; 77 78 83 public CmsModuleVersion(String version) { 84 85 setVersion(version); 86 } 87 88 91 public int compareTo(Object obj) { 92 93 if (obj == this) { 94 return 0; 95 } 96 if (obj instanceof CmsModuleVersion) { 97 if (m_version == null) { 98 return 0; 99 } 100 CmsModuleVersion other = (CmsModuleVersion)obj; 101 if (m_number == other.m_number) { 102 return 0; 103 } 104 return (m_number > other.m_number) ? 1 : -1; 105 } 106 return 0; 107 } 108 109 112 public boolean equals(Object obj) { 113 114 if (obj == this) { 115 return true; 116 } 117 if (obj instanceof CmsModuleVersion) { 118 CmsModuleVersion other = (CmsModuleVersion)obj; 119 if (m_version == null) { 120 return (other.m_version == null); 121 } 122 return m_version.equals(other.m_version); 123 } 124 return false; 125 } 126 127 132 public String getVersion() { 133 134 return m_version; 135 } 136 137 140 public int hashCode() { 141 142 if (m_version == null) { 143 return 0; 144 } 145 146 return m_version.hashCode(); 147 } 148 149 154 public void setVersion(String version) { 155 156 m_number = 0L; 157 if ((version == null) || (version.charAt(0) == '.') || (version.charAt(version.length() - 1) == '.')) { 158 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0)); 159 } 160 String [] split = CmsStringUtil.splitAsArray(version, '.'); 161 m_dots = split.length; 162 if (m_dots > 4) { 163 throw new CmsIllegalArgumentException(Messages.get().container( 164 Messages.ERR_INVALID_VERSION_LENGTH_1, 165 version)); 166 } 167 String [] numbers = new String [5]; 168 System.arraycopy(split, 0, numbers, 1, m_dots); 169 numbers[0] = "1"; 170 for (int i = 1 + m_dots; i < 5; i++) { 171 numbers[i] = "0"; 172 } 173 for (int i = numbers.length - 1; i >= 0; i--) { 174 try { 175 int number = Integer.valueOf(numbers[numbers.length - i - 1]).intValue(); 176 177 if ((number > 999) || (number < 0)) { 178 throw new CmsIllegalArgumentException(Messages.get().container( 179 Messages.ERR_INVALID_VERSION_SUBNUMBER_1, 180 new Integer (number))); 181 } 182 m_number = (long)Math.pow(1000.0, i) * number + m_number; 183 } catch (NumberFormatException e) { 184 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0)); 186 } 187 } 188 189 setVersion(m_number); 190 } 191 192 195 public String toString() { 196 197 return getVersion(); 198 } 199 200 203 protected void increment() { 204 205 if (m_number < 1999999999999L) { 206 m_number += (long)Math.pow(1000.0, (4 - m_dots)); 207 setVersion(m_number); 208 } else { 209 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_MODULE_VERSION_NUMBER_0)); 210 } 211 } 212 213 218 protected boolean isUpdated() { 219 220 return m_updated; 221 } 222 223 228 protected void setUpdated(boolean updated) { 229 230 m_updated = updated; 231 } 232 233 238 private void setVersion(long number) { 239 240 String result = ""; 241 for (int i = 0; i < 4; i++) { 242 long mod = number % 1000L; 243 number = number / 1000L; 244 if (m_dots >= (4 - i)) { 245 if (m_dots > (4 - i)) { 246 result = '.' + result; 247 } 248 result = "" + mod + result; 249 } 250 } 251 252 m_version = result; 253 } 254 } | Popular Tags |