/**
* 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;
//Pegadi imports
import org.pegadi.model.Article;
import java.util.Comparator;
/**
* Compares two articles by their name. (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 ArticleByArticleNameComparator implements Comparator {
/**
* The name of the comparator
*/
protected String name;
/**
* Creates a new comparator with the given name.
*/
public ArticleByArticleNameComparator(String name) {
this.name = name;
}
/**
* Creates an new comparator with the default name.
*/
public ArticleByArticleNameComparator() {
this.name = "Name";
}
public int compare(Object o1, Object o2) {
Article art1 = (Article) o1;
Article art2 = (Article) o2;
String name1 = art1.getName();
String name2 = art2.getName();
int lastCmp;
if (name1 == null && name2 == null)
lastCmp = 0;
else if (name1 == null)
lastCmp = 1;
else if (name2 == null)
lastCmp = -1;
else
lastCmp = name1.compareTo(name2);
return lastCmp;
}
public String toString() {
return name;
}
}