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