sabato 17 marzo 2012

Sorting java object with Comparable

A sample Price sorting sample  (by price and date) with comparable



import java.util.*;

public class InstrumentPricesSorter {

    static public List <InstrumentPrice> sortPrices (List <InstrumentPrice>  list2Sort){
        List  <InstrumentPrice>  result = new ArrayList  <InstrumentPrice>  ();
       
        Collections.sort(list2Sort);
        //Collections.reverse(list2Sort);
        // riordino la collezione in base al prezzo e alla data
       
        HashMap < String   , InstrumentPrice> myMap = new HashMap   < String   , InstrumentPrice>();
        for ( InstrumentPrice  price :  list2Sort )      {
             if ( ! myMap.containsKey(price.getInstrumentId())) {
           
             myMap.put(price.getInstrumentId() , price);
             }
        }
        Collection  <InstrumentPrice> collection =  myMap.values();
        result.addAll(collection);
       
        return result;
    }
   
   
   
   
    public static void main (String [] args ){
        List <InstrumentPrice> list = new ArrayList <InstrumentPrice> ();
        list.add(new InstrumentPrice ("ISIN001",  new Date(2012, 2, 1)  , 10 ));
        list.add(new InstrumentPrice ("ISIN001",  new Date(2012, 2, 2)  , 9 ));
        list.add(new InstrumentPrice ("ISIN001",  new Date(2012, 2, 3)  , 8 ));
        list.add(new InstrumentPrice ("ISIN002",  new Date(2012, 2, 1)  , 56 ));
        list.add(new InstrumentPrice ("ISIN002",  new Date(2012, 2, 2)  , 58 ));
        List <InstrumentPrice >  result = InstrumentPricesSorter.sortPrices(list);
        for ( InstrumentPrice item      : result ){
            System.out.println(item.getInstrumentId() + " " + item.getValue() +  "  " +  item.getDate() );
        }
    }
   
   
}




class InstrumentPrice  implements Comparable <InstrumentPrice>{
    private  String instrumentId;
    private Date date;
    private long value;
   

    public InstrumentPrice(String instrumentId, Date date, long value){
        this.instrumentId = instrumentId;
        this.date = date;
        this.value = value;
    }

    public String getInstrumentId (){
        return this.instrumentId;
    }
   
   
    public Date getDate (){
        return this.date;
    }
   
    public long getValue (){
        return this.value;
    }
   
    public boolean equals (Object obj){
        if ( (obj instanceof  InstrumentPrice)   &&  (   ((InstrumentPrice)obj).instrumentId.equals( this.instrumentId) )   &&  ((InstrumentPrice)obj).date.equals( this.date)  &&   ((InstrumentPrice)obj).getValue() == this.value) {
            return true;
        } else {
            return false;
        }
    }
   
    public int hashCode (){
        return this.instrumentId.hashCode() + this.date.hashCode() + Double.toString(this.value).hashCode() ;
    }
   

    @Override
    public int compareTo(InstrumentPrice o) {
        //TODO verify compare
        int idCompare = this.instrumentId.compareTo(o.instrumentId);
        if ( idCompare != 0 ){
            return idCompare;
        } else  {
            // ho trovato si riferiscono stesso id
            int valueCompare =   Double.compare(this.value, o.value);
            if (valueCompare != 0){
                return  valueCompare * -1 ;
            } else {
                // si riferiscono anche stesso prezzo
                int dateCompare = this.date.compareTo(o.date);
                return dateCompare;
            }
        }   
    }
   
   
   
}

Nessun commento:

Posta un commento