001    /**
002     * Copyright (c) 2000-2012 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.dynamicdatalists.util.comparator;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
020    
021    import java.util.Comparator;
022    
023    /**
024     * @author Marcellus Tavares
025     */
026    public class DDLRecordVersionVersionComparator
027            implements Comparator<DDLRecordVersion> {
028    
029            public DDLRecordVersionVersionComparator() {
030                    this(false);
031            }
032    
033            public DDLRecordVersionVersionComparator(boolean ascending) {
034                    _ascending = ascending;
035            }
036    
037            public int compare(
038                    DDLRecordVersion recordVersion1, DDLRecordVersion recordVersion2) {
039    
040                    int value = 0;
041    
042                    String version1 = recordVersion1.getVersion();
043                    String version2 = recordVersion2.getVersion();
044    
045                    int[] versionParts1 = StringUtil.split(version1, StringPool.PERIOD, 0);
046                    int[] versionParts2 = StringUtil.split(version2, StringPool.PERIOD, 0);
047    
048                    if ((versionParts1.length != 2) && (versionParts2.length != 2)) {
049                            value = 0;
050                    }
051                    else if ((versionParts1.length != 2)) {
052                            value = -1;
053                    }
054                    else if ((versionParts2.length != 2)) {
055                            value = 1;
056                    }
057                    else if (versionParts1[0] > versionParts2[0]) {
058                            value = 1;
059                    }
060                    else if (versionParts1[0] < versionParts2[0]) {
061                            value = -1;
062                    }
063                    else if (versionParts1[1] > versionParts2[1]) {
064                            value = 1;
065                    }
066                    else if (versionParts1[1] < versionParts2[1]) {
067                            value = -1;
068                    }
069    
070                    if (_ascending) {
071                            return value;
072                    }
073                    else {
074                            return -value;
075                    }
076            }
077    
078            public boolean isAscending() {
079                    return _ascending;
080            }
081    
082            private boolean _ascending;
083    
084    }