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