1212import com .rethinkdb .net .Connection ;
1313import com .rethinkdb .net .Result ;
1414import com .rethinkdb .utils .Types ;
15+ import org .jetbrains .annotations .NotNull ;
16+ import org .jetbrains .annotations .Nullable ;
1517
1618import java .util .*;
1719import java .util .Map .Entry ;
2224 * Base class for all ReQL queries.
2325 */
2426public class ReqlAst {
25- protected final TermType termType ;
26- protected final Arguments args ;
27- protected final OptArgs optargs ;
28-
29- protected ReqlAst (TermType termType , Arguments args , OptArgs optargs ) {
30- if (termType == null ) {
31- throw new ReqlDriverError ("termType can't be null!" );
32- }
27+ protected final @ NotNull TermType termType ;
28+ protected final @ Nullable Arguments args ;
29+ protected final @ Nullable OptArgs optargs ;
30+
31+ protected ReqlAst (@ NotNull TermType termType , @ Nullable Arguments args , @ Nullable OptArgs optargs ) {
32+ // if (termType == null) {
33+ // throw new ReqlDriverError("termType can't be null!");
34+ // }
3335 this .termType = termType ;
34- this .args = args != null ? args : new Arguments ();
35- this .optargs = optargs != null ? optargs : new OptArgs ();
36- }
37-
38- public static Map <String , Object > buildOptarg (OptArgs opts ) {
39- Map <String , Object > result = new LinkedHashMap <>(opts .size ());
40- opts .forEach ((name , arg ) -> result .put (name , arg .build ()));
41- return result ;
36+ this .args = args ;
37+ this .optargs = optargs ;
4238 }
4339
4440 protected Object build () {
45- // Create a JSON object from the Ast
41+ // Create a JSON object from the AST
4642 // set initial capacity to max size possible, avoids resizing
4743 List <Object > list = new ArrayList <>(3 );
4844 list .add (termType .value );
49- list .add (args .isEmpty () ? Collections .emptyList () : args .stream ().map (ReqlAst ::build ).collect (Collectors .toList ()));
50- if (optargs . size () > 0 ) {
51- list .add (buildOptarg (optargs ));
45+ list .add (args == null || args .isEmpty () ? Collections .emptyList () : args .stream ().map (ReqlAst ::build ).collect (Collectors .toList ()));
46+ if (optargs != null && ! optargs . isEmpty () ) {
47+ list .add (buildToMap (optargs ));
5248 }
5349 return list ;
5450 }
@@ -1726,15 +1722,17 @@ private void astToString(StringBuilder builder, String name, String indent, bool
17261722 builder .append ('(' ).append (length ).append (length != 1 ? " bytes" : " byte" ).append (")" );
17271723 }
17281724 builder .append ('\n' );
1729- Iterator <ReqlAst > argsIterator = args .iterator ();
1730- while (argsIterator .hasNext ()) {
1731- ReqlAst arg = argsIterator .next ();
1732- arg .astToString (builder , null ,
1733- indent + (tail ? " " : "│ " ),
1734- !argsIterator .hasNext () && optargs .isEmpty ());
1725+ if (args != null ) {
1726+ Iterator <ReqlAst > argsIterator = args .iterator ();
1727+ while (argsIterator .hasNext ()) {
1728+ ReqlAst arg = argsIterator .next ();
1729+ arg .astToString (builder , null ,
1730+ indent + (tail ? " " : "│ " ),
1731+ !argsIterator .hasNext () && (optargs == null || optargs .isEmpty ()));
1732+ }
17351733 }
17361734
1737- if (!optargs .isEmpty ()) {
1735+ if (optargs != null && !optargs .isEmpty ()) {
17381736 builder .append (indent ).append (tail ? " " : "│ " ).append ("└── " ).append ("<optArgs>: \n " );
17391737 Iterator <Entry <String , ReqlAst >> optIterator = optargs .entrySet ().iterator ();
17401738 while (optIterator .hasNext ()) {
@@ -1746,6 +1744,12 @@ private void astToString(StringBuilder builder, String name, String indent, bool
17461744 }
17471745 }
17481746
1747+ static Map <String , Object > buildToMap (OptArgs opts ) {
1748+ Map <String , Object > result = new LinkedHashMap <>(opts .size ());
1749+ opts .forEach ((name , arg ) -> result .put (name , arg .build ()));
1750+ return result ;
1751+ }
1752+
17491753 private static <T > T handleAtom (Result <T > result ) {
17501754 if (!result .responseType ().equals (ResponseType .SUCCESS_ATOM )) {
17511755 throw new IllegalStateException ("result is not an atom." );
0 commit comments