[LIB-9] Add chesshog-qrcode module
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / ChessQRCodeException.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17 package org.hedgecode.chess.qrcode;
18
19 import java.util.ResourceBundle;
20
21 import org.hedgecode.chess.ChessQRCodeConstants;
22
23 /**
24  * Chess-specific QR Codes read/write/parse Exception.
25  *
26  * @author Dmitry Samoshin aka gotty
27  */
28 public class ChessQRCodeException extends Exception {
29
30     public enum Type {
31
32         READ  ("[Read Error] "),
33         WRITE ("[Write Error] "),
34         PARSE ("[Parse Error] ");
35
36         private String msg;
37
38         Type(String msg) {
39             this.msg = msg;
40         }
41     }
42
43     private static final ResourceBundle LOCALE_BUNDLE =
44             ResourceBundle.getBundle(ChessQRCodeConstants.LOCALE_BUNDLE_FILE);
45
46     private Type type;
47     private String localeKey;
48     private String message;
49
50     public ChessQRCodeException(Type type, String localeKey) {
51         this.type = type;
52         this.localeKey = localeKey;
53         this.message = null;
54     }
55
56     public ChessQRCodeException(Type type, String localeKey, String message) {
57         this.type = type;
58         this.localeKey = localeKey;
59         this.message = message;
60     }
61
62     public Type getType() {
63         return type;
64     }
65
66     public String getLocaleKey() {
67         return localeKey;
68     }
69
70     public String getMessage() {
71         StringBuilder sb = new StringBuilder();
72         sb.append(type.msg);
73         if (localeKey != null)
74             sb.append(
75                     LOCALE_BUNDLE.getString(localeKey)
76             );
77         if (message != null)
78             sb.append(": ").append(message);
79         return sb.toString();
80     }
81
82 }