View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.common;
19  
20  import java.math.BigInteger;
21  
22  import org.apache.commons.imaging.ImagingRuntimeException;
23  
24  /**
25   * Thrown when an allocation request is too large.
26   */
27  public class AllocationRequestException extends ImagingRuntimeException {
28  
29      private static final long serialVersionUID = 1L;
30  
31      private static String format(final int limit, final BigInteger request) {
32          return String.format("Allocation limit %,d exceeded: %,d", limit, request);
33      }
34  
35      private final int limit;
36  
37      private final BigInteger request;
38  
39      /**
40       * Constructs a new instance.
41       *
42       * @param limit   The allocation limit.
43       * @param request The allocation request.
44       */
45      public AllocationRequestException(final int limit, final BigInteger request) {
46          super(format(limit, request));
47          this.limit = limit;
48          this.request = request;
49      }
50  
51      /**
52       * Constructs a new instance.
53       *
54       * @param limit     The allocation limit.
55       * @param request   The allocation request.
56       * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that
57       *                  the cause is nonexistent or unknown.)
58       */
59      public AllocationRequestException(final int limit, final BigInteger request, final Throwable throwable) {
60          super(format(limit, request), throwable);
61          this.limit = limit;
62          this.request = request;
63      }
64  
65      /**
66       * Constructs a new instance.
67       *
68       * @param limit   The allocation limit.
69       * @param request The allocation request.
70       */
71      public AllocationRequestException(final int limit, final int request) {
72          this(limit, BigInteger.valueOf(request));
73      }
74  
75      /**
76       * Constructs a new instance.
77       *
78       * @param limit     The allocation limit.
79       * @param request   The allocation request.
80       * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that
81       *                  the cause is nonexistent or unknown.)
82       */
83      public AllocationRequestException(final int limit, final long request, final Throwable throwable) {
84          this(limit, BigInteger.valueOf(request), throwable);
85      }
86  
87      /**
88       * Gets the allocation limit.
89       *
90       * @return the allocation limit.
91       */
92      public int getLimit() {
93          return limit;
94      }
95  
96      /**
97       * Gets the allocation request.
98       *
99       * @return the allocation request.
100      */
101     public BigInteger getRequest() {
102         return request;
103     }
104 }