11package org .javawebstack .abstractdata ;
22
33import org .javawebstack .abstractdata .collector .AbstractArrayCollector ;
4+ import org .javawebstack .abstractdata .exception .AbstractCoercingException ;
45
56import java .util .*;
67import java .util .function .Function ;
@@ -15,109 +16,118 @@ public boolean isArray() {
1516 return true ;
1617 }
1718
18- public AbstractArray array () {
19+ public AbstractArray array (boolean strict ) throws AbstractCoercingException {
1920 return this ;
2021 }
2122
22- public AbstractObject object (String key ) {
23+ public AbstractObject object (boolean strict ) throws AbstractCoercingException {
24+ if (strict )
25+ throw new AbstractCoercingException (Type .OBJECT , Type .ARRAY );
26+ AbstractObject object = new AbstractObject ();
27+ for (int i =0 ; i < elements .size (); i ++)
28+ object .set (String .valueOf (i ), elements .get (i ));
29+ return object ;
30+ }
31+
32+ public AbstractObject object (String key ) throws AbstractCoercingException {
2333 return query (key ).object ();
2434 }
2535
26- public AbstractArray array (String key ) {
36+ public AbstractArray array (String key ) throws AbstractCoercingException {
2737 return query (key ).array ();
2838 }
2939
30- public AbstractPrimitive primitive (String key ) {
40+ public AbstractPrimitive primitive (String key ) throws AbstractCoercingException {
3141 return query (key ).primitive ();
3242 }
3343
34- public String string (String key ) {
44+ public String string (String key ) throws AbstractCoercingException {
3545 return query (key ).string ();
3646 }
3747
38- public Boolean bool (String key ) {
48+ public Boolean bool (String key ) throws AbstractCoercingException {
3949 return query (key ).bool ();
4050 }
4151
42- public Number number (String key ) {
52+ public Number number (String key ) throws AbstractCoercingException {
4353 return query (key ).number ();
4454 }
4555
46- public AbstractObject object (String key , AbstractObject orElse ) {
56+ public AbstractObject object (String key , AbstractObject orElse ) throws AbstractCoercingException {
4757 return query (key , orElse ).object ();
4858 }
4959
50- public AbstractArray array (String key , AbstractArray orElse ) {
60+ public AbstractArray array (String key , AbstractArray orElse ) throws AbstractCoercingException {
5161 return query (key , orElse ).array ();
5262 }
5363
54- public AbstractPrimitive primitive (String key , AbstractPrimitive orElse ) {
64+ public AbstractPrimitive primitive (String key , AbstractPrimitive orElse ) throws AbstractCoercingException {
5565 return query (key , orElse ).primitive ();
5666 }
5767
58- public String string (String key , String orElse ) {
68+ public String string (String key , String orElse ) throws AbstractCoercingException {
5969 return query (key , new AbstractPrimitive (orElse )).string ();
6070 }
6171
62- public Boolean bool (String key , Boolean orElse ) {
72+ public Boolean bool (String key , Boolean orElse ) throws AbstractCoercingException {
6373 return query (key , new AbstractPrimitive (orElse )).bool ();
6474 }
6575
66- public Number number (String key , Number orElse ) {
76+ public Number number (String key , Number orElse ) throws AbstractCoercingException {
6777 return query (key , new AbstractPrimitive (orElse )).number ();
6878 }
6979
70- public AbstractObject object (int index ) {
80+ public AbstractObject object (int index ) throws AbstractCoercingException {
7181 return get (index ).object ();
7282 }
7383
74- public AbstractArray array (int index ) {
84+ public AbstractArray array (int index ) throws AbstractCoercingException {
7585 return get (index ).array ();
7686 }
7787
78- public AbstractPrimitive primitive (int index ) {
88+ public AbstractPrimitive primitive (int index ) throws AbstractCoercingException {
7989 return get (index ).primitive ();
8090 }
8191
82- public String string (int index ) {
92+ public String string (int index ) throws AbstractCoercingException {
8393 return get (index ).string ();
8494 }
8595
86- public Boolean bool (int index ) {
96+ public Boolean bool (int index ) throws AbstractCoercingException {
8797 return get (index ).bool ();
8898 }
8999
90- public Number number (int index ) {
100+ public Number number (int index ) throws AbstractCoercingException {
91101 return get (index ).number ();
92102 }
93103
94- public AbstractObject object (int index , AbstractObject orElse ) {
104+ public AbstractObject object (int index , AbstractObject orElse ) throws AbstractCoercingException {
95105 return get (index , orElse ).object ();
96106 }
97107
98- public AbstractArray array (int index , AbstractArray orElse ) {
108+ public AbstractArray array (int index , AbstractArray orElse ) throws AbstractCoercingException {
99109 return get (index , orElse ).array ();
100110 }
101111
102- public AbstractPrimitive primitive (int index , AbstractPrimitive orElse ) {
112+ public AbstractPrimitive primitive (int index , AbstractPrimitive orElse ) throws AbstractCoercingException {
103113 return get (index , orElse ).primitive ();
104114 }
105115
106- public String string (int index , String orElse ) {
116+ public String string (int index , String orElse ) throws AbstractCoercingException {
107117 return get (index , new AbstractPrimitive (orElse )).string ();
108118 }
109119
110- public Boolean bool (int index , Boolean orElse ) {
120+ public Boolean bool (int index , Boolean orElse ) throws AbstractCoercingException {
111121 return get (index , new AbstractPrimitive (orElse )).bool ();
112122 }
113123
114- public Number number (int index , Number orElse ) {
124+ public Number number (int index , Number orElse ) throws AbstractCoercingException {
115125 return get (index , new AbstractPrimitive (orElse )).number ();
116126 }
117127
118128 public AbstractArray add (AbstractElement element ) {
119129 if (element == null )
120- element = AbstractNull .INSTANCE ;
130+ element = AbstractNull .VALUE ;
121131 elements .add (element );
122132 return this ;
123133 }
@@ -141,7 +151,7 @@ public AbstractArray(Collection<Object> abstractElements) {
141151 }
142152
143153 public AbstractArray addNull () {
144- return add (AbstractNull .INSTANCE );
154+ return add (AbstractNull .VALUE );
145155 }
146156
147157 public AbstractArray add (Number value ) {
@@ -163,7 +173,7 @@ public AbstractArray add(String value) {
163173 }
164174
165175 public AbstractArray setNull (int i ) {
166- return set (i , AbstractNull .INSTANCE );
176+ return set (i , AbstractNull .VALUE );
167177 }
168178
169179 public AbstractArray set (int i , AbstractElement element ) {
@@ -264,6 +274,28 @@ public Object toObject() {
264274 return list ;
265275 }
266276
277+ public List <String > toStringList () {
278+ return toStringList (false );
279+ }
280+
281+ public List <String > toStringList (boolean strict ) {
282+ List <String > list = new ArrayList <>();
283+ for (AbstractElement e : elements )
284+ list .add (e .string (strict ));
285+ return list ;
286+ }
287+
288+ public List <AbstractObject > toObjectList () {
289+ return toObjectList (false );
290+ }
291+
292+ public List <AbstractObject > toObjectList (boolean strict ) {
293+ List <AbstractObject > list = new ArrayList <>();
294+ for (AbstractElement e : elements )
295+ list .add (e .object (strict ));
296+ return list ;
297+ }
298+
267299 public static AbstractArray fromArray (Object [] objects ) {
268300 return new AbstractArray (objects );
269301 }
0 commit comments