001    /**
002     * Copyright (c) 2000-2010 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.kernel.workflow.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portal.kernel.workflow.WorkflowInstance;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public abstract class BaseWorkflowInstanceStateComparator
024            extends OrderByComparator {
025    
026            public BaseWorkflowInstanceStateComparator() {
027                    this(false);
028            }
029    
030            public BaseWorkflowInstanceStateComparator(boolean ascending) {
031                    _ascending = ascending;
032            }
033    
034            public int compare(Object obj1, Object obj2) {
035                    WorkflowInstance workflowInstance1 = (WorkflowInstance)obj1;
036                    WorkflowInstance workflowInstance2 = (WorkflowInstance)obj2;
037    
038                    String state1 = workflowInstance1.getState();
039                    String state2 = workflowInstance2.getState();
040    
041                    int value = state1.compareTo(state2);
042    
043                    if (value == 0) {
044                            Long workflowInstanceId1 =
045                                    workflowInstance1.getWorkflowInstanceId();
046                            Long workflowInstanceId2 =
047                                    workflowInstance2.getWorkflowInstanceId();
048    
049                            value = workflowInstanceId1.compareTo(workflowInstanceId2);
050                    }
051    
052                    if (_ascending) {
053                            return value;
054                    }
055                    else {
056                            return -value;
057                    }
058            }
059    
060            public boolean isAscending() {
061                    return _ascending;
062            }
063    
064            private boolean _ascending;
065    
066    }