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.social.util.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portlet.social.model.SocialActivitySet;
019    
020    /**
021     * @author Matthew Kong
022     */
023    public class SocialActivitySetModifiedDateComparator extends OrderByComparator {
024    
025            public static final String ORDER_BY_ASC =
026                    "SocialActivitySet.modifiedDate ASC";
027    
028            public static final String ORDER_BY_DESC =
029                    "SocialActivitySet.modifiedDate DESC";
030    
031            public static final String[] ORDER_BY_FIELDS = {"modifiedDate"};
032    
033            public SocialActivitySetModifiedDateComparator() {
034                    this(false);
035            }
036    
037            public SocialActivitySetModifiedDateComparator(boolean ascending) {
038                    _ascending = ascending;
039            }
040    
041            @Override
042            public int compare(Object obj1, Object obj2) {
043                    SocialActivitySet activitySet1 = (SocialActivitySet)obj1;
044                    SocialActivitySet activitySet2 = (SocialActivitySet)obj2;
045    
046                    long modifiedDate1 = activitySet1.getModifiedDate();
047                    long modifiedDate2 = activitySet2.getModifiedDate();
048    
049                    int value = 0;
050    
051                    if (modifiedDate1 == modifiedDate2) {
052                            value = 0;
053                    }
054                    else if (modifiedDate1 < modifiedDate2) {
055                            value = -1;
056                    }
057                    else {
058                            value = 1;
059                    }
060    
061                    if (_ascending) {
062                            return value;
063                    }
064                    else {
065                            return -value;
066                    }
067            }
068    
069            @Override
070            public String getOrderBy() {
071                    if (_ascending) {
072                            return ORDER_BY_ASC;
073                    }
074                    else {
075                            return ORDER_BY_DESC;
076                    }
077            }
078    
079            @Override
080            public String[] getOrderByFields() {
081                    return ORDER_BY_FIELDS;
082            }
083    
084            @Override
085            public boolean isAscending() {
086                    return _ascending;
087            }
088    
089            private boolean _ascending;
090    
091    }