/**
* 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.Publication;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
public class PublicationGrouper implements Grouper {
private String name;
public PublicationGrouper(String name) {
this.name = name;
}
public PublicationGrouper() {
this("Publication");
}
public String toString() {
return this.getName();
}
public String getName() {
return name;
}
public boolean equals(Object obj) {
return obj instanceof PublicationGrouper;
}
public GroupedArticles group(Article[] articles, Comparator comparator) {
// first, lets find all publications 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.getPublication() != null)
actualSortObjects.add(article1.getPublication());
else
hasNulls = true;
}
// then, we convert the TreeSet to a Vector
// so that we get a sorted and indexed list of publications.
// if there are articles without Publication, create a "empty"
// folder at the end.
groupIndexes = new ArrayList(actualSortObjects);
if (hasNulls)
groupIndexes.add(new Publication(0, "No publication"));
// initalize the ArrayLists
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 ArrayList
for (Article article : articles) {
Publication tempPublication = article.getPublication();
int tempIndex = groupIndexes.indexOf(tempPublication);
if (tempIndex == -1) // no such publication, 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++) {
Publication tempPublication2 = (Publication) groupIndexes.get(i);
tempHeaders[i] = tempPublication2.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;
}
}