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

org.minidns.util.MultipleIoException Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/*
 * Copyright 2015-2018 the original author or authors
 *
 * This software is licensed under the Apache License, Version 2.0,
 * the GNU Lesser General Public License version 2 or later ("LGPL")
 * and the WTFPL.
 * You may choose either license to govern your use of this software only
 * upon the condition that you accept all of the terms of either
 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
 */
package org.minidns.util;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MultipleIoException extends IOException {

    /**
     * 
     */
    private static final long serialVersionUID = -5932211337552319515L;

    private final List ioExceptions;

    private MultipleIoException(List ioExceptions) {
        super(getMessage(ioExceptions));
        assert(!ioExceptions.isEmpty());
        this.ioExceptions = Collections.unmodifiableList(ioExceptions);
    }

    public List getExceptions() {
        return ioExceptions;
    }

    private static String getMessage(Collection exceptions) {
        StringBuilder sb = new StringBuilder();
        Iterator it = exceptions.iterator();
        while (it.hasNext()) {
            sb.append(it.next().getMessage());
            if (it.hasNext()) {
                sb.append(", ");
            }
        }
        return sb.toString();
    }

    public static void throwIfRequired(List ioExceptions) throws IOException {
        if (ioExceptions == null || ioExceptions.isEmpty()) {
            return;
        }
        if (ioExceptions.size() == 1) {
            throw ioExceptions.get(0);
        }
        throw new MultipleIoException(ioExceptions);
    }

    public static IOException toIOException(List ioExceptions) {
        int size = ioExceptions.size();
        if (size == 1) {
            return ioExceptions.get(0);
        } else if (size > 1) {
            return new MultipleIoException(ioExceptions);
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy