Skip to content

Commit b63b976

Browse files
author
John J. Aylward
committed
Updates javadoc to match actual exceptions thrown.
Also optimizes some boxing statements and returns.
1 parent d402a99 commit b63b976

File tree

2 files changed

+123
-71
lines changed

2 files changed

+123
-71
lines changed

JSONArray.java

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ of this software and associated documentation files (the "Software"), to deal
3636
import java.util.List;
3737
import java.util.Map;
3838

39+
3940
/**
4041
* A JSONArray is an ordered sequence of values. Its external text form is a
4142
* string wrapped in square brackets with commas separating the values. The
@@ -182,7 +183,7 @@ public JSONArray(Collection<?> collection) {
182183
* Construct a JSONArray from an array
183184
*
184185
* @throws JSONException
185-
* If not an array.
186+
* If not an array or if an array value is non-finite number.
186187
*/
187188
public JSONArray(Object array) throws JSONException {
188189
this();
@@ -465,11 +466,11 @@ public String getString(int index) throws JSONException {
465466
}
466467

467468
/**
468-
* Determine if the value is null.
469+
* Determine if the value is <code>null</code>.
469470
*
470471
* @param index
471472
* The index must be between 0 and length() - 1.
472-
* @return true if the value at the index is null, or if there is no value.
473+
* @return true if the value at the index is <code>null</code>, or if there is no value.
473474
*/
474475
public boolean isNull(int index) {
475476
return JSONObject.NULL.equals(this.opt(index));
@@ -953,8 +954,7 @@ public String optString(int index, String defaultValue) {
953954
* @return this.
954955
*/
955956
public JSONArray put(boolean value) {
956-
this.put(value ? Boolean.TRUE : Boolean.FALSE);
957-
return this;
957+
return this.put(value ? Boolean.TRUE : Boolean.FALSE);
958958
}
959959

960960
/**
@@ -964,26 +964,37 @@ public JSONArray put(boolean value) {
964964
* @param value
965965
* A Collection value.
966966
* @return this.
967+
* @throws JSONException
968+
* If the value is non-finite number.
967969
*/
968970
public JSONArray put(Collection<?> value) {
969-
this.put(new JSONArray(value));
970-
return this;
971+
return this.put(new JSONArray(value));
971972
}
972973

973974
/**
974975
* Append a double value. This increases the array's length by one.
975976
*
976977
* @param value
977978
* A double value.
979+
* @return this.
978980
* @throws JSONException
979981
* if the value is not finite.
980-
* @return this.
981982
*/
982983
public JSONArray put(double value) throws JSONException {
983-
Double d = new Double(value);
984-
JSONObject.testValidity(d);
985-
this.put(d);
986-
return this;
984+
return this.put(Double.valueOf(value));
985+
}
986+
987+
/**
988+
* Append a float value. This increases the array's length by one.
989+
*
990+
* @param value
991+
* A float value.
992+
* @return this.
993+
* @throws JSONException
994+
* if the value is not finite.
995+
*/
996+
public JSONArray put(float value) throws JSONException {
997+
return this.put(Float.valueOf(value));
987998
}
988999

9891000
/**
@@ -994,8 +1005,7 @@ public JSONArray put(double value) throws JSONException {
9941005
* @return this.
9951006
*/
9961007
public JSONArray put(int value) {
997-
this.put(new Integer(value));
998-
return this;
1008+
return this.put(Integer.valueOf(value));
9991009
}
10001010

10011011
/**
@@ -1006,8 +1016,7 @@ public JSONArray put(int value) {
10061016
* @return this.
10071017
*/
10081018
public JSONArray put(long value) {
1009-
this.put(new Long(value));
1010-
return this;
1019+
return this.put(Long.valueOf(value));
10111020
}
10121021

10131022
/**
@@ -1017,10 +1026,13 @@ public JSONArray put(long value) {
10171026
* @param value
10181027
* A Map value.
10191028
* @return this.
1029+
* @throws JSONException
1030+
* If a value in the map is non-finite number.
1031+
* @throws NullPointerException
1032+
* If a key in the map is <code>null</code>
10201033
*/
10211034
public JSONArray put(Map<?, ?> value) {
1022-
this.put(new JSONObject(value));
1023-
return this;
1035+
return this.put(new JSONObject(value));
10241036
}
10251037

10261038
/**
@@ -1031,8 +1043,11 @@ public JSONArray put(Map<?, ?> value) {
10311043
* Integer, JSONArray, JSONObject, Long, or String, or the
10321044
* JSONObject.NULL object.
10331045
* @return this.
1046+
* @throws JSONException
1047+
* If the value is non-finite number.
10341048
*/
10351049
public JSONArray put(Object value) {
1050+
JSONObject.testValidity(value);
10361051
this.myArrayList.add(value);
10371052
return this;
10381053
}
@@ -1051,8 +1066,7 @@ public JSONArray put(Object value) {
10511066
* If the index is negative.
10521067
*/
10531068
public JSONArray put(int index, boolean value) throws JSONException {
1054-
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
1055-
return this;
1069+
return this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
10561070
}
10571071

10581072
/**
@@ -1065,11 +1079,10 @@ public JSONArray put(int index, boolean value) throws JSONException {
10651079
* A Collection value.
10661080
* @return this.
10671081
* @throws JSONException
1068-
* If the index is negative or if the value is not finite.
1082+
* If the index is negative or if the value is non-finite.
10691083
*/
10701084
public JSONArray put(int index, Collection<?> value) throws JSONException {
1071-
this.put(index, new JSONArray(value));
1072-
return this;
1085+
return this.put(index, new JSONArray(value));
10731086
}
10741087

10751088
/**
@@ -1083,11 +1096,27 @@ public JSONArray put(int index, Collection<?> value) throws JSONException {
10831096
* A double value.
10841097
* @return this.
10851098
* @throws JSONException
1086-
* If the index is negative or if the value is not finite.
1099+
* If the index is negative or if the value is non-finite.
10871100
*/
10881101
public JSONArray put(int index, double value) throws JSONException {
1089-
this.put(index, new Double(value));
1090-
return this;
1102+
return this.put(index, Double.valueOf(value));
1103+
}
1104+
1105+
/**
1106+
* Put or replace a float value. If the index is greater than the length of
1107+
* the JSONArray, then null elements will be added as necessary to pad it
1108+
* out.
1109+
*
1110+
* @param index
1111+
* The subscript.
1112+
* @param value
1113+
* A float value.
1114+
* @return this.
1115+
* @throws JSONException
1116+
* If the index is negative or if the value is non-finite.
1117+
*/
1118+
public JSONArray put(int index, float value) throws JSONException {
1119+
return this.put(index, Float.valueOf(value));
10911120
}
10921121

10931122
/**
@@ -1104,8 +1133,7 @@ public JSONArray put(int index, double value) throws JSONException {
11041133
* If the index is negative.
11051134
*/
11061135
public JSONArray put(int index, int value) throws JSONException {
1107-
this.put(index, new Integer(value));
1108-
return this;
1136+
return this.put(index, Integer.valueOf(value));
11091137
}
11101138

11111139
/**
@@ -1122,8 +1150,7 @@ public JSONArray put(int index, int value) throws JSONException {
11221150
* If the index is negative.
11231151
*/
11241152
public JSONArray put(int index, long value) throws JSONException {
1125-
this.put(index, new Long(value));
1126-
return this;
1153+
return this.put(index, Long.valueOf(value));
11271154
}
11281155

11291156
/**
@@ -1138,6 +1165,8 @@ public JSONArray put(int index, long value) throws JSONException {
11381165
* @throws JSONException
11391166
* If the index is negative or if the the value is an invalid
11401167
* number.
1168+
* @throws NullPointerException
1169+
* If a key in the map is <code>null</code>
11411170
*/
11421171
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
11431172
this.put(index, new JSONObject(value));
@@ -1161,25 +1190,26 @@ public JSONArray put(int index, Map<?, ?> value) throws JSONException {
11611190
* number.
11621191
*/
11631192
public JSONArray put(int index, Object value) throws JSONException {
1164-
JSONObject.testValidity(value);
11651193
if (index < 0) {
11661194
throw new JSONException("JSONArray[" + index + "] not found.");
11671195
}
11681196
if (index < this.length()) {
1197+
JSONObject.testValidity(value);
11691198
this.myArrayList.set(index, value);
1170-
} else if(index == this.length()){
1199+
return this;
1200+
}
1201+
if(index == this.length()){
11711202
// simple append
1172-
this.put(value);
1173-
} else {
1174-
// if we are inserting past the length, we want to grow the array all at once
1175-
// instead of incrementally.
1176-
this.myArrayList.ensureCapacity(index + 1);
1177-
while (index != this.length()) {
1178-
this.put(JSONObject.NULL);
1179-
}
1180-
this.put(value);
1203+
return this.put(value);
11811204
}
1182-
return this;
1205+
// if we are inserting past the length, we want to grow the array all at once
1206+
// instead of incrementally.
1207+
this.myArrayList.ensureCapacity(index + 1);
1208+
while (index != this.length()) {
1209+
// we don't need to test validity of NULL objects
1210+
this.myArrayList.add(JSONObject.NULL);
1211+
}
1212+
return this.put(value);
11831213
}
11841214

11851215
/**

0 commit comments

Comments
 (0)