refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / BaseOperation.java
blob481bf4a1effac1ba4fd8926fc0009c62062878fd
1 /******************************************************************************
2 * Copyright (c) 2012 GitHub Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
10 * Contributors:
11 * Kevin Sawicki (GitHub Inc.) - initial API and implementation
12 * Stephan Hackstedt <stephan.hackstedt@googlemail.com> - Bug 477695
13 *****************************************************************************/
14 package org.eclipse.egit.core.op;
16 import java.util.ArrayList;
17 import java.util.Collection;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.eclipse.jgit.lib.Repository;
24 /**
25 * Base operation that supports adding pre/post tasks
27 abstract class BaseOperation implements IEGitOperation {
29 protected final Repository repository;
31 protected Collection<PreExecuteTask> preTasks;
33 protected Collection<PostExecuteTask> postTasks;
35 BaseOperation(final Repository repository) {
36 this.repository = repository;
39 /**
40 * Invoke all pre-execute tasks
42 * @param monitor
43 * @throws CoreException
45 protected void preExecute(IProgressMonitor monitor) throws CoreException {
46 synchronized (this) {
47 if (preTasks != null) {
48 SubMonitor progress = SubMonitor.convert(monitor,
49 preTasks.size());
50 for (PreExecuteTask task : preTasks)
51 task.preExecute(repository, progress.newChild(1));
56 /**
57 * Invoke all post-execute tasks
59 * @param monitor
60 * @throws CoreException
62 protected void postExecute(IProgressMonitor monitor) throws CoreException {
63 synchronized (this) {
64 if (postTasks != null) {
65 SubMonitor progress = SubMonitor.convert(monitor,
66 postTasks.size());
67 for (PostExecuteTask task : postTasks)
68 task.postExecute(repository, progress.newChild(1));
73 /**
74 * @param task
75 * to be performed before execution
77 public synchronized void addPreExecuteTask(final PreExecuteTask task) {
78 if (preTasks == null)
79 preTasks = new ArrayList<PreExecuteTask>();
80 preTasks.add(task);
83 /**
84 * @param task
85 * to be performed after execution
87 public synchronized void addPostExecuteTask(PostExecuteTask task) {
88 if (postTasks == null)
89 postTasks = new ArrayList<PostExecuteTask>();
90 postTasks.add(task);