Skip to content

Commit ced6025

Browse files
authored
Convert instanceof to isX() pattern (#107)
## What is the goal of this PR? We've improved the type safety of the code by restricting type casting. To be consistent with a single pattern of performing dynamic casting, we convert all usage of `instanceof` to `isX()` and `asX()`. ## What are the changes implemented in this PR? - Convert instanceof to isX() pattern.
1 parent 2aa66b5 commit ced6025

File tree

6 files changed

+425
-51
lines changed

6 files changed

+425
-51
lines changed

BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ genrule(
4141

4242
java_library(
4343
name = "console",
44-
srcs = glob(["*.java", "command/*.java", "common/*.java"]) + [":version"],
44+
srcs = glob(["*.java", "command/*.java", "common/*.java", "common/exception/*.java"]) + [":version"],
4545
deps = [
4646
"@graknlabs_client_java//:client-java",
4747
"@graknlabs_graql//java:graql",
@@ -201,7 +201,7 @@ release_validate_deps(
201201

202202
checkstyle_test(
203203
name = "checkstyle",
204-
include = glob(["*", "command/*", "common/*", ".grabl/*"]),
204+
include = glob(["*", "command/*", "common/*", "common/exception/*", ".grabl/*"]),
205205
license_type = "agpl",
206206
)
207207

GraknConsole.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,33 @@ private void runRepl(Grakn.Client client) {
9191
} catch (InterruptedException e) {
9292
break;
9393
}
94-
if (command instanceof ReplCommand.Exit) {
94+
if (command.isExit()) {
9595
break;
96-
} else if (command instanceof ReplCommand.Help) {
96+
} else if (command.isHelp()) {
9797
printer.info(ReplCommand.getHelpMenu());
98-
} else if (command instanceof ReplCommand.Clear) {
98+
} else if (command.isClear()) {
9999
reader.getTerminal().puts(InfoCmp.Capability.clear_screen);
100-
} else if (command instanceof ReplCommand.Database.List) {
100+
} else if (command.isDatabaseList()) {
101101
try {
102102
client.databases().all().forEach(database -> printer.info(database));
103103
} catch (GraknClientException e) {
104104
printer.error(e.getMessage());
105105
}
106-
} else if (command instanceof ReplCommand.Database.Create) {
106+
} else if (command.isDatabaseCreate()) {
107107
try {
108108
client.databases().create(command.asDatabaseCreate().database());
109109
printer.info("Database '" + command.asDatabaseCreate().database() + "' created");
110110
} catch (GraknClientException e) {
111111
printer.error(e.getMessage());
112112
}
113-
} else if (command instanceof ReplCommand.Database.Delete) {
113+
} else if (command.isDatabaseDelete()) {
114114
try {
115115
client.databases().delete(command.asDatabaseDelete().database());
116116
printer.info("Database '" + command.asDatabaseDelete().database() + "' deleted");
117117
} catch (GraknClientException e) {
118118
printer.error(e.getMessage());
119119
}
120-
} else if (command instanceof ReplCommand.Transaction) {
120+
} else if (command.isTransaction()) {
121121
String database = command.asTransaction().database();
122122
Grakn.Session.Type sessionType = command.asTransaction().sessionType();
123123
Grakn.Transaction.Type transactionType = command.asTransaction().transactionType();
@@ -142,24 +142,24 @@ private boolean runTransactionRepl(Grakn.Client client, String database, Grakn.S
142142
} catch (InterruptedException e) {
143143
break;
144144
}
145-
if (command instanceof TransactionReplCommand.Exit) {
145+
if (command.isExit()) {
146146
return true;
147-
} else if (command instanceof TransactionReplCommand.Clear) {
147+
} else if (command.isClear()) {
148148
reader.getTerminal().puts(InfoCmp.Capability.clear_screen);
149-
} else if (command instanceof TransactionReplCommand.Help) {
149+
} else if (command.isHelp()) {
150150
printer.info(TransactionReplCommand.getHelpMenu());
151-
} else if (command instanceof TransactionReplCommand.Commit) {
151+
} else if (command.isCommit()) {
152152
tx.commit();
153153
printer.info("Transaction changes committed");
154154
break;
155-
} else if (command instanceof TransactionReplCommand.Rollback) {
155+
} else if (command.isRollback()) {
156156
tx.rollback();
157157
printer.info("Rolled back to the beginning of the transaction");
158-
} else if (command instanceof TransactionReplCommand.Close) {
158+
} else if (command.isClose()) {
159159
tx.close();
160160
printer.info("Transaction closed without committing changes");
161161
break;
162-
} else if (command instanceof TransactionReplCommand.Source) {
162+
} else if (command.isSource()) {
163163
String queryString;
164164
try {
165165
queryString = new String(Files.readAllBytes(Paths.get(command.asSource().file())), StandardCharsets.UTF_8);
@@ -168,7 +168,7 @@ private boolean runTransactionRepl(Grakn.Client client, String database, Grakn.S
168168
continue;
169169
}
170170
runQuery(tx, queryString);
171-
} else if (command instanceof TransactionReplCommand.Query) {
171+
} else if (command.isQuery()) {
172172
runQuery(tx, command.asQuery().query());
173173
}
174174
}

command/ReplCommand.java

Lines changed: 167 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,83 +25,233 @@
2525
import java.util.List;
2626

2727
import static grakn.common.collection.Collections.pair;
28+
import static grakn.console.ErrorMessage.Internal.ILLEGAL_CAST;
29+
30+
public interface ReplCommand {
31+
32+
default boolean isExit() {
33+
return false;
34+
}
35+
36+
default Exit asExit() {
37+
throw new grakn.console.GraknConsoleException(ILLEGAL_CAST);
38+
}
39+
40+
default boolean isHelp() {
41+
return false;
42+
}
43+
44+
default Help asHelp() {
45+
throw new grakn.console.GraknConsoleException(ILLEGAL_CAST);
46+
}
47+
48+
default boolean isClear() {
49+
return false;
50+
}
51+
52+
default Clear asClear() {
53+
throw new grakn.console.GraknConsoleException(ILLEGAL_CAST);
54+
}
55+
56+
default boolean isDatabaseList() {
57+
return false;
58+
}
59+
60+
default Database.List asDatabaseList() {
61+
throw new grakn.console.GraknConsoleException(ILLEGAL_CAST);
62+
}
63+
64+
default boolean isDatabaseCreate() {
65+
return false;
66+
}
67+
68+
default Database.Create asDatabaseCreate() {
69+
throw new grakn.console.GraknConsoleException(ILLEGAL_CAST);
70+
}
71+
72+
default boolean isDatabaseDelete() {
73+
return false;
74+
}
75+
76+
default Database.Delete asDatabaseDelete() {
77+
throw new grakn.console.GraknConsoleException(ILLEGAL_CAST);
78+
}
79+
80+
default boolean isTransaction() {
81+
return false;
82+
}
83+
84+
default Transaction asTransaction() {
85+
throw new grakn.console.GraknConsoleException(ILLEGAL_CAST);
86+
}
87+
88+
class Exit implements ReplCommand {
2889

29-
public abstract class ReplCommand {
30-
public static class Exit extends ReplCommand {
3190
private static String token = "exit";
3291
private static String helpCommand = token;
3392
private static String description = "Exit console";
93+
94+
@Override
95+
public boolean isExit() {
96+
return true;
97+
}
98+
99+
@Override
100+
public Exit asExit() {
101+
return this;
102+
}
34103
}
35104

36-
public static class Help extends ReplCommand {
105+
class Help implements ReplCommand {
106+
37107
private static String token = "help";
38108
private static String helpCommand = token;
39109
private static String description = "Print this help menu";
110+
111+
@Override
112+
public boolean isHelp() {
113+
return true;
114+
}
115+
116+
@Override
117+
public Help asHelp() {
118+
return this;
119+
}
40120
}
41121

42-
public static class Clear extends ReplCommand {
122+
class Clear implements ReplCommand {
123+
43124
private static String token = "clear";
44125
private static String helpCommand = token;
45126
private static String description = "Clear console screen";
127+
128+
@Override
129+
public boolean isClear() {
130+
return true;
131+
}
132+
133+
@Override
134+
public Clear asClear() {
135+
return this;
136+
}
46137
}
47138

48-
public static abstract class Database extends ReplCommand {
139+
abstract class Database implements ReplCommand {
140+
49141
private static String token = "database";
50142

51143
public static class List extends ReplCommand.Database {
144+
52145
private static String token = "list";
53146
private static String helpCommand = Database.token + " " + token;
54147
private static String description = "List the databases on the server";
148+
149+
@Override
150+
public boolean isDatabaseList() {
151+
return true;
152+
}
153+
154+
@Override
155+
public Database.List asDatabaseList() {
156+
return this;
157+
}
55158
}
56159

57160
public static class Create extends ReplCommand.Database {
161+
58162
private static String token = "create";
59163
private static String helpCommand = Database.token + " " + token + " " + "<db>";
60164
private static String description = "Create a database with name <db> on the server";
61165

62166
private final String database;
167+
63168
public Create(String database) {
64169
this.database = database;
65170
}
66-
public String database() { return database; }
171+
172+
public String database() {
173+
return database;
174+
}
175+
176+
@Override
177+
public boolean isDatabaseCreate() {
178+
return true;
179+
}
180+
181+
@Override
182+
public Database.Create asDatabaseCreate() {
183+
return this;
184+
}
67185
}
68186

69187
public static class Delete extends ReplCommand.Database {
188+
70189
private static String token = "delete";
71190
private static String helpCommand = Database.token + " " + token + " " + "<db>";
72191
private static String description = "Delete a database with name <db> on the server";
73192

74193
private final String database;
194+
75195
public Delete(String database) {
76196
this.database = database;
77197
}
78-
public String database() { return database; }
198+
199+
public String database() {
200+
return database;
201+
}
202+
203+
@Override
204+
public boolean isDatabaseDelete() {
205+
return true;
206+
}
207+
208+
@Override
209+
public Database.Delete asDatabaseDelete() {
210+
return this;
211+
}
79212
}
80213
}
81214

82-
public static class Transaction extends ReplCommand {
215+
class Transaction implements ReplCommand {
216+
83217
private static String token = "transaction";
84218
private static String helpCommand = token + " <db> schema|data read|write";
85219
private static String description = "Start a transaction to database <db> with schema or data session, with read or write transaction";
86220

87221
private final String database;
88222
private final Grakn.Session.Type sessionType;
89223
private final Grakn.Transaction.Type transactionType;
224+
90225
public Transaction(String database, Grakn.Session.Type sessionType, Grakn.Transaction.Type transactionType) {
91226
this.database = database;
92227
this.sessionType = sessionType;
93228
this.transactionType = transactionType;
94229
}
95-
public String database() { return database; }
96-
public Grakn.Session.Type sessionType() { return sessionType; }
97-
public Grakn.Transaction.Type transactionType() { return transactionType; }
98-
}
99230

100-
public Database.Create asDatabaseCreate() { return (Database.Create)this; }
101-
public Database.Delete asDatabaseDelete() { return (Database.Delete)this; }
102-
public Transaction asTransaction() { return (Transaction) this; }
231+
public String database() {
232+
return database;
233+
}
234+
235+
public Grakn.Session.Type sessionType() {
236+
return sessionType;
237+
}
238+
239+
public Grakn.Transaction.Type transactionType() {
240+
return transactionType;
241+
}
242+
243+
@Override
244+
public boolean isTransaction() {
245+
return true;
246+
}
247+
248+
@Override
249+
public Transaction asTransaction() {
250+
return this;
251+
}
252+
}
103253

104-
public static String getHelpMenu() {
254+
static String getHelpMenu() {
105255
List<Pair<String, String>> menu = Arrays.asList(
106256
pair(Database.List.helpCommand, Database.List.description),
107257
pair(Database.Create.helpCommand, Database.Create.description),
@@ -114,7 +264,7 @@ public static String getHelpMenu() {
114264
return Utils.buildHelpMenu(menu);
115265
}
116266

117-
public static ReplCommand getCommand(LineReader reader, Printer printer, String prompt) throws InterruptedException {
267+
static ReplCommand getCommand(LineReader reader, Printer printer, String prompt) throws InterruptedException {
118268
ReplCommand command = null;
119269
while (command == null) {
120270
String line = Utils.readNonEmptyLine(reader, prompt);

0 commit comments

Comments
 (0)