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.trash.util.comparator;
016    
017    import com.liferay.portal.kernel.util.DateUtil;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portlet.trash.model.TrashEntry;
020    
021    /**
022     * @author Sergio Gonz??lez
023     */
024    public class EntryCreateDateComparator extends OrderByComparator<TrashEntry> {
025    
026            public static final String ORDER_BY_ASC = "TrashEntry.createDate ASC";
027    
028            public static final String ORDER_BY_DESC = "TrashEntry.createDate DESC";
029    
030            public static final String[] ORDER_BY_FIELDS = {"createDate"};
031    
032            public EntryCreateDateComparator() {
033                    this(false);
034            }
035    
036            public EntryCreateDateComparator(boolean ascending) {
037                    _ascending = ascending;
038            }
039    
040            @Override
041            public int compare(TrashEntry entry1, TrashEntry entry2) {
042                    int value = DateUtil.compareTo(
043                            entry1.getCreateDate(), entry2.getCreateDate());
044    
045                    if (_ascending) {
046                            return value;
047                    }
048                    else {
049                            return -value;
050                    }
051            }
052    
053            @Override
054            public String getOrderBy() {
055                    if (_ascending) {
056                            return ORDER_BY_ASC;
057                    }
058                    else {
059                            return ORDER_BY_DESC;
060                    }
061            }
062    
063            @Override
064            public String[] getOrderByFields() {
065                    return ORDER_BY_FIELDS;
066            }
067    
068            @Override
069            public boolean isAscending() {
070                    return _ascending;
071            }
072    
073            private final boolean _ascending;
074    
075    }