4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
8 package jp.sourceforge.jovsonz;
10 import java.io.IOException;
13 * JSON BOOLEAN型Valueを表す。
25 public final class JsBoolean
26 implements JsValue, Comparable<JsBoolean> {
29 public static final JsBoolean TRUE = new JsBoolean();
31 public static final JsBoolean FALSE = new JsBoolean();
34 public static final String TEXT_TRUE = "true";
36 public static final String TEXT_FALSE = "false";
39 public static final int HASH_TRUE = Boolean.TRUE.hashCode();
41 public static final int HASH_FALSE = Boolean.FALSE.hashCode();
54 * JSON文字列ソースからBOOLEAN型Valueを読み込む。
56 * <p>別型の可能性のある先頭文字を読み込んだ場合、
57 * ソースに文字を読み戻した後nullが返される。
59 * @param source 文字列ソース
60 * @return BOOLEAN型Value。別型の可能性がある場合はnull。
61 * @throws IOException 入力エラー
62 * @throws JsParseException 不正トークンもしくは意図しない入力終了
64 static JsBoolean parseBoolean(JsonSource source)
65 throws IOException, JsParseException{
66 JsBoolean result = null;
67 boolean hasError = false;
69 char charHead = source.readOrDie();
72 if(source.matchOrDie("rue")){
73 result = JsBoolean.TRUE;
79 if(source.matchOrDie("alse")){
80 result = JsBoolean.FALSE;
86 source.unread(charHead);
91 throw new JsParseException(JsParseException.ERRMSG_INVALIDTOKEN,
92 source.getLineNumber() );
101 * <p>常に{@link JsTypes#BOOLEAN}を返す。
103 * @return {@inheritDoc}
106 public JsTypes getJsTypes(){
107 return JsTypes.BOOLEAN;
113 * <p>この実装ではthisの出現のみを通知する。
115 * @param visitor {@inheritDoc}
116 * @throws JsVisitException {@inheritDoc}
119 public void traverse(ValueVisitor visitor)
120 throws JsVisitException{
121 visitor.visitValue(this);
129 * 真なら{@link #HASH_TRUE}、偽なら{@link #HASH_FALSE}を返す。
131 * @return {@inheritDoc}
134 public int hashCode(){
136 if(this == TRUE) result = HASH_TRUE;
137 else result = HASH_FALSE;
146 * @param obj {@inheritDoc}
147 * @return {@inheritDoc}
150 public boolean equals(Object obj){
151 if(this == obj) return true;
152 if(obj instanceof JsBoolean) return false;
159 * <p>BOOLEAN型Valueを順序付ける。
160 * ({@link #TRUE}、{@link #FALSE})の順に順序付けられる。
162 * @param value {@inheritDoc}
163 * @return {@inheritDoc}
164 * @throws NullPointerException 引数がnull
167 public int compareTo(JsBoolean value) throws NullPointerException{
168 if(value == null) throw new NullPointerException();
171 if(this == value) result = 0;
172 else if(this == TRUE) result = -1;
179 * boolean値を反映したBOOLEAN型Valueを返す。
181 * @param bool boolean値
182 * @return BOOLEAN型Value
184 public static JsBoolean valueOf(boolean bool){
185 if(bool) return TRUE;
194 public boolean booleanValue(){
195 if(this == TRUE) return true;
204 public boolean isTrue(){
205 if(this == TRUE) return true;
214 public boolean isFalse(){
215 if(this != TRUE) return true;
222 * <p>JSON表記の一部としての利用も可能。
224 * @return {@inheritDoc}
227 public String toString(){
228 if(this == TRUE) return TEXT_TRUE;