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            @Override
038            public int compare(
039                    DLFileVersion dlFileVersion1, DLFileVersion dlFileVersion2) {
040    
041                    int value = 0;
042    
043                    String version1 = dlFileVersion1.getVersion();
044    
045                    if (version1.equals(
046                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
047    
048                            return -1;
049                    }
050    
051                    String version2 = dlFileVersion2.getVersion();
052    
053                    if (version2.equals(
054                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
055    
056                            return 1;
057                    }
058    
059                    int[] versionParts1 = StringUtil.split(version1, StringPool.PERIOD, 0);
060                    int[] versionParts2 = StringUtil.split(version2, StringPool.PERIOD, 0);
061    
062                    if ((versionParts1.length != 2) && (versionParts2.length != 2)) {
063                            value = 0;
064                    }
065                    else if (versionParts1.length != 2) {
066                            value = -1;
067                    }
068                    else if (versionParts2.length != 2) {
069                            value = 1;
070                    }
071                    else if (versionParts1[0] > versionParts2[0]) {
072                            value = 1;
073                    }
074                    else if (versionParts1[0] < versionParts2[0]) {
075                            value = -1;
076                    }
077                    else if (versionParts1[1] > versionParts2[1]) {
078                            value = 1;
079                    }
080                    else if (versionParts1[1] < versionParts2[1]) {
081                            value = -1;
082                    }
083    
084                    if (_ascending) {
085                            return value;
086                    }
087                    else {
088                            return -value;
089                    }
090            }
091    
092            public boolean isAscending() {
093                    return _ascending;
094            }
095    
096            private boolean _ascending;
097    
098    }