001    /**
002     * Copyright (c) 2000-2013 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    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
021    
022    import java.util.Comparator;
023    
024    /**
025     * @author Bruno Farache
026     */
027    public class FileVersionVersionComparator implements Comparator<DLFileVersion> {
028    
029            public FileVersionVersionComparator() {
030                    this(false);
031            }
032    
033            public FileVersionVersionComparator(boolean ascending) {
034                    _ascending = ascending;
035            }
036    
037            public int compare(
038                    DLFileVersion dlFileVersion1, DLFileVersion dlFileVersion2) {
039    
040                    int value = 0;
041    
042                    String version1 = dlFileVersion1.getVersion();
043    
044                    if (version1.equals(
045                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
046    
047                            return -1;
048                    }
049    
050                    String version2 = dlFileVersion2.getVersion();
051    
052                    if (version2.equals(
053                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
054    
055                            return 1;
056                    }
057    
058                    int[] versionParts1 = StringUtil.split(version1, StringPool.PERIOD, 0);
059                    int[] versionParts2 = StringUtil.split(version2, StringPool.PERIOD, 0);
060    
061                    if ((versionParts1.length != 2) && (versionParts2.length != 2)) {
062                            value = 0;
063                    }
064                    else if (versionParts1.length != 2) {
065                            value = -1;
066                    }
067                    else if (versionParts2.length != 2) {
068                            value = 1;
069                    }
070                    else if (versionParts1[0] > versionParts2[0]) {
071                            value = 1;
072                    }
073                    else if (versionParts1[0] < versionParts2[0]) {
074                            value = -1;
075                    }
076                    else if (versionParts1[1] > versionParts2[1]) {
077                            value = 1;
078                    }
079                    else if (versionParts1[1] < versionParts2[1]) {
080                            value = -1;
081                    }
082    
083                    if (_ascending) {
084                            return value;
085                    }
086                    else {
087                            return -value;
088                    }
089            }
090    
091            public boolean isAscending() {
092                    return _ascending;
093            }
094    
095            private boolean _ascending;
096    
097    }