001 package edu.nrao.sss.sort; 002 003 import java.util.Comparator; 004 005 /** 006 * A sort key for {@code Integers}s (and {@code int}s). 007 * <p/> 008 * The most common way to extend this class is to subclass it into a class 009 * that also implements {@link Comparator}. The main job of the subclass 010 * is to provide the integers to this parent class. Example: 011 * <pre> 012 * /** 013 * * Sorts engines based on the number of cylinders they have. 014 * */ 015 * public class EngineCylinderCountKey extends IntSortKey 016 * implements Comparator<Engine> 017 * { 018 * public int compare(Engine e1, Engine e2) 019 * { 020 * return compareObjects(e1.getCylinders().size(), 021 * e2.getCylinders().size()); 022 * } 023 * }</pre> 024 * <p> 025 * Clients of the example <tt>EngineCylinderCountKey</tt> class are able to 026 * configure that comparator via the {@link #setOrder(SortOrder)} method, 027 * something they cannot do with 028 * the plain <tt>Comparator</tt> interface. They may then place instances 029 * of this class in a {@link CompoundComparator}.</p> 030 * <p> 031 * <b>Version Info:</b> 032 * <table style="margin-left:2em"> 033 * <tr><td>$Revision: 593 $</td></tr> 034 * <tr><td>$Date: 2007-05-07 15:54:14 -0600 (Mon, 07 May 2007) $</td></tr> 035 * <tr><td>$Author: dharland $</td></tr> 036 * </table></p> 037 * 038 * @author David M. Harland 039 * @since 2007-05-07 040 */ 041 public abstract class IntSortKey 042 extends SortKey<Integer> 043 { 044 /** Helps create a new instance. */ 045 protected IntSortKey() 046 { 047 super(); 048 } 049 050 /** 051 * Uses the natural ordering of {@code Integer}. 052 */ 053 protected int compareNatural(Integer o1, Integer o2) 054 { 055 return o1.compareTo(o2); 056 } 057 058 /** 059 * Uses the natural ordering of {@code Integer}. 060 */ 061 protected int compareAscending(Integer o1, Integer o2) 062 { 063 return compareNatural(o1, o2); 064 } 065 }