001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public int compare(MBMessage msg1, MBMessage msg2) {
031                    Long parentMessageId1 = new Long(msg1.getParentMessageId());
032                    Long parentMessageId2 = new Long(msg2.getParentMessageId());
033    
034                    int value = parentMessageId1.compareTo(parentMessageId2);
035    
036                    if (value == 0) {
037                            value = DateUtil.compareTo(
038                                    msg1.getCreateDate(), msg2.getCreateDate());
039                    }
040    
041                    if (value == 0) {
042                            Long messageId1 = new Long(msg1.getMessageId());
043                            Long messageId2 = new Long(msg2.getMessageId());
044    
045                            value = messageId1.compareTo(messageId2);
046                    }
047    
048                    return value;
049            }
050    
051    }