All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencms.module.CmsModuleVersion Maven / Gradle / Ivy

Go to download

OpenCms is an enterprise-ready, easy to use website content management system based on Java and XML technology. Offering a complete set of features, OpenCms helps content managers worldwide to create and maintain beautiful websites fast and efficiently.

There is a newer version: 17.0
Show newest version
/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH & Co. KG, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.module;

import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.util.CmsStringUtil;

/**
 * A version number for an OpenCms module.

* * A module version number has the form n1.n2.n3.n4. * Only n1 is required, n2 - n4 are optional.

* * The valid range for each n is 0 - 999. * Examples for valid version numbers are 0.9, 1.0.0.5 * or 5. * The maximum version number is 999.999.999.999.

* * The comparison is started with n1 being the most important value, * followed by n2 - n4. * For example 5.0.0.1 > 4.999.999.999 since 5 > 4.

* * For any n1 - n4, if n > 0 leading zeros are ignored. * So 001.002.004.004 = 1.2.3.4. Unrequired leading zeros are automatically * stripped from version numbers.

* * @since 6.0.0 */ public class CmsModuleVersion implements Comparable { /** Default version for new modules. */ public static final String DEFAULT_VERSION = "0.1"; /** The dot count of the version. */ private int m_dots; /** The version number (for comparisons). */ private long m_number; /** Indicates if the module version was already updated. */ private boolean m_updated; /** The version String. */ private String m_version; /** * Creates a new module version based on a String.

* * @param version the version to set */ public CmsModuleVersion(String version) { setVersion(version); } /** * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo(Object obj) { if (obj == this) { return 0; } if (obj instanceof CmsModuleVersion) { if (m_version == null) { return 0; } CmsModuleVersion other = (CmsModuleVersion)obj; if (m_number == other.m_number) { return 0; } return (m_number > other.m_number) ? 1 : -1; } return 0; } /** * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof CmsModuleVersion) { CmsModuleVersion other = (CmsModuleVersion)obj; if (m_version == null) { return (other.m_version == null); } return m_version.equals(other.m_version); } return false; } /** * Returns the current version String.

* * @return the current version String */ public String getVersion() { return m_version; } /** * @see java.lang.Object#hashCode() */ @Override public int hashCode() { if (m_version == null) { return 0; } return m_version.hashCode(); } /** * Sets the version as a String.

* * @param version the version String to set */ public void setVersion(String version) { m_number = 0L; if ((version == null) || (version.charAt(0) == '.') || (version.charAt(version.length() - 1) == '.')) { throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0)); } String[] split = CmsStringUtil.splitAsArray(version, '.'); m_dots = split.length; if (m_dots > 4) { throw new CmsIllegalArgumentException( Messages.get().container(Messages.ERR_INVALID_VERSION_LENGTH_1, version)); } String[] numbers = new String[5]; System.arraycopy(split, 0, numbers, 1, m_dots); numbers[0] = "1"; for (int i = 1 + m_dots; i < 5; i++) { numbers[i] = "0"; } for (int i = numbers.length - 1; i >= 0; i--) { try { int number = Integer.valueOf(numbers[numbers.length - i - 1]).intValue(); if ((number > 999) || (number < 0)) { throw new CmsIllegalArgumentException( Messages.get().container(Messages.ERR_INVALID_VERSION_SUBNUMBER_1, new Integer(number))); } m_number = ((long)Math.pow(1000.0, i) * number) + m_number; } catch (NumberFormatException e) { // no valid version provided throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0)); } } setVersion(m_number); } /** * @see java.lang.Object#toString() */ @Override public String toString() { return getVersion(); } /** * Increments this version number by 1 in the last digit.

*/ protected void increment() { if (m_number < 1999999999999L) { m_number += (long)Math.pow(1000.0, (4 - m_dots)); setVersion(m_number); } else { throw new CmsRuntimeException(Messages.get().container(Messages.ERR_MODULE_VERSION_NUMBER_0)); } } /** * Returns the updated status.

* * @return the updated status */ protected boolean isUpdated() { return m_updated; } /** * Sets the updated status.

* * @param updated the updated status to set */ protected void setUpdated(boolean updated) { m_updated = updated; } /** * Sets the version as a number.

* * @param number the version number to set */ private void setVersion(long number) { String result = ""; for (int i = 0; i < 4; i++) { long mod = number % 1000L; number = number / 1000L; if (m_dots >= (4 - i)) { if (m_dots > (4 - i)) { result = '.' + result; } result = "" + mod + result; } } m_version = result; } }