All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.protelis.test.observer.SimpleExceptionObserver Maven / Gradle / Ivy

There is a newer version: 17.7.1
Show newest version
/*
 * Copyright (C) 2021, Danilo Pianini and contributors listed in the project's build.gradle.kts or pom.xml file.
 *
 * This file is part of Protelis, and is distributed under the terms of the GNU General Public License,
 * with a linking exception, as described in the file LICENSE.txt in this project's top directory.
 */

package org.protelis.test.observer;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

/**
 * Simple exception observer.
 */
public final class SimpleExceptionObserver implements ExceptionObserver {
    private final List exceptions = new LinkedList<>();

    @Override
    public Exception exceptionThrown(final Exception ex) {
        exceptions.add(ex);
        return ex;
    }

    @Override
    public List getExceptionList() {
        return Collections.unmodifiableList(exceptions);
    }

    @Override
    public Optional getLastException() {
        try {
            return Optional.of(((LinkedList) exceptions).getLast());
        } catch (NoSuchElementException e) {
            return Optional.empty();
        }
    }

    @Override
    public Optional getFirstException() {
        try {
            return Optional.of(((LinkedList) exceptions).getFirst());
        } catch (NoSuchElementException e) {
            return Optional.empty();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy