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.portal.util.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portal.model.Layout;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class LayoutPriorityComparator extends OrderByComparator {
024    
025            public static final String ORDER_BY_ASC = "Layout.priority ASC";
026    
027            public static final String[] ORDER_BY_FIELDS = {"priority"};
028    
029            public LayoutPriorityComparator() {
030            }
031    
032            public LayoutPriorityComparator(Layout layout, boolean lessThan) {
033                    _layout = layout;
034                    _lessThan = lessThan;
035            }
036    
037            @Override
038            public int compare(Object obj1, Object obj2) {
039                    Layout layout1 = (Layout)obj1;
040                    Layout layout2 = (Layout)obj2;
041    
042                    int priority1 = layout1.getPriority();
043                    int priority2 = layout2.getPriority();
044    
045                    if (priority1 > priority2) {
046                            return 1;
047                    }
048                    else if (priority1 < priority2) {
049                            return -1;
050                    }
051                    else {
052                            if (_layout != null) {
053                                    if (_layout.equals(layout1)) {
054                                            if (_lessThan) {
055                                                    return 1;
056                                            }
057                                            else {
058                                                    return -1;
059                                            }
060                                    }
061                                    else if (_layout.equals(layout2)) {
062                                            if (_lessThan) {
063                                                    return -1;
064                                            }
065                                            else {
066                                                    return 1;
067                                            }
068                                    }
069                            }
070    
071                            return 0;
072                    }
073            }
074    
075            @Override
076            public String getOrderBy() {
077                    return ORDER_BY_ASC;
078            }
079    
080            @Override
081            public String[] getOrderByFields() {
082                    return ORDER_BY_FIELDS;
083            }
084    
085            @Override
086            public boolean isAscending() {
087                    return true;
088            }
089    
090            private Layout _layout;
091            private boolean _lessThan;
092    
093    }