001: // ============================================================================
002: // $Id: Sort.java,v 1.6 2006/12/16 16:48:58 davidahall Exp $
003: // Copyright (c) 2006 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.algorithms;
034:
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Collections;
038: import java.util.Comparator;
039: import java.util.Iterator;
040:
041: import static net.sf.jga.util.ArrayUtils.*;
042: import static net.sf.jga.util.CollectionUtils.*;
043:
044: /**
045: * Adapts standard java Sort capabilities to the interfaces common to jga.
046: * <p>
047: * Copyright © 2006 David A. Hall
048: */
049:
050: public class Sort {
051:
052: // ============
053: // Arrays
054: // ============
055:
056: /**
057: * Returns an iterable object over the sorted contents of the array.
058: * The array will not be modified.
059: */
060: static public <T extends Comparable<? super T>> Iterable<T> sort(
061: T[] ts) {
062: return sort(iterable(ts));
063: }
064:
065: /**
066: * Returns an iterable object over the sorted contents of the array, using the given comparator
067: * to determine ordering. The array itself will not be modified. .
068: */
069: static public <T> Iterable<T> sort(T[] ts,
070: Comparator<? super T> comp) {
071: // NOTE: it might be faster to clone the array and use ArrayUtils.sort, but we'll need to
072: // check that cloning the array can be done in a typesafe manner.
073: return sort(iterable(ts), comp);
074: }
075:
076: // ============
077: // Iterables
078: // ============
079:
080: /**
081: * Returns an iterable object over the sorted contents of the input. The input itself
082: * will not be modified.
083: */
084: static public <T extends Comparable<? super T>> Iterable<T> sort(
085: Iterable<T> i) {
086: ArrayList<T> list = append(new ArrayList<T>(), i.iterator());
087: Collections.sort(list);
088: return list;
089: }
090:
091: /**
092: * Returns an iterable object over the sorted contents of the input, using the given comparator
093: * to determine ordering. The input will not be modified.
094: */
095: static public <T> Iterable<T> sort(Iterable<T> i,
096: Comparator<? super T> comp) {
097: ArrayList<T> list = append(new ArrayList<T>(), i.iterator());
098: Collections.sort(list, comp);
099: return list;
100: }
101:
102: // ============
103: // Iterators
104: // ============
105:
106: /**
107: * Returns an iterator object over the sorted contents of the input. The input itself
108: * will not be modified.
109: */
110: static public <T extends Comparable<? super T>> Iterator<T> sort(
111: Iterator<T> i) {
112: ArrayList<T> list = append(new ArrayList<T>(), i);
113: Collections.sort(list);
114: return list.iterator();
115: }
116:
117: /**
118: * Returns an iterator object over the sorted contents of the input, using the given comparator
119: * to determine ordering. The input will not be modified.
120: */
121: static public <T> Iterator<T> sort(Iterator<T> i,
122: Comparator<? super T> comp) {
123: ArrayList<T> list = append(new ArrayList<T>(), i);
124: Collections.sort(list, comp);
125: return list.iterator();
126: }
127:
128: // ============
129: // IterableCopy
130: // ============
131:
132: /**
133: * Appends the sorted contents of the input to the output. The input will not be modified.
134: */
135: static public <T extends Comparable<? super T>, TCollection extends Collection<? super T>> TCollection sort(
136: Iterable<? extends T> i, TCollection co) {
137: ArrayList<T> list = append(new ArrayList<T>(), i.iterator());
138: Collections.sort(list);
139: co.addAll(list);
140: return co;
141: }
142:
143: /**
144: * Appends the sorted contents of the input to the output, using the given comparator
145: * to determine ordering. The input will not be modified.
146: */
147: static public <T, TCollection extends Collection<? super T>> TCollection sort(
148: Iterable<T> i, Comparator<? super T> comp, TCollection co) {
149: ArrayList<T> list = append(new ArrayList<T>(), i.iterator());
150: Collections.sort(list, comp);
151: co.addAll(list);
152: return co;
153: }
154:
155: }
|