KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dbc > java > Sorter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package test.dbc.java;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27
28 /**
29  *
30  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
31  * @version $Revision: 37406 $
32  * @@org.jboss.aspects.dbc.Dbc
33 */

34 public class Sorter
35 {
36    /**
37     * Returns the original array with all the elements sorted incrementally
38     * @@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;"})
39     */

40    public static int[] sort(int[] unsorted)
41    {
42       //Not the most efficient sorting algorithm in the world, but hey
43
ArrayList JavaDoc list = new ArrayList JavaDoc();
44       
45       for (int i = 0 ; i < unsorted.length ; i++)
46       {
47          list.add(new Integer JavaDoc(unsorted[i]));
48       }
49       
50       Collections.sort(list,
51             new Comparator JavaDoc()
52             {
53                 public boolean equals(Object JavaDoc obj)
54                 {
55                    return false;
56                 }
57
58                 public int compare(Object JavaDoc o1, Object JavaDoc o2)
59                 {
60                    int i1 = ((Integer JavaDoc)o1).intValue();
61                    int i2 = ((Integer JavaDoc)o2).intValue();
62                    
63                    if (i1 < i2)return -1;
64                    if (i1 == i2)return 0;
65                    return 1;
66                 }
67             });
68       
69       int[] sorted = new int[unsorted.length];
70       for (int i = 0 ; i < list.size() ; i++)
71       {
72          sorted[i] = ((Integer JavaDoc)list.get(i)).intValue();
73       }
74       return sorted;
75    }
76    
77    /**
78     * This will break the post condition
79     * @@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;"})
80     */

81    public static int[] brokenSort(int[] unsorted)
82    {
83       return unsorted;
84    }
85 }
86
Popular Tags