//
//  KeyValueSorting.java
//  xal
//
//  Created by Tom Pelaia on 1/30/2009.
//  Copyright 2009 Oak Ridge National Lab. All rights reserved.
//

package xal.tools.data;

import java.util.Comparator;
import java.util.List;


/** factory to support sorting using the key value adaptor */
public class KeyValueSorting {
	/** indicates ascending order */
	static final public int DESCENDING = -1;
	
	/** indicates ascending order */
	static final public int ASCENDING = 1;
	
	
	/** 
	 * Generate a comparator that compares objects according to the method obtained by a key value adaptor generated internally.
	 * The keyed values being compared are assumed to be comparable.
	 * @param keyPath indicates the method used for comparison
	 * @param order either ASCENDING or DESCENDING
	 * @return a comparator which compares objects according to the keyed parameter
	 */
	static public Comparator comparatorForKeyPath( final String keyPath, final int order ) {
		return comparatorForKeyPath( new KeyValueAdaptor(), keyPath, order );
	}
	
	
	/** 
	 * Generate a comparator that compares objects according to the method obtained by the key value adaptor
	 * The keyed values being compared are assumed to be comparable.
	 * @param adaptor key value adaptor used for getting the method associated with the key
	 * @param keyPath indicates the method used for comparison
	 * @param order either ASCENDING or DESCENDING
	 * @return a comparator which compares objects according to the keyed parameter
	 */
	static public Comparator comparatorForKeyPath( final KeyValueAdaptor adaptor, final String keyPath, final int order ) {
		return new Comparator() {
			/** compares the two items for order */
            @SuppressWarnings( "unchecked" )    // no way to predetermine the value types
			public int compare( final Object item1, final Object item2 ) {
				final Comparable value1 = (Comparable)adaptor.valueForKeyPath( item1, keyPath );
				final Comparable value2 = (Comparable)adaptor.valueForKeyPath( item2, keyPath );
				return order * value1.compareTo( value2);
			}
			
			/** the specified object is equal to this comparator */
			public boolean equals( final Object object ) {
				return object == this;
			}
		};
	}
	
	
	/**
	 * Coalesce the specified comparators to generate a compound comparator whose sub comparators are evaluated in the specified order.
	 * @param comparators array of parameters to coalesce taken in order (sort first by the first comparator and last by the last comparator)
	 * @return the compound comparator
	 */
	static public Comparator compoundComparator( final Comparator ... comparators ) {
		return new Comparator() {
			/** compares the two items for order */
            @SuppressWarnings( "unchecked" )    // no way to predetermine the item types
			public int compare( final Object item1, final Object item2 ) {
				for ( final Comparator comparator : comparators ) {
					final int ordering = comparator.compare( item1, item2 );
					if ( ordering != 0 ) return ordering;
				}
				return 0;
			}
			
			/** the specified object is equal to this comparator */
			public boolean equals( final Object object ) {
				return object == this;
			}
		};
	}
}

@@ -506,38 +506,38 @@
 r that compares 
-object
+record
 s according to t
@@ -871,34 +871,59 @@
 atic public 
-Comparator
+%3CRecordType%3E Comparator%3CRecordType%3E
  comparatorF
@@ -984,16 +984,44 @@
 %09return 
+KeyValueSorting.%3CRecordType%3E
 comparat
@@ -1120,38 +1120,38 @@
 r that compares 
-object
+record
 s according to t
@@ -1554,34 +1554,59 @@
 atic public 
-Comparator
+%3CRecordType%3E Comparator%3CRecordType%3E
  comparatorF
@@ -1704,32 +1704,44 @@
 n new Comparator
+%3CRecordType%3E
 () %7B%0A%09%09%09/** comp
@@ -1895,39 +1895,51 @@
 nal 
-Object item1, final Object item
+RecordType record1, final RecordType record
 2 ) 
@@ -2007,20 +2007,22 @@
 eyPath( 
-item
+record
 1, keyPa
@@ -2041,40 +2041,24 @@
 nal 
-Comparable value2 = (Comparable)
+Object value2 = 
 adap
@@ -2078,20 +2078,22 @@
 eyPath( 
-item
+record
 2, keyPa
@@ -2141,16 +2141,17 @@
 ( value2
+ 
 );%0A%09%09%09%7D%0A
@@ -2640,26 +2640,51 @@
  public 
-Comparator
+%3CRecordType%3E Comparator%3CRecordType%3E
  compoun
@@ -2712,16 +2712,28 @@
 mparator
+%3CRecordType%3E
  ... com
@@ -2768,16 +2768,28 @@
 mparator
+%3CRecordType%3E
 () %7B%0A%09%09%09
@@ -2950,39 +2950,51 @@
 nal 
-Object item1, final Object item
+RecordType record1, final RecordType record
 2 ) 
@@ -3021,16 +3021,28 @@
 mparator
+%3CRecordType%3E
  compara
@@ -3113,19 +3113,23 @@
 re( 
-item1, item
+record1, record
 2 );

Use generics in KeyValueSorting to address compiler warnings.