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(Messages.get().container(
160                Messages.ERR_INVALID_VERSION_LENGTH_1,
161                version));
162        }
163        String[] numbers = new String[5];
164        System.arraycopy(split, 0, numbers, 1, m_dots);
165        numbers[0] = "1";
166        for (int i = 1 + m_dots; i < 5; i++) {
167            numbers[i] = "0";
168        }
169        for (int i = numbers.length - 1; i >= 0; i--) {
170            try {
171                int number = Integer.valueOf(numbers[numbers.length - i - 1]).intValue();
172
173                if ((number > 999) || (number < 0)) {
174                    throw new CmsIllegalArgumentException(Messages.get().container(
175                        Messages.ERR_INVALID_VERSION_SUBNUMBER_1,
176                        new Integer(number)));
177                }
178                m_number = ((long)Math.pow(1000.0, i) * number) + m_number;
179            } catch (NumberFormatException e) {
180                // no valid version provided
181                throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0));
182            }
183        }
184
185        setVersion(m_number);
186    }
187
188    /**
189     * @see java.lang.Object#toString()
190     */
191    @Override
192    public String toString() {
193
194        return getVersion();
195    }
196
197    /**
198     * Increments this version number by 1 in the last digit.<p>
199     */
200    protected void increment() {
201
202        if (m_number < 1999999999999L) {
203            m_number += (long)Math.pow(1000.0, (4 - m_dots));
204            setVersion(m_number);
205        } else {
206            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_MODULE_VERSION_NUMBER_0));
207        }
208    }
209
210    /**
211     * Returns the updated status.<p>
212     *
213     * @return the updated status
214     */
215    protected boolean isUpdated() {
216
217        return m_updated;
218    }
219
220    /**
221     * Sets the updated status.<p>
222     *
223     * @param updated the updated status to set
224     */
225    protected void setUpdated(boolean updated) {
226
227        m_updated = updated;
228    }
229
230    /**
231     * Sets the version as a number.<p>
232     * 
233     * @param number the version number to set
234     */
235    private void setVersion(long number) {
236
237        String result = "";
238        for (int i = 0; i < 4; i++) {
239            long mod = number % 1000L;
240            number = number / 1000L;
241            if (m_dots >= (4 - i)) {
242                if (m_dots > (4 - i)) {
243                    result = '.' + result;
244                }
245                result = "" + mod + result;
246            }
247        }
248
249        m_version = result;
250    }
251}