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.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            @Override
035            public int compare(Object obj1, Object obj2) {
036                    WorkflowInstance workflowInstance1 = (WorkflowInstance)obj1;
037                    WorkflowInstance workflowInstance2 = (WorkflowInstance)obj2;
038    
039                    String state1 = workflowInstance1.getState();
040                    String state2 = workflowInstance2.getState();
041    
042                    int value = state1.compareTo(state2);
043    
044                    if (value == 0) {
045                            Long workflowInstanceId1 =
046                                    workflowInstance1.getWorkflowInstanceId();
047                            Long workflowInstanceId2 =
048                                    workflowInstance2.getWorkflowInstanceId();
049    
050                            value = workflowInstanceId1.compareTo(workflowInstanceId2);
051                    }
052    
053                    if (_ascending) {
054                            return value;
055                    }
056                    else {
057                            return -value;
058                    }
059            }
060    
061            @Override
062            public boolean isAscending() {
063                    return _ascending;
064            }
065    
066            private boolean _ascending;
067    
068    }