Skip to content

Commit 9dafae8

Browse files
refact: Statements extends List<Statement>
1 parent 4f91746 commit 9dafae8

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

src/main/java/net/sf/jsqlparser/statement/Statements.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@
1111

1212
import java.io.Serializable;
1313
import java.util.ArrayList;
14-
import java.util.Collection;
15-
import java.util.Collections;
1614
import java.util.List;
17-
import java.util.Optional;
1815

19-
public class Statements implements Serializable {
20-
21-
private List<Statement> statements;
16+
public class Statements extends ArrayList<Statement> implements Serializable {
2217

18+
@Deprecated
2319
public List<Statement> getStatements() {
24-
return statements;
20+
return this;
2521
}
2622

23+
@Deprecated
2724
public void setStatements(List<Statement> statements) {
28-
this.statements = statements;
25+
this.clear();
26+
this.addAll(statements);
2927
}
3028

3129
public void accept(StatementVisitor statementVisitor) {
@@ -35,7 +33,7 @@ public void accept(StatementVisitor statementVisitor) {
3533
@Override
3634
public String toString() {
3735
StringBuilder b = new StringBuilder();
38-
for (Statement stmt : statements) {
36+
for (Statement stmt : this) {
3937
// IfElseStatements and Blocks control the Semicolons by themselves
4038
if (stmt instanceof IfElseStatement || stmt instanceof Block) {
4139
b.append(stmt).append("\n");
@@ -45,21 +43,4 @@ public String toString() {
4543
}
4644
return b.toString();
4745
}
48-
49-
public Statements withStatements(List<Statement> statements) {
50-
this.setStatements(statements);
51-
return this;
52-
}
53-
54-
public Statements addStatements(Statement... statements) {
55-
List<Statement> collection = Optional.ofNullable(getStatements()).orElseGet(ArrayList::new);
56-
Collections.addAll(collection, statements);
57-
return this.withStatements(collection);
58-
}
59-
60-
public Statements addStatements(Collection<? extends Statement> statements) {
61-
List<Statement> collection = Optional.ofNullable(getStatements()).orElseGet(ArrayList::new);
62-
collection.addAll(statements);
63-
return this.withStatements(collection);
64-
}
6546
}

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,6 @@ Block Block() #Block : {
737737

738738
Statements Statements() #Statements: {
739739
Statements stmts = new Statements();
740-
List<Statement> list = new ArrayList<Statement>();
741-
742740
IfElseStatement ifElseStatement = null;
743741
Statement stm = null;
744742
Statement stm2 = null;
@@ -756,18 +754,18 @@ Statements Statements() #Statements: {
756754
<K_ELSE> ( stm2 = SingleStatement() | stm2 = Block() ) { ifElseStatement.setElseStatement(stm2); }
757755
]
758756

759-
{ list.add( ifElseStatement ); }
757+
{ stmts.add( ifElseStatement ); }
760758
)
761759
|
762760
(
763761
stm = SingleStatement()
764762
| stm = Block()
765763

766764
[ LOOKAHEAD(2) <ST_SEMICOLON> ]
767-
) { list.add(stm); }
765+
) { stmts.add(stm); }
768766
|
769767
LOOKAHEAD( { getAsBoolean(Feature.allowUnsupportedStatements) } ) stm = UnsupportedStatement()
770-
{ if ( !((UnsupportedStatement) stm).isEmpty() ) list.add(stm); }
768+
{ if ( !((UnsupportedStatement) stm).isEmpty() ) stmts.add(stm); }
771769
)
772770

773771
(
@@ -784,20 +782,20 @@ Statements Statements() #Statements: {
784782
<K_ELSE> ( stm2 = SingleStatement() | stm2 = Block() ) { ifElseStatement.setElseStatement(stm2); }
785783
]
786784

787-
{ list.add( ifElseStatement ); }
785+
{ stmts.add( ifElseStatement ); }
788786
)
789787
|
790788
(
791789
stm = SingleStatement()
792790
| stm = Block()
793791

794792
[ LOOKAHEAD(2) <ST_SEMICOLON> ]
795-
) { list.add(stm); }
793+
) { stmts.add(stm); }
796794
|
797795
// For any reason, we can't LOOKAHEAD( { getAsBoolean(Feature.allowUnsupportedStatements) } ) here
798796
// As it will result in a Stack Overflow
799797
stm = UnsupportedStatement()
800-
{ if ( !((UnsupportedStatement) stm).isEmpty() ) list.add(stm); }
798+
{ if ( !((UnsupportedStatement) stm).isEmpty() ) stmts.add(stm); }
801799
]
802800
)*
803801
<EOF>
@@ -810,7 +808,7 @@ Statements Statements() #Statements: {
810808
}
811809
}
812810
{
813-
return stmts.withStatements(list);
811+
return stmts;
814812
}
815813
}
816814

0 commit comments

Comments
 (0)