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.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    }