@Override
    protected Object doTaskImpl(TaskMonitor monitor, ObjectRepository repository) {
        InstanceStream stream = (InstanceStream) getPreparedClassOption(this.streamOption);
        Instances cache = new Instances(stream.getHeader(), 0);
        monitor.setCurrentActivity("Caching instances...", -1.0);
        while ((cache.numInstances() < this.maximumCacheSizeOption.getValue())
                && stream.hasMoreInstances()) {
            cache.add(stream.nextInstance());
            if (cache.numInstances()
                    % MainTask.INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
                if (monitor.taskShouldAbort()) {
                    return null;
                }
                long estimatedRemainingInstances = stream
                        .estimatedRemainingInstances();
                long maxRemaining = this.maximumCacheSizeOption.getValue()
                        - cache.numInstances();
                if ((estimatedRemainingInstances < 0)
                        || (maxRemaining < estimatedRemainingInstances)) {
                    estimatedRemainingInstances = maxRemaining;
                }
                monitor
                        .setCurrentActivityFractionComplete(estimatedRemainingInstances < 0 ? -1.0
                                : (double) cache.numInstances()
                                / (double) (cache.numInstances() + estimatedRemainingInstances));
            }
        }
        monitor.setCurrentActivity("Shuffling instances...", -1.0);
        cache.randomize(new Random(this.shuffleRandomSeedOption.getValue()));
        return new CachedInstancesStream(cache);
    }