Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 93ac401f0ae24b7877a85a3a41aa59cd1c7cfe63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*******************************************************************************
 * Copyright (c) 2012 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal;

import java.util.Vector;
import org.eclipse.jpt.common.utility.ExceptionHandler;
import org.eclipse.jpt.common.utility.internal.iterables.LiveCloneIterable;
import org.eclipse.jpt.common.utility.internal.iterables.SnapshotCloneIterable;

/**
 * An exception handler that collects and hold the exceptions handed to it.
 * They can be retrieved at a later time via calls to {@link #getExceptions()}
 * and {{@link #clearExceptions()}.
 */
public class CollectingExceptionHandler
	implements ExceptionHandler
{
	private final Vector<Throwable> exceptions = new Vector<Throwable>();

	public void handleException(Throwable t) {
		this.exceptions.add(t);
	}

	/**
	 * Return the current list of exceptions handled by the handler so far.
	 */
	public Iterable<Throwable> getExceptions() {
		return new LiveCloneIterable<Throwable>(this.exceptions);
	}

	/**
	 * Clear and return the current list of exceptions handled by the handler
	 * so far.
	 */
	public Iterable<Throwable> clearExceptions() {
		synchronized (this.exceptions) {
			Iterable<Throwable> result = new SnapshotCloneIterable<Throwable>(this.exceptions);
			this.exceptions.clear();
			return result;
		}
	}

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.exceptions);
	}
}

Back to the top