001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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.apache.commons.imaging;
018
019import java.io.IOException;
020
021/**
022 * The root class for implementing custom exceptions in the Apache Commons Imaging component.
023 */
024public class ImagingException extends IOException {
025
026    private static final long serialVersionUID = -1L;
027
028    static String getType(final Object value) {
029        if (value == null) {
030            return "null";
031        }
032        if (value instanceof Object[]) {
033            return "[Object[]: " + ((Object[]) value).length + "]";
034        }
035        if (value instanceof char[]) {
036            return "[char[]: " + ((char[]) value).length + "]";
037        }
038        if (value instanceof byte[]) {
039            return "[byte[]: " + ((byte[]) value).length + "]";
040        }
041        if (value instanceof short[]) {
042            return "[short[]: " + ((short[]) value).length + "]";
043        }
044        if (value instanceof int[]) {
045            return "[int[]: " + ((int[]) value).length + "]";
046        }
047        if (value instanceof long[]) {
048            return "[long[]: " + ((long[]) value).length + "]";
049        }
050        if (value instanceof float[]) {
051            return "[float[]: " + ((float[]) value).length + "]";
052        }
053        if (value instanceof double[]) {
054            return "[double[]: " + ((double[]) value).length + "]";
055        }
056        if (value instanceof boolean[]) {
057            return "[boolean[]: " + ((boolean[]) value).length + "]";
058        }
059        return value.getClass().getName();
060    }
061
062    /**
063     * Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently be initialized by a call to
064     * {@link #initCause}.
065     *
066     * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method.
067     */
068    public ImagingException(final String message) {
069        super(message);
070    }
071
072    /**
073     * Constructs a new exception with the specified detail message and cause.
074     * <p>
075     * Note that the detail message associated with {@code cause} is <em>not</em> automatically incorporated in this exception's detail message.
076     * </p>
077     *
078     * @param message the detail message (which is saved for later retrieval by the {@link #getMessage()} method).
079     * @param data    data typed to build the message.
080     */
081    public ImagingException(final String message, final Object data) {
082        super(message + ": " + data + " (" + getType(data) + ")");
083    }
084
085    /**
086     * Constructs a new exception with the specified detail message and cause.
087     * <p>
088     * Note that the detail message associated with {@code cause} is <em>not</em> automatically incorporated in this exception's detail message.
089     * </p>
090     *
091     * @param message the detail message (which is saved for later retrieval by the {@link #getMessage()} method).
092     * @param cause   the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that
093     *                the cause is nonexistent or unknown.)
094     */
095    public ImagingException(final String message, final Throwable cause) {
096        super(message, cause);
097    }
098
099    /**
100     * Constructs a new exception.
101     *
102     * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that the
103     *              cause is nonexistent or unknown.)
104     */
105    public ImagingException(final Throwable cause) {
106        super(cause);
107    }
108}