/*
 * $Id: DocumentSortFactory.java,v 1.3 2004/07/20 08:41:36 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: DocumentSortFactory.java,v $
 * Revision 1.3  2004/07/20 08:41:36  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.util.sort;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import vkc.db.entities.VKCDocument;

public class DocumentSortFactory {

    public static final int SORT_BY_PUBLISH_DATE_UP   = 1;
    public static final int SORT_BY_PUBLISH_DATE_DOWN = 2;
    
    private DocumentSortFactory() {
    }
    
    public final static void sort( List l, int mode ){

        if ( l==null ) {
            return;
        }

        switch( mode ) {        

            case SORT_BY_PUBLISH_DATE_UP:
                Collections.sort( l, COMP_PUBLISH_DATE_UP );
                break;

            case SORT_BY_PUBLISH_DATE_DOWN:
                Collections.sort( l, COMP_PUBLISH_DATE_DOWN );
                break;
        }
    }

    private static final Comparator COMP_PUBLISH_DATE_UP = new Comparator(  ) {

        public int compare( Object o1, Object o2 )
        {
            Date d1 = ((VKCDocument)o1).getPublished();
            Date d2 = ((VKCDocument)o2).getPublished();
            
            return d1.compareTo( d2 );
        }
    };

    private static final Comparator COMP_PUBLISH_DATE_DOWN = new Comparator() {

        public int compare( Object o1, Object o2 )
        {
            Date d1 = ((VKCDocument)o1).getPublished();
            Date d2 = ((VKCDocument)o2).getPublished();
            
            return -d1.compareTo( d2 );
        }
    };
}