@SuppressWarnings({"unchecked", "rawtypes"}) protected void spill() { // Make sure we have something to spill. if (memory.size() > 0) { OutputStream out; try { out = getSpillStream(); } catch (IOException e) { throw new AtlasException(e); } // Sort the tuples // Collections.sort() will copy to an array, sort, and then copy back. Avoid that // extra copy by copying to an array and using Arrays.sort(). Also it lets us use // Collection<E> instead of List<E> as the type for the memory object. Unfortunately // because of Java's crazy generics we have to do it as an Object array. Object[] array = memory.toArray(); Arrays.sort(array, (Comparator) comparator); Sink<E> serializer = serializationFactory.createSerializer(out); try { for (Object tuple : array) { serializer.send((E) tuple); } } finally { serializer.close(); } spilled = true; policy.reset(); memory.clear(); } }