blob: 93ac401f0ae24b7877a85a3a41aa59cd1c7cfe63 [file] [log] [blame]
/*******************************************************************************
* 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);
}
}