001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.commons.compress;
021
022import java.io.IOException;
023import java.util.function.Supplier;
024
025import org.apache.commons.lang3.function.Suppliers;
026
027/**
028 * Signals that a Pack200 Compress exception of some sort has occurred.
029 *
030 * @since 1.28.0
031 */
032public class CompressException extends IOException {
033
034    /** Serial. */
035    private static final long serialVersionUID = 1;
036
037    /**
038     * Checks that the specified object reference is not {@code null} and throws a customized {@link CompressException} if it is. *
039     *
040     * @param <T>             The type of the reference.
041     * @param <E>             The type of the exception.
042     * @param cls             The exception class.
043     * @param obj             The object reference to check for nullity.
044     * @param messageSupplier supplier of the detail message to be used in the event that a {@code ArchiveException} is thrown
045     * @return {@code obj} if not {@code null}.
046     * @throws E if {@code obj} is {@code null}.
047     */
048    protected static <T, E extends Throwable> T requireNonNull(final Class<? super E> cls, final T obj, final Supplier<String> messageSupplier) throws E {
049        if (obj == null) {
050            try {
051                cls.getConstructor(String.class).newInstance(Suppliers.get(messageSupplier));
052            } catch (ReflectiveOperationException | SecurityException e) {
053                new CompressException(Suppliers.get(messageSupplier), e);
054            }
055        }
056        return obj;
057    }
058
059    /**
060     * Constructs an {@code CompressException} with {@code null} as its error detail message.
061     */
062    public CompressException() {
063        // empty
064    }
065
066    /**
067     * Constructs a new exception with the specified detail message. The cause is not initialized.
068     *
069     * @param message The message (which is saved for later retrieval by the {@link #getMessage()} method).
070     */
071    public CompressException(final String message) {
072        super(message);
073    }
074
075    /**
076     * Constructs a new exception with the specified detail message and cause.
077     *
078     * @param message The message (which is saved for later retrieval by the {@link #getMessage()} method).
079     * @param cause   The cause (which is saved for later retrieval by the {@link #getCause()} method). A null value indicates that the cause is nonexistent or
080     *                unknown.
081     */
082    public CompressException(final String message, final Throwable cause) {
083        super(message, cause);
084    }
085
086    /**
087     * Constructs a {@code CompressException} with the specified cause and a detail message.
088     *
089     * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method). (A null value is permitted, and indicates that the cause
090     *              is nonexistent or unknown.)
091     */
092    public CompressException(final Throwable cause) {
093        super(cause);
094    }
095}