/** * 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 java.util.Comparator; /** * A generic three-layer @link{Comparator} for comparing/sorting * articles by various parameters (ArticleType, Publication, * Journalist, Characters, etc...) * * @author <a href="mailto:jb@underdusken.no">Jørgen Binningsbø</a> * @version $Id$ * @see Comparator */ public class GenericComparator implements Comparator { Comparator comparator1; Comparator comparator2; Comparator comparator3; public GenericComparator(Comparator comparator1, Comparator comparator2, Comparator comparator3) { initialize(comparator1, comparator2, comparator3); } public GenericComparator(Comparator comparator1, Comparator comparator2) { initialize(comparator1, comparator2, null); } public GenericComparator(Comparator comparator1) { initialize(comparator1, null, null); } protected void initialize(Comparator comparator1, Comparator comparator2, Comparator comparator3) { this.comparator1 = comparator1; this.comparator2 = comparator2; this.comparator3 = comparator3; } public int compare(Object o1, Object o2) { int lastCmp = comparator1.compare(o1, o2); if (lastCmp == 0 & comparator2 != null) { lastCmp = comparator2.compare(o1, o2); if (lastCmp == 0 & comparator3 != null) { lastCmp = comparator3.compare(o1, o2); } } return lastCmp; } }