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.tools.comparator;
016    
017    import java.util.Comparator;
018    import java.util.List;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class ColumnsComparator implements Comparator<Object> {
024    
025            public ColumnsComparator(String columnName) {
026                    this(new String[] {columnName});
027            }
028    
029            public ColumnsComparator(List<String> columnNames) {
030                    this(columnNames.toArray(new String[columnNames.size()]));
031            }
032    
033            public ColumnsComparator(String[] columnNames) {
034                    _columnNames = columnNames;
035            }
036    
037            public int compare(Object obj1, Object obj2) {
038                    Object[] column1 = (Object[])obj1;
039                    Object[] column2 = (Object[])obj2;
040    
041                    String columnName1 = (String)column1[0];
042                    String columnName2 = (String)column2[0];
043    
044                    int x = -1;
045    
046                    for (int i = 0; i < _columnNames.length; i++) {
047                            if (_columnNames[i].equals(columnName1)) {
048                                    x = i;
049    
050                                    break;
051                            }
052                    }
053    
054                    int y = -1;
055    
056                    for (int i = 0; i < _columnNames.length; i++) {
057                            if (_columnNames[i].equals(columnName2)) {
058                                    y = i;
059    
060                                    break;
061                            }
062                    }
063    
064                    if ((x == -1) && (y > -1)) {
065                            return 1;
066                    }
067                    else if ((x > -1) && (y == -1)) {
068                            return -1;
069                    }
070                    else if ((x > -1) && (y > -1)) {
071                            if (x < y) {
072                                    return -1;
073                            }
074                            else if (x > y) {
075                                    return 1;
076                            }
077                    }
078    
079                    return 0;
080            }
081    
082            private String[] _columnNames;
083    
084    }