001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.DLFileVersion;
020    
021    import java.util.Comparator;
022    
023    /**
024     * @author Bruno Farache
025     */
026    public class FileVersionVersionComparator
027            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(DLFileVersion fileVersion1, DLFileVersion fileVersion2) {
038                    int value = 0;
039    
040                    int[] versionParts1 = StringUtil.split(
041                            fileVersion1.getVersion(), StringPool.PERIOD, 0);
042                    int[] versionParts2 = StringUtil.split(
043                            fileVersion2.getVersion(), StringPool.PERIOD, 0);
044    
045                    if ((versionParts1.length != 2) && (versionParts2.length != 2)) {
046                            value = 0;
047                    }
048                    else if ((versionParts1.length != 2)) {
049                            value = -1;
050                    }
051                    else if ((versionParts2.length != 2)) {
052                            value = 1;
053                    }
054                    else if (versionParts1[0] > versionParts2[0]) {
055                            value = 1;
056                    }
057                    else if (versionParts1[0] < versionParts2[0]) {
058                            value = -1;
059                    }
060                    else if (versionParts1[1] > versionParts2[1]) {
061                            value = 1;
062                    }
063                    else if (versionParts1[1] < versionParts2[1]) {
064                            value = -1;
065                    }
066    
067                    if (_ascending) {
068                            return value;
069                    }
070                    else {
071                            return -value;
072                    }
073            }
074    
075            public boolean isAscending() {
076                    return _ascending;
077            }
078    
079            private boolean _ascending;
080    
081    }