001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.error;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.conqat.lib.commons.collections.CollectionUtils;
023import org.conqat.lib.commons.collections.UnmodifiableList;
024
025/**
026 * An exception handler that collects exceptions for later use.
027 * 
028 * @author hummelb
029 */
030public class CollectingExceptionHandler<X extends Exception>
031                implements IExceptionHandler<X, NeverThrownRuntimeException> {
032
033        /** The exceptions stored. */
034        private final List<X> exceptions = new ArrayList<X>();
035
036        /** {@inheritDoc} */
037        @Override
038        public void handleException(X exception) throws NeverThrownRuntimeException {
039                exceptions.add(exception);
040        }
041
042        /** Returns the list of exception caught so far. */
043        public UnmodifiableList<X> getExceptions() {
044                return CollectionUtils.asUnmodifiable(exceptions);
045        }
046
047        /** Clears the list of exceptions. */
048        public void clearExceptions() {
049                exceptions.clear();
050        }
051}