/** * Modifies this array so that its elements are in sorted order. * The array items are assumed to be comparable. * * @param self the array to be sorted * @return the sorted array * @since 1.5.5 */ public static <T> T[] sort(T[] self) { Arrays.sort(self, new NumberAwareComparator<T>()); return self; }
/** * Modifies this collection by removing its elements that are contained * within the specified object array. * * See also <code>findAll</code> and <code>grep</code> when wanting to produce a new list * containing items which don't match some criteria while leaving the original collection unchanged. * * @param self a Collection to be modified * @param items array containing elements to be removed from this collection * @return <tt>true</tt> if this collection changed as a result of the call * @see Collection#removeAll(Collection) * @since 1.7.2 */ public static boolean removeAll(Collection self, Object[] items) { Collection pickFrom = new TreeSet(new NumberAwareComparator()); pickFrom.addAll(Arrays.asList(items)); return self.removeAll(pickFrom); }
/** * Modifies this collection so that it retains only its elements that are contained * in the specified array. In other words, removes from this collection all of * its elements that are not contained in the specified array. * * See also <code>grep</code> and <code>findAll</code> when wanting to produce a new list * containing items which match some specified items but leaving the original collection unchanged. * * @param self a Collection to be modified * @param items array containing elements to be retained from this collection * @return <tt>true</tt> if this collection changed as a result of the call * @see Collection#retainAll(Collection) * @since 1.7.2 */ public static boolean retainAll(Collection self, Object[] items) { Collection pickFrom = new TreeSet(new NumberAwareComparator()); pickFrom.addAll(Arrays.asList(items)); return self.retainAll(pickFrom); }
/** * Returns a sorted version of the given array using the supplied comparator. * * @param self the array to be sorted * @return the sorted array * @see #toSorted(Object[], Comparator) * @since 2.4.0 */ public static <T> T[] toSorted(T[] self) { return toSorted(self, new NumberAwareComparator<T>()); }
/** * Sorts the Iterator. Assumes that the Iterator elements are * comparable and uses a {@link NumberAwareComparator} to determine the resulting order. * {@code NumberAwareComparator} has special treatment for numbers but otherwise uses the * natural ordering of the Iterator elements. * A new iterator is produced that traverses the items in sorted order. * * @param self the Iterator to be sorted * @return the sorted items as an Iterator * @see #toSorted(Iterator, Comparator) * @since 2.4.0 */ public static <T> Iterator<T> toSorted(Iterator<T> self) { return toSorted(self, new NumberAwareComparator<T>()); }
/** * Create a Collection composed of the intersection of both collections. Any * elements that exist in both collections are added to the resultant collection. * For collections of custom objects; the objects should implement java.lang.Comparable * <pre class="groovyTestCase">assert [4,5] == [1,2,3,4,5].intersect([4,5,6,7,8])</pre> * By default, Groovy uses a {@link NumberAwareComparator} when determining if an * element exists in both collections. * * @param left a Collection * @param right a Collection * @return a Collection as an intersection of both collections * @see #intersect(Collection, Collection, Comparator) * @since 1.5.6 */ public static <T> Collection<T> intersect(Collection<T> left, Collection<T> right) { return intersect(left, right, new NumberAwareComparator<T>()); }
/** * Sorts the Iterable. Assumes that the Iterable elements are * comparable and uses a {@link NumberAwareComparator} to determine the resulting order. * {@code NumberAwareComparator} has special treatment for numbers but otherwise uses the * natural ordering of the Iterable elements. The elements are first placed into a new list which * is then sorted and returned - leaving the original Iterable unchanged. * <pre class="groovyTestCase"> * def orig = [1, 3, 2] * def sorted = orig.toSorted() * assert orig == [1, 3, 2] * assert sorted == [1, 2, 3] * </pre> * * @param self the Iterable to be sorted * @return the sorted iterable as a List * @see #toSorted(Iterable, Comparator) * @since 2.4.0 */ public static <T> List<T> toSorted(Iterable<T> self) { return toSorted(self, new NumberAwareComparator<T>()); }
/** * Sorts the given array into sorted order. * The array items are assumed to be comparable. * If mutate is true, the array is sorted in place and returned. Otherwise, a new sorted * array is returned and the original array remains unchanged. * <pre class="groovyTestCase"> * def orig = ["hello","hi","Hey"] as String[] * def sorted = orig.sort(false) * assert orig == ["hello","hi","Hey"] as String[] * assert sorted == ["Hey","hello","hi"] as String[] * orig.sort(true) * assert orig == ["Hey","hello","hi"] as String[] * </pre> * * @param self the array to be sorted * @param mutate false will always cause a new array to be created, true will mutate the array in place * @return the sorted array * @since 1.8.1 */ public static <T> T[] sort(T[] self, boolean mutate) { T[] answer = mutate ? self : self.clone(); Arrays.sort(answer, new NumberAwareComparator<T>()); return answer; }
/** * Returns <code>true</code> if the intersection of two iterables is empty. * <pre class="groovyTestCase">assert [1,2,3].disjoint([3,4,5]) == false</pre> * <pre class="groovyTestCase">assert [1,2].disjoint([3,4]) == true</pre> * * @param left an Iterable * @param right an Iterable * @return boolean <code>true</code> if the intersection of two iterables * is empty, <code>false</code> otherwise. * @since 2.4.0 */ public static boolean disjoint(Iterable left, Iterable right) { Collection leftCol = asCollection(left); Collection rightCol = asCollection(right); if (leftCol.isEmpty() || rightCol.isEmpty()) return true; Collection pickFrom = new TreeSet(new NumberAwareComparator()); pickFrom.addAll(rightCol); for (final Object o : leftCol) { if (pickFrom.contains(o)) return false; } return true; }
/** * Sorts the Iterable. Assumes that the Iterable items are * comparable and uses their natural ordering to determine the resulting order. * If the Iterable is a List and mutate is true, * it is sorted in place and returned. Otherwise, the elements are first placed * into a new list which is then sorted and returned - leaving the original Iterable unchanged. * <pre class="groovyTestCase">assert [1,2,3] == [3,1,2].sort()</pre> * <pre class="groovyTestCase"> * def orig = [1, 3, 2] * def sorted = orig.sort(false) * assert orig == [1, 3, 2] * assert sorted == [1, 2, 3] * </pre> * * @param self the iterable to be sorted * @param mutate false will always cause a new list to be created, true will mutate lists in place * @return the sorted iterable as a List * @since 2.2.0 */ public static <T> List<T> sort(Iterable<T> self, boolean mutate) { List<T> answer = mutate ? asList(self) : toList(self); Collections.sort(answer, new NumberAwareComparator<T>()); return answer; }
Comparator<T> numberComparator = new NumberAwareComparator<T>();
private static final NumberAwareComparator<Comparable> COMPARABLE_NUMBER_AWARE_COMPARATOR = new NumberAwareComparator<Comparable>();
/** * Modifies this array so that its elements are in sorted order. * The array items are assumed to be comparable. * * @param self the array to be sorted * @return the sorted array * @since 1.5.5 */ public static <T> T[] sort(T[] self) { Arrays.sort(self, new NumberAwareComparator<T>()); return self; }
/** * Modifies this collection by removing its elements that are contained * within the specified object array. * * See also <code>findAll</code> and <code>grep</code> when wanting to produce a new list * containing items which don't match some criteria while leaving the original collection unchanged. * * @param self a Collection to be modified * @param items array containing elements to be removed from this collection * @return <tt>true</tt> if this collection changed as a result of the call * @see Collection#removeAll(Collection) * @since 1.7.2 */ public static boolean removeAll(Collection self, Object[] items) { Collection pickFrom = new TreeSet(new NumberAwareComparator()); pickFrom.addAll(Arrays.asList(items)); return self.removeAll(pickFrom); }
/** * Modifies this collection so that it retains only its elements that are contained * in the specified array. In other words, removes from this collection all of * its elements that are not contained in the specified array. * * See also <code>grep</code> and <code>findAll</code> when wanting to produce a new list * containing items which match some specified items but leaving the original collection unchanged. * * @param self a Collection to be modified * @param items array containing elements to be retained from this collection * @return <tt>true</tt> if this collection changed as a result of the call * @see Collection#retainAll(Collection) * @since 1.7.2 */ public static boolean retainAll(Collection self, Object[] items) { Collection pickFrom = new TreeSet(new NumberAwareComparator()); pickFrom.addAll(Arrays.asList(items)); return self.retainAll(pickFrom); }
/** * Returns <code>true</code> if the intersection of two collections is empty. * <pre class="groovyTestCase">assert [1,2,3].disjoint([3,4,5]) == false</pre> * <pre class="groovyTestCase">assert [1,2].disjoint([3,4]) == true</pre> * * @param left a Collection * @param right a Collection * @return boolean <code>true</code> if the intersection of two collections * is empty, <code>false</code> otherwise. * @since 1.0 */ public static boolean disjoint(Collection left, Collection right) { if (left.isEmpty() || right.isEmpty()) return true; Collection pickFrom = new TreeSet(new NumberAwareComparator()); pickFrom.addAll(right); for (final Object o : left) { if (pickFrom.contains(o)) return false; } return true; }
/** * Provides a method that compares two comparables using Groovy's * default number aware comparator. * * @param self a Comparable * @param other another Comparable * @return a -ve number, 0 or a +ve number according to Groovy's compareTo contract * @since 1.6.0 */ public static int numberAwareCompareTo(Comparable self, Comparable other) { NumberAwareComparator<Comparable> numberAwareComparator = new NumberAwareComparator<Comparable>(); return numberAwareComparator.compare(self, other); }
/** * Sorts the given array into sorted order. * The array items are assumed to be comparable. * If mutate is true, the array is sorted in place and returned. Otherwise, a new sorted * array is returned and the original array remains unchanged. * <pre class="groovyTestCase"> * def orig = ["hello","hi","Hey"] as String[] * def sorted = orig.sort(false) * assert orig == ["hello","hi","Hey"] as String[] * assert sorted == ["Hey","hello","hi"] as String[] * orig.sort(true) * assert orig == ["Hey","hello","hi"] as String[] * </pre> * * @param self the array to be sorted * @param mutate false will always cause a new array to be created, true will mutate the array in place * @return the sorted array * @since 1.8.1 */ public static <T> T[] sort(T[] self, boolean mutate) { T[] answer = mutate ? self : self.clone(); Arrays.sort(answer, new NumberAwareComparator<T>()); return answer; }
/** * Create a Collection composed of the intersection of both collections. Any * elements that exist in both collections are added to the resultant collection. * <pre class="groovyTestCase">assert [4,5] == [1,2,3,4,5].intersect([4,5,6,7,8])</pre> * * @param left a Collection * @param right a Collection * @return a Collection as an intersection of both collections * @since 1.5.6 */ public static <T> Collection<T> intersect(Collection<T> left, Collection<T> right) { if (left.isEmpty()) return createSimilarCollection(left, 0); if (left.size() < right.size()) { Collection<T> swaptemp = left; left = right; right = swaptemp; } // TODO optimise if same type? // boolean nlgnSort = sameType(new Collection[]{left, right}); Collection<T> result = createSimilarCollection(left, left.size()); //creates the collection to look for values. Collection<T> pickFrom = new TreeSet<T>(new NumberAwareComparator<T>()); pickFrom.addAll(left); for (final T t : right) { if (pickFrom.contains(t)) result.add(t); } return result; }
/** * Sorts the Collection. Assumes that the collection items are * comparable and uses their natural ordering to determine the resulting order. * If the Collection is a List and mutate is true, * it is sorted in place and returned. Otherwise, the elements are first placed * into a new list which is then sorted and returned - leaving the original Collection unchanged. * <pre class="groovyTestCase">assert [1,2,3] == [3,1,2].sort()</pre> * <pre class="groovyTestCase"> * def orig = [1, 3, 2] * def sorted = orig.sort(false) * assert orig == [1, 3, 2] * assert sorted == [1, 2, 3] * </pre> * * @param self the collection to be sorted * @param mutate false will always cause a new list to be created, true will mutate lists in place * @return the sorted collection as a List * @since 1.8.1 */ public static <T> List<T> sort(Collection<T> self, boolean mutate) { List<T> answer = mutate ? asList(self) : toList(self); Collections.sort(answer, new NumberAwareComparator<T>()); return answer; }