4 * License : The MIT License
\r
5 * Copyright(c) 2009 olyutorskii
\r
8 package jp.sourceforge.jindolf.json;
\r
10 import java.io.IOException;
\r
11 import java.util.ArrayList;
\r
12 import java.util.Collection;
\r
13 import java.util.Collections;
\r
14 import java.util.Iterator;
\r
15 import java.util.List;
\r
20 public class JsArray
\r
21 extends AbstractJsValue
\r
22 implements Iterable<JsValue> {
\r
24 private final List<JsValue> valueList = new ArrayList<JsValue>();
\r
25 private boolean changed = false;
\r
38 * JSON Arrayを文字ストリームからパースする。
\r
39 * @param reader 文字入力
\r
40 * @return JSON Array。入力終了ならnull
\r
41 * @throws IOException 入力エラー
\r
42 * @throws JsParseException パースエラー
\r
44 static JsArray parseArray(JsonReader reader)
\r
49 Json.skipWhiteSpace(reader);
\r
50 chData = reader.read();
\r
51 if(chData < '\u0000') return null;
\r
52 if(chData != '[') throw new JsParseException();
\r
54 JsArray result = new JsArray();
\r
57 Json.skipWhiteSpace(reader);
\r
58 chData = reader.read();
\r
59 if(chData < '\u0000') throw new JsParseException();
\r
60 if(chData == ']') break;
\r
62 if(result.size() <= 0){
\r
63 reader.unread(chData);
\r
65 if(chData != ',') throw new JsParseException();
\r
66 Json.skipWhiteSpace(reader);
\r
69 JsValue value = Json.parseValue(reader);
\r
71 throw new JsParseException();
\r
83 * @param value JSON Value
\r
85 public void add(JsValue value){
\r
86 this.valueList.add(value);
\r
87 this.changed = true;
\r
93 * @param index 0で始まる位置
\r
95 * @throws IndexOutOfBoundsException 不正な位置指定
\r
97 public JsValue get(int index) throws IndexOutOfBoundsException{
\r
98 return this.valueList.get(index);
\r
104 public void clear(){
\r
105 if(this.valueList.size() > 0) this.changed = true;
\r
106 this.valueList.clear();
\r
112 * @param value JSON Value
\r
113 * @return 既存のValueが削除されたならtrue
\r
115 public boolean remove(JsValue value){
\r
116 boolean removed = this.valueList.remove(value);
\r
117 if(removed) this.changed = true;
\r
126 return this.valueList.size();
\r
130 * Valueにアクセスするための反復子を提供する。
\r
131 * この反復子での削除作業はできない。
\r
134 public Iterator<JsValue> iterator(){
\r
135 Collection<JsValue> unmodColl =
\r
136 Collections.unmodifiableCollection(this.valueList);
\r
137 return unmodColl.iterator();
\r
142 * @return {@inheritDoc}
\r
145 public int hashCode(){
\r
146 return this.valueList.hashCode();
\r
151 * @param obj {@inheritDoc}
\r
152 * @return {@inheritDoc}
\r
155 public boolean equals(Object obj){
\r
156 if(obj == null) return false;
\r
157 if(this == obj) return true;
\r
159 if( ! (obj instanceof JsArray) ) return false;
\r
160 JsArray array = (JsArray) obj;
\r
162 return this.valueList.equals(array.valueList);
\r
167 * @return {@inheritDoc}
\r
170 public String toString(){
\r
171 StringBuilder text = new StringBuilder();
\r
174 boolean hasElem = false;
\r
175 for(JsValue value : this.valueList){
\r
176 if(hasElem) text.append(',');
\r
177 text.append(value);
\r
182 return text.toString();
\r
187 * @param visitor {@inheritDoc}
\r
188 * @throws JsVisitException {@inheritDoc}
\r
191 public void traverse(ValueVisitor visitor) throws JsVisitException{
\r
192 visitor.visitValue(this);
\r
194 for(JsValue value : this.valueList){
\r
195 value.traverse(visitor);
\r
198 visitor.visitCollectionClose(this);
\r
205 * @return {@inheritDoc}
\r
208 public boolean hasChanged(){
\r
209 if(this.changed) return true;
\r
211 for(JsValue value : this.valueList){
\r
212 if(value.hasChanged()) return true;
\r
222 public void setUnchanged(){
\r
223 this.changed = false;
\r
225 for(JsValue value : this.valueList){
\r
226 value.setUnchanged();
\r