001    /**
002     * Copyright (c) 2000-2010 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.OrderByComparator;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
021    
022    /**
023     * @author Bruno Farache
024     */
025    public class FileVersionVersionComparator extends OrderByComparator {
026    
027            public static String ORDER_BY_ASC = "version ASC";
028    
029            public static String ORDER_BY_DESC = "version DESC";
030    
031            public static String[] ORDER_BY_FIELDS = {"version"};
032    
033            public FileVersionVersionComparator() {
034                    this(false);
035            }
036    
037            public FileVersionVersionComparator(boolean ascending) {
038                    _ascending = ascending;
039            }
040    
041            public int compare(Object obj1, Object obj2) {
042                    DLFileVersion fileVersion1 = (DLFileVersion)obj1;
043                    DLFileVersion fileVersion2 = (DLFileVersion)obj2;
044    
045                    int value = 0;
046    
047                    int[] versionParts1 = StringUtil.split(
048                            fileVersion1.getVersion(), StringPool.PERIOD, 0);
049                    int[] versionParts2 = StringUtil.split(
050                            fileVersion2.getVersion(), StringPool.PERIOD, 0);
051    
052                    if ((versionParts1.length != 2) && (versionParts2.length != 2)) {
053                            value = 0;
054                    }
055                    else if ((versionParts1.length != 2)) {
056                            value = -1;
057                    }
058                    else if ((versionParts2.length != 2)) {
059                            value = 1;
060                    }
061                    else if (versionParts1[0] > versionParts2[0]) {
062                            value = 1;
063                    }
064                    else if (versionParts1[0] < versionParts2[0]) {
065                            value = -1;
066                    }
067                    else if (versionParts1[1] > versionParts2[1]) {
068                            value = 1;
069                    }
070                    else if (versionParts1[1] < versionParts2[1]) {
071                            value = -1;
072                    }
073    
074                    if (_ascending) {
075                            return value;
076                    }
077                    else {
078                            return -value;
079                    }
080            }
081    
082            public String getOrderBy() {
083                    if (_ascending) {
084                            return ORDER_BY_ASC;
085                    }
086                    else {
087                            return ORDER_BY_DESC;
088                    }
089            }
090    
091            public String[] getOrderByFields() {
092                    return ORDER_BY_FIELDS;
093            }
094    
095            public boolean isAscending() {
096                    return _ascending;
097            }
098    
099            private boolean _ascending;
100    
101    }