1 salcianu 1.1.2.1 // UComp.java, created Thu Feb 10 13:22:57 2000 by salcianu 2 salcianu 1.1.2.1 // Copyright (C) 2000 Alexandru SALCIANU <salcianu@retezat.lcs.mit.edu> 3 salcianu 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 salcianu 1.1.2.1 package harpoon.Util; 5 salcianu 1.1.2.1 6 salcianu 1.1.2.1 import java.util.Comparator; 7 salcianu 1.1.2.1 8 salcianu 1.1.2.1 /** 9 salcianu 1.1.2.1 * <code>UComp</code> is an universal comparator, which compares any two 10 salcianu 1.1.2.1 objecs by simply compraing their string representation. It is useful 11 salcianu 1.1.2.1 when you need a deterministic string representation of a set (for debug 12 salcianu 1.1.2.1 purposes). As sets don't have any ordering info, two equal sets 13 salcianu 1.1.2.1 could have different representations; instead you can convert the set to 14 salcianu 1.1.2.1 an array of elements, sort it with the help of this universal comparator 15 salcianu 1.1.2.1 and print it. 16 salcianu 1.1.2.1 * 17 salcianu 1.1.2.1 * @author Alexandru SALCIANU <salcianu@retezat.lcs.mit.edu> 18 cananian 1.2 * @version $Id: UComp.java,v 1.2 2002/02/25 21:08:55 cananian Exp $ 19 salcianu 1.1.2.1 */ 20 salcianu 1.1.2.1 public class UComp implements Comparator{ 21 salcianu 1.1.2.1 22 salcianu 1.1.2.1 public static UComp uc = new UComp(); 23 salcianu 1.1.2.1 24 salcianu 1.1.2.1 /** Compares its two arguments for order. 25 salcianu 1.1.2.1 Returns a negative integer, zero, or a positive integer as the 26 salcianu 1.1.2.1 string representation of the first argument is less than, equal 27 salcianu 1.1.2.1 to, or greater to the string representation of the second. */ 28 salcianu 1.1.2.1 public int compare(Object o1,Object o2){ 29 salcianu 1.1.2.1 if(o1 == o2) return 0; 30 salcianu 1.1.2.1 String str1 = (o1 == null)?"null":o1.toString(); 31 salcianu 1.1.2.1 String str2 = (o2 == null)?"null":o2.toString(); 32 salcianu 1.1.2.1 return str1.compareTo(str2); 33 salcianu 1.1.2.1 } 34 salcianu 1.1.2.1 35 salcianu 1.1.2.1 /** Indicates whether some other object is "equal" to" 36 salcianu 1.1.2.1 <code>this</code> Comparator. The easiest implementation: 37 salcianu 1.1.2.1 always return <code>false</code> unless 38 salcianu 1.1.2.1 <code>obj == this</code> .*/ 39 salcianu 1.1.2.1 public boolean equals(Object obj){ 40 salcianu 1.1.2.1 return this == obj; 41 salcianu 1.1.2.1 } 42 cananian 1.2 }