OSDN Git Service

start v1.101.107-SNAPSHOT
[jovsonz/Jovsonz.git] / src / main / java / jp / sourceforge / jovsonz / JsBoolean.java
1 /*
2  * JSON boolean value
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sourceforge.jovsonz;
9
10 import java.io.IOException;
11
12 /**
13  * JSON BOOLEAN型Valueを表す。
14  *
15  * <p>真偽値を反映する。
16  * インスタンスは2つしか存在しえない。
17  *
18  * <p>表記例
19  *
20  * <pre>
21  * true
22  * false
23  * </pre>
24  */
25 public final class JsBoolean
26         implements JsValue, Comparable<JsBoolean> {
27
28     /** 唯一の真値。 */
29     public static final JsBoolean TRUE  = new JsBoolean();
30     /** 唯一の偽値。 */
31     public static final JsBoolean FALSE = new JsBoolean();
32
33     /** 真の文字列表現。 */
34     public static final String TEXT_TRUE  = "true";
35     /** 偽の文字列表現。 */
36     public static final String TEXT_FALSE = "false";
37
38     /** 真のハッシュ値。 */
39     public static final int HASH_TRUE = Boolean.TRUE.hashCode();
40     /** 偽のハッシュ値。 */
41     public static final int HASH_FALSE = Boolean.FALSE.hashCode();
42
43     /**
44      * 隠しコンストラクタ。
45      *
46      * <p>2回しか呼ばれないはず。
47      */
48     private JsBoolean(){
49         super();
50         return;
51     }
52
53     /**
54      * JSON文字列ソースからBOOLEAN型Valueを読み込む。
55      *
56      * <p>別型の可能性のある先頭文字を読み込んだ場合、
57      * ソースに文字を読み戻した後nullが返される。
58      *
59      * @param source 文字列ソース
60      * @return BOOLEAN型Value。別型の可能性がある場合はnull。
61      * @throws IOException 入力エラー
62      * @throws JsParseException 不正トークンもしくは意図しない入力終了
63      */
64     static JsBoolean parseBoolean(JsonSource source)
65             throws IOException, JsParseException{
66         JsBoolean result = null;
67         boolean hasError = false;
68
69         char charHead = source.readOrDie();
70         switch(charHead){
71         case 't':
72             if(source.matchOrDie("rue")){
73                 result = JsBoolean.TRUE;
74             }else{
75                 hasError = true;
76             }
77             break;
78         case 'f':
79             if(source.matchOrDie("alse")){
80                 result = JsBoolean.FALSE;
81             }else{
82                 hasError = true;
83             }
84             break;
85         default:
86             source.unread(charHead);
87             break;
88         }
89
90         if(hasError){
91             throw new JsParseException(JsParseException.ERRMSG_INVALIDTOKEN,
92                                        source.getLineNumber() );
93         }
94
95         return result;
96     }
97
98     /**
99      * {@inheritDoc}
100      *
101      * <p>常に{@link JsTypes#BOOLEAN}を返す。
102      *
103      * @return {@inheritDoc}
104      */
105     @Override
106     public JsTypes getJsTypes(){
107         return JsTypes.BOOLEAN;
108     }
109
110     /**
111      * 各種構造の出現をビジターに通知する。
112      *
113      * <p>この実装ではthisの出現のみを通知する。
114      *
115      * @param visitor {@inheritDoc}
116      * @throws JsVisitException {@inheritDoc}
117      */
118     @Override
119     public void traverse(ValueVisitor visitor)
120             throws JsVisitException{
121         visitor.visitValue(this);
122         return;
123     }
124
125     /**
126      * {@inheritDoc}
127      *
128      * <p>ハッシュ値を返す。
129      * 真なら{@link #HASH_TRUE}、偽なら{@link #HASH_FALSE}を返す。
130      *
131      * @return {@inheritDoc}
132      */
133     @Override
134     public int hashCode(){
135         int result;
136         if(this == TRUE) result = HASH_TRUE;
137         else             result = HASH_FALSE;
138         return result;
139     }
140
141     /**
142      * {@inheritDoc}
143      *
144      * <p>等価判定を行う。
145      *
146      * @param obj {@inheritDoc}
147      * @return {@inheritDoc}
148      */
149     @Override
150     public boolean equals(Object obj){
151         if(this == obj) return true;
152         if(obj instanceof JsBoolean) return false;
153         return false;
154     }
155
156     /**
157      * {@inheritDoc}
158      *
159      * <p>BOOLEAN型Valueを順序付ける。
160      * ({@link #TRUE}、{@link #FALSE})の順に順序付けられる。
161      *
162      * @param value {@inheritDoc}
163      * @return {@inheritDoc}
164      * @throws NullPointerException 引数がnull
165      */
166     @Override
167     public int compareTo(JsBoolean value) throws NullPointerException{
168         if(value == null) throw new NullPointerException();
169
170         int result;
171         if(this == value)     result =  0;
172         else if(this == TRUE) result = -1;
173         else                  result = +1;
174
175         return result;
176     }
177
178     /**
179      * boolean値を反映したBOOLEAN型Valueを返す。
180      *
181      * @param bool boolean値
182      * @return BOOLEAN型Value
183      */
184     public static JsBoolean valueOf(boolean bool){
185         if(bool) return TRUE;
186         return FALSE;
187     }
188
189     /**
190      * boolean値を返す。
191      *
192      * @return boolean値
193      */
194     public boolean booleanValue(){
195         if(this == TRUE) return true;
196         return false;
197     }
198
199     /**
200      * 真か判定する。
201      *
202      * @return 真ならtrue
203      */
204     public boolean isTrue(){
205         if(this == TRUE) return true;
206         return false;
207     }
208
209     /**
210      * 偽か判定する。
211      *
212      * @return 偽ならtrue
213      */
214     public boolean isFalse(){
215         if(this != TRUE) return true;
216         return false;
217     }
218
219     /**
220      * 文字列表現を返す。
221      *
222      * <p>JSON表記の一部としての利用も可能。
223      *
224      * @return {@inheritDoc}
225      */
226     @Override
227     public String toString(){
228         if(this == TRUE) return TEXT_TRUE;
229         return TEXT_FALSE;
230     }
231
232 }