1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package edu.caltech.nanodb.commands;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import edu.caltech.nanodb.server.NanoDBServer;
import edu.caltech.nanodb.server.properties.PropertyRegistry;
/**
* Implements the "SHOW VARIABLES" command.
*/
public class ShowPropertiesCommand extends Command {
private String filter = null;
public ShowPropertiesCommand() {
super(Command.Type.UTILITY);
}
public void setFilter(String filter) {
this.filter = filter;
}
@Override
public void execute(NanoDBServer server) throws ExecutionException {
PropertyRegistry propReg = server.getPropertyRegistry();
ArrayList<String> propertyNames =
new ArrayList<>(propReg.getAllPropertyNames());
Collections.sort(propertyNames);
ArrayList<String> values = new ArrayList<>();
int maxNameLength = 0;
int maxValueLength = 0;
for (String name : propertyNames) {
Object value = propReg.getPropertyValue(name);
String valueStr = (value != null ? value.toString() : null);
values.add(valueStr);
if (name.length() > maxNameLength)
maxNameLength = name.length();
if (valueStr == null) {
if (maxValueLength < 4)
maxValueLength = 4;
}
else if (valueStr.length() > maxValueLength) {
maxValueLength = valueStr.length();
}
}
String formatStr = String.format("| %%-%ds | %%%ds |%%n",
maxNameLength, maxValueLength);
char[] lines = new char[maxNameLength + maxValueLength + 7];
Arrays.fill(lines, '-');
lines[0] = '+';
lines[lines.length - 1] = '+';
lines[maxNameLength + 3] = '+';
String lineStr = new String(lines);
out.println(lineStr);
out.printf(formatStr, "PROPERTY NAME", "VALUE");
out.println(lineStr);
for (int i = 0; i < propertyNames.size(); i++) {
String name = propertyNames.get(i);
String valueStr = values.get(i);
out.printf(formatStr, name, valueStr);
}
out.println(lineStr);
}
}