NoSuchMessageException.java
01 /*
02  * Copyright 2008-2016 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
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 griffon.core.i18n;
17 
18 import javax.annotation.Nonnull;
19 import java.util.Locale;
20 
21 import static griffon.util.GriffonNameUtils.requireNonBlank;
22 import static java.util.Objects.requireNonNull;
23 
24 /**
25  @author Alexander Klein
26  @since 2.0.0
27  */
28 public class NoSuchMessageException extends RuntimeException {
29     private static final long serialVersionUID = 9098095569037988099L;
30 
31     private final String key;
32     private final Locale locale;
33 
34     /**
35      * Create a new exception.
36      *
37      @param key    key that could not be resolved for given locale
38      @param locale locale that was used to search for the code within
39      */
40     public NoSuchMessageException(@Nonnull String key, @Nonnull Locale locale) {
41         this(key, locale, null);
42     }
43 
44     /**
45      * Create a new exception.
46      *
47      @param key key that could not be resolved for given locale
48      */
49     public NoSuchMessageException(@Nonnull String key) {
50         this(key, Locale.getDefault());
51     }
52 
53     /**
54      * Create a new exception.
55      *
56      @param key   key that could not be resolved for given locale
57      @param cause throwable that caused this exception
58      */
59     public NoSuchMessageException(@Nonnull String key, @Nonnull Throwable cause) {
60         this(key, Locale.getDefault(), cause);
61     }
62 
63     /**
64      * Create a new exception.
65      *
66      @param key    key that could not be resolved for given locale
67      @param locale locale that was used to search for the code within
68      @param cause  throwable that caused this exception
69      */
70     public NoSuchMessageException(@Nonnull String key, @Nonnull Locale locale, @Nonnull Throwable cause) {
71         super("No message found under key '" + requireNonBlank(key, "key""' for locale '" + requireNonNull(locale, "locale""'.", cause);
72         this.key = key;
73         this.locale = locale;
74     }
75 
76     /**
77      * Get the key without a valid value
78      *
79      @return The key
80      */
81     @Nonnull
82     public String getKey() {
83         return key;
84     }
85 
86     /**
87      * Get the locale without a valid value
88      *
89      @return The locale
90      */
91     @Nonnull
92     public Locale getLocale() {
93         return locale;
94     }
95 }