KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > algorithms > Sort


1 // ============================================================================
2
// $Id: Sort.java,v 1.6 2006/12/16 16:48:58 davidahall Exp $
3
// Copyright (c) 2006 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.algorithms;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.Comparator JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 import static net.sf.jga.util.ArrayUtils.*;
42 import static net.sf.jga.util.CollectionUtils.*;
43
44 /**
45  * Adapts standard java Sort capabilities to the interfaces common to jga.
46  * <p>
47  * Copyright &copy; 2006 David A. Hall
48  */

49
50 public class Sort {
51
52     // ============
53
// Arrays
54
// ============
55

56     /**
57      * Returns an iterable object over the sorted contents of the array.
58      * The array will not be modified.
59      */

60     static public <T extends Comparable JavaDoc<? super T>> Iterable JavaDoc<T> sort(T[] ts) {
61         return sort(iterable(ts));
62     }
63
64     /**
65      * Returns an iterable object over the sorted contents of the array, using the given comparator
66      * to determine ordering. The array itself will not be modified. .
67      */

68     static public <T> Iterable JavaDoc<T> sort(T[] ts, Comparator JavaDoc<? super T> comp) {
69         // NOTE: it might be faster to clone the array and use ArrayUtils.sort, but we'll need to
70
// check that cloning the array can be done in a typesafe manner.
71
return sort(iterable(ts), comp);
72     }
73     
74     // ============
75
// Iterables
76
// ============
77

78     /**
79      * Returns an iterable object over the sorted contents of the input. The input itself
80      * will not be modified.
81      */

82     static public <T extends Comparable JavaDoc<? super T>> Iterable JavaDoc<T> sort(Iterable JavaDoc<T> i) {
83         ArrayList JavaDoc<T> list = append(new ArrayList JavaDoc<T>(), i.iterator());
84         Collections.sort(list);
85         return list;
86     }
87
88     
89     /**
90      * Returns an iterable object over the sorted contents of the input, using the given comparator
91      * to determine ordering. The input will not be modified.
92      */

93     static public <T> Iterable JavaDoc<T> sort(Iterable JavaDoc<T> i, Comparator JavaDoc<? super T> comp) {
94         ArrayList JavaDoc<T> list = append(new ArrayList JavaDoc<T>(), i.iterator());
95         Collections.sort(list, comp);
96         return list;
97     }
98
99     // ============
100
// Iterators
101
// ============
102

103     /**
104      * Returns an iterator object over the sorted contents of the input. The input itself
105      * will not be modified.
106      */

107     static public <T extends Comparable JavaDoc<? super T>> Iterator JavaDoc<T> sort(Iterator JavaDoc<T> i) {
108         ArrayList JavaDoc<T> list = append(new ArrayList JavaDoc<T>(), i);
109         Collections.sort(list);
110         return list.iterator();
111     }
112
113
114     /**
115      * Returns an iterator object over the sorted contents of the input, using the given comparator
116      * to determine ordering. The input will not be modified.
117      */

118     static public <T> Iterator JavaDoc<T> sort(Iterator JavaDoc<T> i, Comparator JavaDoc<? super T> comp) {
119         ArrayList JavaDoc<T> list = append(new ArrayList JavaDoc<T>(), i);
120         Collections.sort(list, comp);
121         return list.iterator();
122     }
123     
124     // ============
125
// IterableCopy
126
// ============
127

128     /**
129      * Appends the sorted contents of the input to the output. The input will not be modified.
130      */

131     static public <T extends Comparable JavaDoc<? super T>, TCollection extends Collection JavaDoc<? super T>>
132     TCollection sort(Iterable JavaDoc<? extends T> i, TCollection co)
133     {
134         ArrayList JavaDoc<T> list = append(new ArrayList JavaDoc<T>(), i.iterator());
135         Collections.sort(list);
136         co.addAll(list);
137         return co;
138     }
139
140     
141     /**
142      * Appends the sorted contents of the input to the output, using the given comparator
143      * to determine ordering. The input will not be modified.
144      */

145     static public <T, TCollection extends Collection JavaDoc<? super T>> TCollection
146     sort(Iterable JavaDoc<T> i, Comparator JavaDoc<? super T> comp, TCollection co) {
147         ArrayList JavaDoc<T> list = append(new ArrayList JavaDoc<T>(), i.iterator());
148         Collections.sort(list, comp);
149         co.addAll(list);
150         return co;
151     }
152
153 }
154
Popular Tags