001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.util.comparator;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
020    
021    import java.util.Comparator;
022    
023    /**
024     * @author Iv??n Zaera
025     */
026    public class VersionNumberComparator implements Comparator<String> {
027    
028            public VersionNumberComparator() {
029                    this(false);
030            }
031    
032            public VersionNumberComparator(boolean ascending) {
033                    _ascending = ascending;
034            }
035    
036            @Override
037            public int compare(String version1, String version2) {
038                    int value = 0;
039    
040                    if (version1.equals(
041                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
042    
043                            return -1;
044                    }
045    
046                    if (version2.equals(
047                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
048    
049                            return 1;
050                    }
051    
052                    int[] versionParts1 = StringUtil.split(version1, StringPool.PERIOD, 0);
053                    int[] versionParts2 = StringUtil.split(version2, StringPool.PERIOD, 0);
054    
055                    if ((versionParts1.length != 2) && (versionParts2.length != 2)) {
056                            value = 0;
057                    }
058                    else if (versionParts1.length != 2) {
059                            value = -1;
060                    }
061                    else if (versionParts2.length != 2) {
062                            value = 1;
063                    }
064                    else if (versionParts1[0] > versionParts2[0]) {
065                            value = 1;
066                    }
067                    else if (versionParts1[0] < versionParts2[0]) {
068                            value = -1;
069                    }
070                    else if (versionParts1[1] > versionParts2[1]) {
071                            value = 1;
072                    }
073                    else if (versionParts1[1] < versionParts2[1]) {
074                            value = -1;
075                    }
076    
077                    if (_ascending) {
078                            return value;
079                    }
080                    else {
081                            return -value;
082                    }
083            }
084    
085            public boolean isAscending() {
086                    return _ascending;
087            }
088    
089            private final boolean _ascending;
090    
091    }