OSDN Git Service

start v1.101.107-SNAPSHOT
[jovsonz/Jovsonz.git] / src / main / java / jp / sourceforge / jovsonz / JsNull.java
1 /*
2  * JSON null 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 NULL型Valueを表す。
14  *
15  * <p>Javaのnullとは一切無関係。
16  * その実体はシングルトン。
17  *
18  * <p>表記例
19  *
20  * <pre>
21  * null
22  * </pre>
23  */
24 public final class JsNull
25         implements JsValue, Comparable<JsNull> {
26
27     /** ただ唯一のインスタンス。 */
28     public static final JsNull NULL = new JsNull();
29
30     /** 唯一の文字列表現。 */
31     public static final String TEXT = "null";
32
33     /** 唯一のハッシュ値。 */
34     public static final int ONLYHASH = 982451653; // 大きな素数;
35
36     /**
37      * 隠しコンストラクタ。
38      *
39      * <p>1回しか呼ばれないはず
40      */
41     private JsNull(){
42         super();
43         return;
44     }
45
46     /**
47      * JSON文字列ソースからNULL型Valueを読み込む。
48      *
49      * <p>別型の可能性のある先頭文字を読み込んだ場合、
50      * ソースに文字を読み戻した後nullが返される。
51      *
52      * @param source 文字列ソース
53      * @return NULL型Value。別型の可能性がある場合はnull。
54      * @throws IOException 入力エラー
55      * @throws JsParseException 不正トークンもしくは意図しない入力終了
56      */
57     static JsNull parseNull(JsonSource source)
58             throws IOException, JsParseException{
59         char charHead = source.readOrDie();
60
61         if(charHead != 'n'){
62             source.unread(charHead);
63             return null;
64         }
65
66         if( ! source.matchOrDie("ull") ){
67             throw new JsParseException(JsParseException.ERRMSG_INVALIDTOKEN,
68                                        source.getLineNumber() );
69         }
70
71         return JsNull.NULL;
72     }
73
74     /**
75      * {@inheritDoc}
76      *
77      * <p>常に{@link JsTypes#NULL}を返す。
78      *
79      * @return {@inheritDoc}
80      */
81     @Override
82     public JsTypes getJsTypes(){
83         return JsTypes.NULL;
84     }
85
86     /**
87      * 各種構造の出現をビジターに通知する。
88      *
89      * <p>この実装ではthisの出現のみを通知する。
90      *
91      * @param visitor {@inheritDoc}
92      * @throws JsVisitException {@inheritDoc}
93      */
94     @Override
95     public void traverse(ValueVisitor visitor)
96             throws JsVisitException{
97         visitor.visitValue(this);
98         return;
99     }
100
101     /**
102      * {@inheritDoc}
103      *
104      * <p>ハッシュ値を返す。
105      * 常に{@value ONLYHASH}を返す。
106      *
107      * @return {@inheritDoc}
108      */
109     @Override
110     public int hashCode(){
111         return ONLYHASH;
112     }
113
114     /**
115      * {@inheritDoc}
116      *
117      * <p>等価判定を行う。
118      * {@link #NULL}が渡された時のみtrueを返す。
119      *
120      * @param obj {@inheritDoc}
121      * @return {@inheritDoc}
122      */
123     @Override
124     public boolean equals(Object obj){
125         if(this == obj) return true;
126         if(obj instanceof JsNull) return true;  // シングルトンには冗長
127         return false;
128     }
129
130     /**
131      * {@inheritDoc}
132      *
133      * <p>NULL型Valueを順序付ける。シングルトン相手にほぼ無意味。
134      * null以外の引数には必ず0を返す。
135      *
136      * @param value {@inheritDoc}
137      * @return {@inheritDoc}
138      * @throws NullPointerException 引数がnull
139      */
140     @Override
141     public int compareTo(JsNull value) throws NullPointerException{
142         if(value == null) throw new NullPointerException();
143         return 0;
144     }
145
146     /**
147      * {@inheritDoc}
148      *
149      * <p>文字列表現を返す。
150      * 常に文字列 {@value TEXT} を返す。
151      * JSON表記の一部としての利用も可能。
152      *
153      * @return {@inheritDoc}
154      */
155     @Override
156     public String toString(){
157         return TEXT;
158     }
159
160 }