001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.server.filters;
021    
022    import org.apache.commons.collections.comparators.ReverseComparator;
023    
024    import java.io.Serializable;
025    import java.util.*;
026    
027    public class FilterResult {
028      private List<Object[]> rows;
029      private Filter filter;
030      public static final int SORTED_COLUMN_INDEX = 3;
031    
032      public FilterResult(Filter filter, List rows) {
033        this.rows = new ArrayList(rows);
034        this.filter = filter;
035      }
036    
037      /**
038       * @return a list of arrays
039       */
040      public List getRows() {
041        return rows;
042      }
043    
044      public int size() {
045        return rows.size();
046      }
047    
048      public Integer getSnapshotId(Object row) {
049        return (Integer) ((Object[]) row)[getSnapshotIdIndex()];
050      }
051    
052      public Integer getProjectId(Object row) {
053        return (Integer) ((Object[]) row)[getProjectIdIndex()];
054      }
055    
056      public Integer getRootProjectId(Object row) {
057        return (Integer) ((Object[]) row)[getRootProjectIdIndex()];
058      }
059    
060      public int getSnapshotIdIndex() {
061        return 0;
062      }
063    
064      public int getProjectIdIndex() {
065        return 1;
066      }
067    
068      public int getRootProjectIdIndex() {
069        return 2;
070      }
071    
072      public void sort() {
073        if (filter.isSorted()) {
074          Comparator comparator = (filter.isTextSort() ? new StringIgnoreCaseComparator(SORTED_COLUMN_INDEX) : new NumericComparator(SORTED_COLUMN_INDEX));
075          if (!filter.isAscendingSort()) {
076            comparator = new ReverseComparator(comparator);
077          }
078          Collections.sort(rows, comparator);
079        }
080      }
081    
082      public void removeUnvalidRows() {
083        int numberOfCriteria = filter.getMeasureCriteria().size();
084        if (numberOfCriteria > 0) {
085          int fromColumnIndex = (filter.isSorted() ? SORTED_COLUMN_INDEX + 1 : SORTED_COLUMN_INDEX);
086          for (Iterator<Object[]> it = rows.iterator(); it.hasNext(); ) {
087            Object[] row = it.next();
088            boolean remove = false;
089            for (int index = 0; index < numberOfCriteria; index++) {
090              if (row[fromColumnIndex + index] == null) {
091                remove = true;
092              }
093            }
094            if (remove) {
095              it.remove();
096            }
097          }
098        }
099      }
100    
101      static final class NumericComparator implements Comparator, Serializable {
102        private static final long serialVersionUID = 4627704879575964978L;
103        private int index;
104    
105        NumericComparator(int index) {
106          this.index = index;
107        }
108    
109        public int compare(Object a1, Object a2) {
110          Comparable c1 = (Comparable) ((Object[]) a1)[index];
111          Object o2 = ((Object[]) a2)[index];
112          return (c1 == null ? -1 : (o2 == null ? 1 : c1.compareTo(o2)));
113        }
114      }
115    
116      static final class StringIgnoreCaseComparator implements Comparator, Serializable {
117        private static final long serialVersionUID = 963926659201833101L;
118        private int index;
119    
120        StringIgnoreCaseComparator(int index) {
121          this.index = index;
122        }
123    
124        public int compare(Object o1, Object o2) {
125          String s1 = (String)((Object[]) o1)[index];
126          if (s1 == null) {
127            return -1;
128          }
129          String s2 = (String)((Object[]) o2)[index];
130          if (s2 == null) {
131            return 1;
132          }
133          return s1.compareToIgnoreCase(s2);
134        }
135      }
136    }
137