Use generic to prevent duplicate code in this case I have this class hierarchy. I want to apply generics to it but don't know how to do.
AddWikiTask, ScanTask and RescanTask are subclasses of AbstractWorkerTask.
DictionaryScanner has 3 functions to check if all tasks finish. How to prevent duplicate code in 3 those functions?

This is class hierarchy

    public abstract class AbstractWorkerTask&lt;Params, Progress, Result&gt;
    extends AsyncTask&lt;Params, Progress, Result&gt;
    implements Workable {
    ...}

    class AddWikiTask extends AbstractWorkerTask&lt;String, Void, Pair&lt;Integer, Dictionary&gt;&gt; {
    ...}

    class ScanTask extends AbstractWorkerTask&lt;DictionaryBean, Void, Pair&lt;Integer, Dictionary&gt;&gt; {
    ...}

    class RescanTask extends AbstractWorkerTask&lt;DictionaryInformation, Void, Pair&lt;Integer, Dictionary&gt;&gt; {
    ...}


This is DictionaryScanner.      

public final class DictionaryScanner { 
    private final List&lt;RescanTask&gt; rescanTasks = new ArrayList&lt;RescanTask&gt;();
    private final List&lt;ScanTask&gt; scanTasks = new ArrayList&lt;ScanTask&gt;();
    private final List&lt;AddWikiTask&gt; wikiTasks = new ArrayList&lt;AddWikiTask&gt;();

        public boolean didAllRescanTasksFinish() {
            for (final RescanTask task : rescanTasks) {
                if (task.isWorking()) {
                    return false;
                }
            }
            return true;
        }

        public boolean didAllAddWikiTasksFinish() {
            for (final AddWikiTask task : wikiTasks) {
                if (task.isWorking()) {
                    return false;
                }
            }
            return true;
        }

        public boolean didAllScanTasksFinish() {
            for (final ScanTask task : scanTasks) {
                if (task.isWorking()) {
                    return false;
                }
            }
            return true;
        }
}


Edit: And apply generic for this. It's too long. I really don't know how to refactor it.

private void setOnPreExecuteForAddWikiTask(final AddWikiTask task, final ProgressDialog progressDialog) {
    task.setOnPreExecuteListener(new OnPreExecuteListener() {
        @Override
        public void onPreExecute() {
            if (didAllAddWikiTasksFinish()) {
                progressDialog.show();
            }
        }
    });
}

private void setOnPreExecuteForRescanTask(final RescanTask task, final ProgressDialog progressDialog) {
    task.setOnPreExecuteListener(new OnPreExecuteListener() {
        @Override
        public void onPreExecute() {
            if (didAllRescanTasksFinish()) {
                progressDialog.show();
            }
        }
    });
}

private void setOnPreExecuteForScanTask(final ScanTask task) {
    task.setOnPreExecuteListener(new OnPreExecuteListener() {
        @Override
        public void onPreExecute() {
            if (didAllScanTasksFinish() &amp;&amp; progressBar != null) {
                progressBar.setVisibility(ProgressBar.VISIBLE);
            }
        }
    });
}


If the snippet is not elaborate enough, please let me know.
You can take the common code in the functions in DictionaryScanner out into a common method:

    public boolean didAllRescanTasksFinish() {
        didAllXTasksFinish(rescanTasks);
    }

    private boolean didAllXTasksFinish(List&lt;AbstractWorkerTask&gt; tasks) {
        for (final AbstractWorkerTask task : tasks) {
            if (task.isWorking()) {
                return false;
            }
        }
        return true;
    }


Otherwise your second block of code is about as simple as you can get, because each listener has different content. If you try to remove the boilerplate you will find it gets much more difficult to understand.