Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IncompatibleTypeException |
|
| 2.4;2.4 |
1 | /* | |
2 | * Copyright 2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 1.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl1.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.krms.api.engine; | |
17 | ||
18 | import org.kuali.rice.core.api.exception.RiceRuntimeException; | |
19 | ||
20 | /** | |
21 | * An exception which indicates that the type of data being evaluated in the | |
22 | * engine does not match the expected type. | |
23 | * | |
24 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
25 | * | |
26 | */ | |
27 | public class IncompatibleTypeException extends RiceRuntimeException { | |
28 | ||
29 | private static final long serialVersionUID = -8359509154258982033L; | |
30 | ||
31 | private final Object value; | |
32 | private final Class<?>[] validTypes; | |
33 | ||
34 | /** | |
35 | * Constructs an IncompatibleTypeException with a reference to the object | |
36 | * being checked and an array of valid Class objects which the type failed | |
37 | * to match. | |
38 | * | |
39 | * <p>A message describing the nature of the incompatible types will | |
40 | * automatically be generated and can be retrieved through {@link #getMessage()} | |
41 | * | |
42 | * @param value the object which was being evaluated against a set of valid types | |
43 | * @param validTypes the valid types against which the value was being evaluated | |
44 | * but failed to be compatible with | |
45 | */ | |
46 | public IncompatibleTypeException(Object value, Class<?>... validTypes) { | |
47 | 5 | this(null, value, validTypes); |
48 | 5 | } |
49 | ||
50 | /** | |
51 | * Constructs an IncompatibleTypeException with a message, a reference to the object | |
52 | * being checked, and an array of valid Class objects which the type failed | |
53 | * to match. | |
54 | * | |
55 | * <p>The additional message will be prepended to the front of an automatically | |
56 | * generated message describing the nature of the incompatible types and can be | |
57 | * retrieved through {@link #getMessage()}. | |
58 | * | |
59 | * @param additionalMessage the additional message to prepend to the generated exception detail message | |
60 | * @param value the object which was being evaluated against a set of valid types | |
61 | * @param validTypes the valid types against which the value was being evaluated | |
62 | * but failed to be compatible with | |
63 | */ | |
64 | public IncompatibleTypeException(String additionalMessage, Object value, Class<?>... validTypes) { | |
65 | 15 | super(constructMessage(additionalMessage, value, validTypes)); |
66 | 15 | this.value = value; |
67 | 15 | this.validTypes = validTypes; |
68 | 15 | } |
69 | ||
70 | private static String constructMessage(String additionalMessage, Object value, Class<?>... validTypes) { | |
71 | 15 | StringBuilder message = new StringBuilder(); |
72 | 15 | if (additionalMessage != null) { |
73 | 5 | message.append(additionalMessage); |
74 | } | |
75 | 15 | if (message.length() > 0) { |
76 | 5 | message.append(" -> "); |
77 | } | |
78 | 15 | if (validTypes != null && validTypes.length > 0) { |
79 | 6 | message.append("Type should have been one of ["); |
80 | 18 | for (Class<?> validType : validTypes) { |
81 | 12 | message.append(validType.getName()).append(", "); |
82 | } | |
83 | // trim off the last two character to get rid of the last ", " | |
84 | 6 | message.delete(message.length() - 2, message.length()); |
85 | 6 | message.append("] but was ").append(value == null ? "null" : value.getClass().getName()); |
86 | } else { | |
87 | 9 | message.append("Type was ").append(value == null ? "null" : value.getClass().getName()); |
88 | } | |
89 | 15 | return message.toString(); |
90 | } | |
91 | ||
92 | /** | |
93 | * Returns the object which triggered the incompatible type exception. | |
94 | * | |
95 | * @return the value passed to this exception upon construction | |
96 | */ | |
97 | public Object getValue() { | |
98 | 15 | return value; |
99 | } | |
100 | ||
101 | /** | |
102 | * Returns the array of Class objects which include the types against | |
103 | * which the object value was deemed incompatible. | |
104 | * | |
105 | * @return the valid types passed to this exception upon construction | |
106 | */ | |
107 | public Class<?>[] getValidTypes() { | |
108 | 15 | return validTypes; |
109 | } | |
110 | ||
111 | } |