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.messageboards.util.comparator;
016    
017    import com.liferay.portal.kernel.util.DateUtil;
018    import com.liferay.portlet.messageboards.model.MBMessage;
019    
020    import java.io.Serializable;
021    
022    import java.util.Comparator;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class MessageThreadComparator
028            implements Comparator<MBMessage>, Serializable {
029    
030            @Override
031            public int compare(MBMessage msg1, MBMessage msg2) {
032                    Long parentMessageId1 = new Long(msg1.getParentMessageId());
033                    Long parentMessageId2 = new Long(msg2.getParentMessageId());
034    
035                    int value = parentMessageId1.compareTo(parentMessageId2);
036    
037                    if (value == 0) {
038                            value = DateUtil.compareTo(
039                                    msg1.getCreateDate(), msg2.getCreateDate());
040                    }
041    
042                    if (value == 0) {
043                            Long messageId1 = new Long(msg1.getMessageId());
044                            Long messageId2 = new Long(msg2.getMessageId());
045    
046                            value = messageId1.compareTo(messageId2);
047                    }
048    
049                    return value;
050            }
051    
052    }