01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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;
25: import java.util.Collections;
26: import java.util.Comparator;
27:
28: /**
29: *
30: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
31: * @version $Revision: 57186 $
32: * @@org.jboss.aspects.dbc.Dbc
33: */
34: public class Sorter {
35: /**
36: * Returns the original array with all the elements sorted incrementally
37: * @@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;"})
38: */
39: public static int[] sort(int[] unsorted) {
40: //Not the most efficient sorting algorithm in the world, but hey
41: ArrayList list = new ArrayList();
42:
43: for (int i = 0; i < unsorted.length; i++) {
44: list.add(new Integer(unsorted[i]));
45: }
46:
47: Collections.sort(list, new Comparator() {
48: public boolean equals(Object obj) {
49: return false;
50: }
51:
52: public int compare(Object o1, Object o2) {
53: int i1 = ((Integer) o1).intValue();
54: int i2 = ((Integer) o2).intValue();
55:
56: if (i1 < i2)
57: return -1;
58: if (i1 == i2)
59: return 0;
60: return 1;
61: }
62: });
63:
64: int[] sorted = new int[unsorted.length];
65: for (int i = 0; i < list.size(); i++) {
66: sorted[i] = ((Integer) list.get(i)).intValue();
67: }
68: return sorted;
69: }
70:
71: /**
72: * This will break the post condition
73: * @@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;"})
74: */
75: public static int[] brokenSort(int[] unsorted) {
76: return unsorted;
77: }
78: }
|