/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.pegadi.articlelist; import org.pegadi.model.Article; import org.pegadi.model.ArticleType; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.TreeSet; public class ArticleTypeGrouper implements Grouper { private String name; public ArticleTypeGrouper(String name) { this.name = name; } public ArticleTypeGrouper() { this("ArticleType"); } public String toString() { return getName(); } public String getName() { return name; } public boolean equals(Object obj) { return obj instanceof ArticleTypeGrouper; } public GroupedArticles group(Article[] articles, Comparator comparator) { // first, lets find all articleTypes in the incoming set. // we use TreeSet, since this class doesn't permit duplicates, // and also automagically sorts the objects for us, for the given comparator. ArrayList[] tempArrayListOut; String[] tempHeaders; ArticleSum[] tempSums; TreeSet actualSortObjects = new TreeSet(); boolean hasNulls = false; GroupedArticles groupedArticles = new GroupedArticles(); ArrayList groupIndexes; for (Article article1 : articles) { if (article1.getArticleType() != null) actualSortObjects.add(article1.getArticleType()); else hasNulls = true; } // then, we convert the TreeSet to a Vector // so that we get a sorted and indexed list of articleTypes. // if there are articles without ArticleType, create a "empty" // folder at the end. groupIndexes = new ArrayList(actualSortObjects); if (hasNulls) groupIndexes.add(new ArticleType(null, "No articleType", "")); // initalize the TreeSets int numberOfGroups = groupIndexes.size(); groupedArticles.setNumberOfGroups(numberOfGroups); tempArrayListOut = new ArrayList[numberOfGroups]; for (int i = 0; i < tempArrayListOut.length; i++) tempArrayListOut[i] = new ArrayList(); // now, iterate over all the articles, and duplicating // them into the correct TreeSet for (Article article : articles) { ArticleType tempArticleType = article.getArticleType(); int tempIndex = groupIndexes.indexOf(tempArticleType); if (tempIndex == -1) // no such articleType, put it in "empt dep." tempArrayListOut[numberOfGroups - 1].add(article); else tempArrayListOut[tempIndex].add(article); } // Sort all the arraylists for (ArrayList aTempArrayListOut : tempArrayListOut) Collections.sort(aTempArrayListOut, comparator); // update the group headers tempHeaders = new String[numberOfGroups]; for (int i = 0; i < numberOfGroups; i++) { ArticleType tempArticleType2 = (ArticleType) groupIndexes.get(i); tempHeaders[i] = tempArticleType2.getName(); } groupedArticles.setGroupHeaders(tempHeaders); // update the group sums tempSums = new ArticleSum[numberOfGroups]; for (int i = 0; i < numberOfGroups; i++) { tempSums[i] = new ArticleSum(); for (Object o : tempArrayListOut[i]) { Article tempArticle = (Article) o; int tempInt = tempArticle.getWantedNumberOfCharacters(); tempSums[i].add(tempInt); } } // update, and return the results. groupedArticles.setGroupedArticles(tempArrayListOut); groupedArticles.setGroupHeaders(tempHeaders); groupedArticles.setGroupSums(tempSums); return groupedArticles; } }