001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.module; 029 030import org.opencms.main.CmsIllegalArgumentException; 031import org.opencms.main.CmsRuntimeException; 032import org.opencms.util.CmsStringUtil; 033 034/** 035 * A version number for an OpenCms module.<p> 036 * 037 * A module version number has the form <code>n1.n2.n3.n4</code>. 038 * Only <code>n1</code> is required, <code>n2 - n4</code> are optional.<p> 039 * 040 * The valid range for each <code>n</code> is 0 - 999. 041 * Examples for valid version numbers are <code>0.9</code>, <code>1.0.0.5</code> 042 * or <code>5</code>. 043 * The maximum version number is <code>999.999.999.999</code>.<p> 044 * 045 * The comparison is started with <code>n1</code> being the most important value, 046 * followed by <code>n2 - n4</code>. 047 * For example <code>5.0.0.1 > 4.999.999.999</code> since <code>5 > 4</code>.<p> 048 * 049 * For any <code>n1 - n4</code>, if <code>n > 0</code> leading zeros are ignored. 050 * So <code>001.002.004.004 = 1.2.3.4</code>. Unrequired leading zeros are automatically 051 * stripped from version numbers.<p> 052 * 053 * @since 6.0.0 054 */ 055public class CmsModuleVersion implements Comparable<Object> { 056 057 /** Default version for new modules. */ 058 public static final String DEFAULT_VERSION = "0.1"; 059 060 /** The dot count of the version. */ 061 private int m_dots; 062 063 /** The version number (for comparisons). */ 064 private long m_number; 065 066 /** Indicates if the module version was already updated. */ 067 private boolean m_updated; 068 069 /** The version String. */ 070 private String m_version; 071 072 /** 073 * Creates a new module version based on a String.<p> 074 * 075 * @param version the version to set 076 */ 077 public CmsModuleVersion(String version) { 078 079 setVersion(version); 080 } 081 082 /** 083 * @see java.lang.Comparable#compareTo(java.lang.Object) 084 */ 085 public int compareTo(Object obj) { 086 087 if (obj == this) { 088 return 0; 089 } 090 if (obj instanceof CmsModuleVersion) { 091 if (m_version == null) { 092 return 0; 093 } 094 CmsModuleVersion other = (CmsModuleVersion)obj; 095 if (m_number == other.m_number) { 096 return 0; 097 } 098 return (m_number > other.m_number) ? 1 : -1; 099 } 100 return 0; 101 } 102 103 /** 104 * @see java.lang.Object#equals(java.lang.Object) 105 */ 106 @Override 107 public boolean equals(Object obj) { 108 109 if (obj == this) { 110 return true; 111 } 112 if (obj instanceof CmsModuleVersion) { 113 CmsModuleVersion other = (CmsModuleVersion)obj; 114 if (m_version == null) { 115 return (other.m_version == null); 116 } 117 return m_version.equals(other.m_version); 118 } 119 return false; 120 } 121 122 /** 123 * Returns the current version String.<p> 124 * 125 * @return the current version String 126 */ 127 public String getVersion() { 128 129 return m_version; 130 } 131 132 /** 133 * @see java.lang.Object#hashCode() 134 */ 135 @Override 136 public int hashCode() { 137 138 if (m_version == null) { 139 return 0; 140 } 141 142 return m_version.hashCode(); 143 } 144 145 /** 146 * Sets the version as a String.<p> 147 * 148 * @param version the version String to set 149 */ 150 public void setVersion(String version) { 151 152 m_number = 0L; 153 if ((version == null) || (version.charAt(0) == '.') || (version.charAt(version.length() - 1) == '.')) { 154 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0)); 155 } 156 String[] split = CmsStringUtil.splitAsArray(version, '.'); 157 m_dots = split.length; 158 if (m_dots > 4) { 159 throw new CmsIllegalArgumentException( 160 Messages.get().container(Messages.ERR_INVALID_VERSION_LENGTH_1, version)); 161 } 162 String[] numbers = new String[5]; 163 System.arraycopy(split, 0, numbers, 1, m_dots); 164 numbers[0] = "1"; 165 for (int i = 1 + m_dots; i < 5; i++) { 166 numbers[i] = "0"; 167 } 168 for (int i = numbers.length - 1; i >= 0; i--) { 169 try { 170 int number = Integer.valueOf(numbers[numbers.length - i - 1]).intValue(); 171 172 if ((number > 999) || (number < 0)) { 173 throw new CmsIllegalArgumentException( 174 Messages.get().container(Messages.ERR_INVALID_VERSION_SUBNUMBER_1, new Integer(number))); 175 } 176 m_number = ((long)Math.pow(1000.0, i) * number) + m_number; 177 } catch (NumberFormatException e) { 178 // no valid version provided 179 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0)); 180 } 181 } 182 183 setVersion(m_number); 184 } 185 186 /** 187 * @see java.lang.Object#toString() 188 */ 189 @Override 190 public String toString() { 191 192 return getVersion(); 193 } 194 195 /** 196 * Increments this version number by 1 in the last digit.<p> 197 */ 198 protected void increment() { 199 200 if (m_number < 1999999999999L) { 201 m_number += (long)Math.pow(1000.0, (4 - m_dots)); 202 setVersion(m_number); 203 } else { 204 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_MODULE_VERSION_NUMBER_0)); 205 } 206 } 207 208 /** 209 * Returns the updated status.<p> 210 * 211 * @return the updated status 212 */ 213 protected boolean isUpdated() { 214 215 return m_updated; 216 } 217 218 /** 219 * Sets the updated status.<p> 220 * 221 * @param updated the updated status to set 222 */ 223 protected void setUpdated(boolean updated) { 224 225 m_updated = updated; 226 } 227 228 /** 229 * Sets the version as a number.<p> 230 * 231 * @param number the version number to set 232 */ 233 private void setVersion(long number) { 234 235 String result = ""; 236 for (int i = 0; i < 4; i++) { 237 long mod = number % 1000L; 238 number = number / 1000L; 239 if (m_dots >= (4 - i)) { 240 if (m_dots > (4 - i)) { 241 result = '.' + result; 242 } 243 result = "" + mod + result; 244 } 245 } 246 247 m_version = result; 248 } 249}