/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package test.dbc.java;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 *
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.1.8.3 $
 * @@org.jboss.aspects.dbc.Dbc
*/
public class Sorter
{
   /**
    * Returns the original array with all the elements sorted incrementally
    * @@org.jboss.aspects.dbc.PostCond ({"$rtn.length == $0.length", "java: for (int i = 0 ; i < $rtn.length ; i++){if (i > 0){if ($rtn[i] < $rtn[i - 1])return false;}}return true;"})
    */
   public static int[] sort(int[] unsorted)
   {
      //Not the most efficient sorting algorithm in the world, but hey
      ArrayList list = new ArrayList();
      
      for (int i = 0 ; i < unsorted.length ; i++)
      {
         list.add(new Integer(unsorted[i]));
      }
      
      Collections.sort(list, 
            new Comparator()
            {
         		public boolean equals(Object obj)
         		{
         		   return false;
         		}

         		public int compare(Object o1, Object o2)
         		{
         		   int i1 = ((Integer)o1).intValue();
         		   int i2 = ((Integer)o2).intValue();
         		   
         		   if (i1 < i2)return -1;
         		   if (i1 == i2)return 0;
         		   return 1;
         		}
            });
      
      int[] sorted = new int[unsorted.length];
      for (int i = 0 ; i < list.size() ; i++)
      {
         sorted[i] = ((Integer)list.get(i)).intValue();
      }
      return sorted;
   }
   
   /**
    * This will break the post condition
    * @@org.jboss.aspects.dbc.PostCond ({"java: for (int i = 0 ; i < $rtn.length ; i++){if (i > 0){if ($rtn[i] < $rtn[i - 1])return false;}}return true;"})
    */
   public static int[] brokenSort(int[] unsorted)
   {
      return unsorted;
   }
}
