4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
8 package jp.sourceforge.jovsonz;
10 import java.io.IOException;
13 * JSON BOOLEAN型Valueを表す。
22 public final class JsBoolean
23 implements JsValue, Comparable<JsBoolean> {
26 public static final JsBoolean TRUE = new JsBoolean();
28 public static final JsBoolean FALSE = new JsBoolean();
31 public static final String TEXT_TRUE = "true";
33 public static final String TEXT_FALSE = "false";
36 public static final int HASH_TRUE = Boolean.TRUE.hashCode();
38 public static final int HASH_FALSE = Boolean.FALSE.hashCode();
50 * JSON文字列ソースからBOOLEAN型Valueを読み込む。
51 * 別型の可能性のある先頭文字を読み込んだ場合、
52 * ソースに文字を読み戻した後nullが返される。
53 * @param source 文字列ソース
54 * @return BOOLEAN型Value。別型の可能性がある場合はnull。
55 * @throws IOException 入力エラー
56 * @throws JsParseException 不正トークンもしくは意図しない入力終了
58 static JsBoolean parseBoolean(JsonSource source)
59 throws IOException, JsParseException{
60 JsBoolean result = null;
61 boolean hasError = false;
63 char charHead = source.readOrDie();
66 if(source.matchOrDie("rue")){
67 result = JsBoolean.TRUE;
73 if(source.matchOrDie("alse")){
74 result = JsBoolean.FALSE;
80 source.unread(charHead);
85 throw new JsParseException(JsParseException.ERRMSG_INVALIDTOKEN,
86 source.getLineNumber() );
94 * 常に{@link JsTypes#BOOLEAN}を返す。
95 * @return {@inheritDoc}
98 public JsTypes getJsTypes(){
99 return JsTypes.BOOLEAN;
104 * この実装ではthisの出現のみを通知する。
105 * @param visitor {@inheritDoc}
106 * @throws JsVisitException {@inheritDoc}
109 public void traverse(ValueVisitor visitor)
110 throws JsVisitException{
111 visitor.visitValue(this);
118 * 真なら{@link #HASH_TRUE}、偽なら{@link #HASH_FALSE}を返す。
119 * @return {@inheritDoc}
122 public int hashCode(){
124 if(this == TRUE) result = HASH_TRUE;
125 else result = HASH_FALSE;
132 * @param obj {@inheritDoc}
133 * @return {@inheritDoc}
136 public boolean equals(Object obj){
137 if(this == obj) return true;
138 if(obj instanceof JsBoolean) return false;
144 * BOOLEAN型Valueを順序付ける。
145 * ({@link #TRUE}、{@link #FALSE})の順に順序付けられる。
146 * @param value {@inheritDoc}
147 * @return {@inheritDoc}
148 * @throws NullPointerException 引数がnull
151 public int compareTo(JsBoolean value) throws NullPointerException{
152 if(value == null) throw new NullPointerException();
155 if(this == value) result = 0;
156 else if(this == TRUE) result = -1;
163 * boolean値を反映したBOOLEAN型Valueを返す。
164 * @param bool boolean値
165 * @return BOOLEAN型Value
167 public static JsBoolean valueOf(boolean bool){
168 if(bool) return TRUE;
176 public boolean booleanValue(){
177 if(this == TRUE) return true;
185 public boolean isTrue(){
186 if(this == TRUE) return true;
194 public boolean isFalse(){
195 if(this != TRUE) return true;
201 * JSON表記の一部としての利用も可能。
202 * @return {@inheritDoc}
205 public String toString(){
206 if(this == TRUE) return TEXT_TRUE;