NoSuchMessageException.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.core.i18n;
19 
20 import javax.annotation.Nonnull;
21 import java.util.Locale;
22 
23 import static griffon.util.GriffonNameUtils.requireNonBlank;
24 import static java.util.Objects.requireNonNull;
25 
26 /**
27  @author Alexander Klein
28  @since 2.0.0
29  */
30 public class NoSuchMessageException extends RuntimeException {
31     private static final long serialVersionUID = 9098095569037988099L;
32 
33     private final String key;
34     private final Locale locale;
35 
36     /**
37      * Create a new exception.
38      *
39      @param key    key that could not be resolved for given locale
40      @param locale locale that was used to search for the code within
41      */
42     public NoSuchMessageException(@Nonnull String key, @Nonnull Locale locale) {
43         this(key, locale, null);
44     }
45 
46     /**
47      * Create a new exception.
48      *
49      @param key key that could not be resolved for given locale
50      */
51     public NoSuchMessageException(@Nonnull String key) {
52         this(key, Locale.getDefault());
53     }
54 
55     /**
56      * Create a new exception.
57      *
58      @param key   key that could not be resolved for given locale
59      @param cause throwable that caused this exception
60      */
61     public NoSuchMessageException(@Nonnull String key, @Nonnull Throwable cause) {
62         this(key, Locale.getDefault(), cause);
63     }
64 
65     /**
66      * Create a new exception.
67      *
68      @param key    key that could not be resolved for given locale
69      @param locale locale that was used to search for the code within
70      @param cause  throwable that caused this exception
71      */
72     public NoSuchMessageException(@Nonnull String key, @Nonnull Locale locale, @Nonnull Throwable cause) {
73         super("No message found under key '" + requireNonBlank(key, "key""' for locale '" + requireNonNull(locale, "locale""'.", cause);
74         this.key = key;
75         this.locale = locale;
76     }
77 
78     /**
79      * Get the key without a valid value
80      *
81      @return The key
82      */
83     @Nonnull
84     public String getKey() {
85         return key;
86     }
87 
88     /**
89      * Get the locale without a valid value
90      *
91      @return The locale
92      */
93     @Nonnull
94     public Locale getLocale() {
95         return locale;
96     }
97 }