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 package org.apache.commons.math3.exception;
18 
19 import org.apache.commons.math3.exception.util.Localizable;
20 import org.apache.commons.math3.exception.util.LocalizedFormats;
21 
22 /**
23  * Exception to be thrown when two sets of dimensions differ.
24  *
25  * @since 3.0
26  */
27 public class MultiDimensionMismatchException extends MathIllegalArgumentException {
28     /** Serializable version Id. */
29     private static final long serialVersionUID = -8415396756375798143L;
30 
31     /** Wrong dimensions. */
32     private final Integer[] wrong;
33     /** Correct dimensions. */
34     private final Integer[] expected;
35 
36     /**
37      * Construct an exception from the mismatched dimensions.
38      *
39      * @param wrong Wrong dimensions.
40      * @param expected Expected dimensions.
41      */
MultiDimensionMismatchException(Integer[] wrong, Integer[] expected)42     public MultiDimensionMismatchException(Integer[] wrong,
43                                            Integer[] expected) {
44         this(LocalizedFormats.DIMENSIONS_MISMATCH, wrong, expected);
45     }
46 
47     /**
48      * Construct an exception from the mismatched dimensions.
49      *
50      * @param specific Message pattern providing the specific context of
51      * the error.
52      * @param wrong Wrong dimensions.
53      * @param expected Expected dimensions.
54      */
MultiDimensionMismatchException(Localizable specific, Integer[] wrong, Integer[] expected)55     public MultiDimensionMismatchException(Localizable specific,
56                                            Integer[] wrong,
57                                            Integer[] expected) {
58         super(specific, wrong, expected);
59         this.wrong = wrong.clone();
60         this.expected = expected.clone();
61     }
62 
63     /**
64      * @return an array containing the wrong dimensions.
65      */
getWrongDimensions()66     public Integer[] getWrongDimensions() {
67         return wrong.clone();
68     }
69     /**
70      * @return an array containing the expected dimensions.
71      */
getExpectedDimensions()72     public Integer[] getExpectedDimensions() {
73         return expected.clone();
74     }
75 
76     /**
77      * @param index Dimension index.
78      * @return the wrong dimension stored at {@code index}.
79      */
getWrongDimension(int index)80     public int getWrongDimension(int index) {
81         return wrong[index].intValue();
82     }
83     /**
84      * @param index Dimension index.
85      * @return the expected dimension stored at {@code index}.
86      */
getExpectedDimension(int index)87     public int getExpectedDimension(int index) {
88         return expected[index].intValue();
89     }
90 }
91