/**
* 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;
//resys-imports
import no.dusken.common.model.Person;
import org.pegadi.model.Article;
import java.util.Comparator;
/**
* Compares two articles by the name of their journalists.
* (and that only, does not fail back to ID should the names match).
*
* @author <a href="mailto:jb@underdusken.no">Jørgen Binningsbø</a>
* @version $Id$
* @see Comparator
*/
public class ArticleByJournalistComparator implements Comparator {
/**
* The name of the comparator
*/
protected String name;
/**
* Creates a new comparator with the given name.
*/
public ArticleByJournalistComparator(String name) {
this.name = name;
}
/**
* Creates an new comparator with the default name.
*/
public ArticleByJournalistComparator() {
this.name = "Journalist";
}
public int compare(Object o1, Object o2) {
int lastCmp;
Article art1 = (Article) o1;
Article art2 = (Article) o2;
Person journ1 = art1.getJournalist();
Person journ2 = art2.getJournalist();
if (journ1 == null && journ2 == null)
lastCmp = 0;
else if (journ1 == null)
lastCmp = 1;
else if (journ2 == null)
lastCmp = -1;
else
lastCmp = art1.getJournalist().getName().compareTo(art2.getJournalist().getName());
return lastCmp;
}
public String toString() {
return name;
}
}