package com.mindprod.example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import static java.lang.System.*;
/**
* Demonstrate use of generic Comparables and Comparators.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-05-23 developed from snippet
* @since 2009-05-23
*/
public class TestGenericComparable
{
/**
* test harness
*
* @param args not used
*/
public static void main( String[] args )
{
final ArrayList<Pair> listOfPairs = new ArrayList<>( 4 );
listOfPairs.add( new Pair( "index.html", 1700000 ) );
listOfPairs.add( new Pair( "unmain.html", 2500000 ) );
listOfPairs.add( new Pair( "feedback.html", 10000 ) );
listOfPairs.add( new Pair( "zip.html", 43449 ) );
Collections.sort( listOfPairs );
for ( Pair p : listOfPairs )
{
out.println( p.count + " " + p.filename );
}
Collections.sort( listOfPairs, new Numerically() );
final Pair[] arrayOfPairs = listOfPairs.toArray( new Pair[ listOfPairs.size() ] );
Arrays.sort( arrayOfPairs );
Arrays.sort( arrayOfPairs, new Numerically() );
for ( Pair p : arrayOfPairs )
{
out.println( p.count + " " + p.filename );
}
}
/**
* Case insensitive ascending by filename.
* <p/>
* Defines an alternate sort order for Pair.
*/
private static class Numerically implements Comparator<Pair>
{
/**
* Case insensitive ascending by filename.
* Defines an alternate sort order for Pair with JDK 1.5+ generics.
* Compare two Pair Objects.
* Compares count then filename.
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first Pair to compare
* @param b second Pair to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( Pair a, Pair b )
{
int diff = a.count - b.count;
if ( diff != 0 )
{
return diff;
}
return a.filename.compareToIgnoreCase( b.filename );
}
}
/**
* Class to represent an Filename/Count pair.
*/
static class Pair implements Comparable<Pair>
{
/**
* The name of one file to be described.
*/
final String filename;
/**
* hit count.
*/
final int count;
/**
* Constructor.
*
* @param filename filename
* @param count some number associated with the file.
*/
Pair( String filename, int count )
{
this.filename = filename;
this.count = count;
}
/**
* Case insensitive ascending by filename.
* Defines default the sort order for Pair Objects.
* Compare this Pair with another Pair with JDK 1.5+ generics.
* Compares filename.
* Informally, returns (this-other) or +ve if this is more positive than other.
*
* @param other other Pair to compare with this one
*
* @return +ve if this>other, 0 if this==other, -ve if this<other
*/
public final int compareTo( Pair other )
{
return this.filename.compareToIgnoreCase( other.filename );
}
}
}