    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 */
    017
    018package org.apache.commons.imaging.common;
    019
    020import java.math.BigInteger;
    021
    022import org.apache.commons.imaging.ImagingRuntimeException;
    023
    024/**
    025 * Thrown when an allocation request is too large.
    026 */
    027public class AllocationRequestException extends ImagingRuntimeException {
    028
    029    private static final long serialVersionUID = 1L;
    030
    031    private static String format(final int limit, final BigInteger request) {
    032        return String.format("Allocation limit %,d exceeded: %,d", limit, request);
    033    }
    034
    035    private final int limit;
    036
    037    private final BigInteger request;
    038
    039    /**
    040     * Constructs a new instance.
    041     *
    042     * @param limit   The allocation limit.
    043     * @param request The allocation request.
    044     */
    045    public AllocationRequestException(final int limit, final BigInteger request) {
    046        super(format(limit, request));
    047        this.limit = limit;
    048        this.request = request;
    049    }
    050
    051    /**
    052     * Constructs a new instance.
    053     *
    054     * @param limit     The allocation limit.
    055     * @param request   The allocation request.
    056     * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that
    057     *                  the cause is nonexistent or unknown.)
    058     */
    059    public AllocationRequestException(final int limit, final BigInteger request, final Throwable throwable) {
    060        super(format(limit, request), throwable);
    061        this.limit = limit;
    062        this.request = request;
    063    }
    064
    065    /**
    066     * Constructs a new instance.
    067     *
    068     * @param limit   The allocation limit.
    069     * @param request The allocation request.
    070     */
    071    public AllocationRequestException(final int limit, final int request) {
    072        this(limit, BigInteger.valueOf(request));
    073    }
    074
    075    /**
    076     * Constructs a new instance.
    077     *
    078     * @param limit     The allocation limit.
    079     * @param request   The allocation request.
    080     * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that
    081     *                  the cause is nonexistent or unknown.)
    082     */
    083    public AllocationRequestException(final int limit, final long request, final Throwable throwable) {
    084        this(limit, BigInteger.valueOf(request), throwable);
    085    }
    086
    087    /**
    088     * Gets the allocation limit.
    089     *
    090     * @return the allocation limit.
    091     */
    092    public int getLimit() {
    093        return limit;
    094    }
    095
    096    /**
    097     * Gets the allocation request.
    098     *
    099     * @return the allocation request.
    100     */
    101    public BigInteger getRequest() {
    102        return request;
    103    }
    104}