Repository navigation
Expand file tree
/
Copy pathTable.java
More file actions
225 lines (199 loc) · 6.74 KB
/
Copy pathTable.java
File metadata and controls
225 lines (199 loc) · 6.74 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import java.util.ArrayList;
import java.util.List;
public class Table {
private List<String> fieldNames;
private List<String> fieldTypes;
private List<String> primaryKeys;
private List<String> secondaryKeys;
private String tableName;
private List<Row> rows;
public Table(String tableName, List<String> fieldNames,
List<String> fieldTypes, List<String> primaryKeys) {
this(tableName, fieldNames, fieldTypes, primaryKeys, new ArrayList<>());
}
public Table(String tableName, List<String> fieldNames,
List<String> fieldTypes, List<String> primaryKeys,
List<String> secondaryKeys) {
this.tableName = tableName;
this.fieldNames = new ArrayList<>(fieldNames);
this.fieldTypes = new ArrayList<>(fieldTypes);
this.primaryKeys = new ArrayList<>(primaryKeys);
this.secondaryKeys = new ArrayList<>(secondaryKeys);
this.rows = new ArrayList<>();
}
public String getTableName() { return tableName; }
public List<String> getFieldNames() { return fieldNames; }
public List<String> getFieldTypes() { return fieldTypes; }
public List<String> getPrimaryKeys() { return primaryKeys; }
public List<String> getSecondaryKeys() { return secondaryKeys; }
public List<Row> getRows() { return rows; }
public boolean isPrimaryKey(String fieldName) {
return primaryKeys.contains(fieldName);
}
public boolean isSecondaryKey(String fieldName) {
return secondaryKeys.contains(fieldName);
}
public boolean isKeyField(String fieldName) {
return isPrimaryKey(fieldName) || isSecondaryKey(fieldName);
}
private boolean hasDuplicateLongValue(String fieldName, Long value) {
for (Row row : rows) {
if (value.equals(row.getLong(fieldName))) {
return true;
}
}
return false;
}
private boolean hasDuplicateStringValue(String fieldName, String value) {
for (Row row : rows) {
if (value.equals(row.getString(fieldName))) {
return true;
}
}
return false;
}
public List<String> getMissingFields(Row row) {
List<String> missing = new ArrayList<>();
for (String field : fieldNames) {
if (!row.hasField(field)) {
missing.add(field);
}
}
return missing;
}
public String findDuplicatePrimaryKey(Row row) {
for (String pk : primaryKeys) {
String pkType = getFieldType(pk);
if (pkType.equals("LONG")) {
Long value = row.getLong(pk);
if (hasDuplicateLongValue(pk, value)) {
return pk;
}
} else if (pkType.equals("STRING")) {
String value = row.getString(pk);
if (hasDuplicateStringValue(pk, value)) {
return pk;
}
}
}
return null;
}
public boolean insert(Row row) {
if (findDuplicatePrimaryKey(row) != null) {
return false;
}
rows.add(row);
return true;
}
public Row getByStringKey(String keyField, String keyValue) {
if (!isKeyField(keyField)) {
return null;
}
for (Row row : rows) {
if (keyValue.equals(row.getString(keyField))) {
return row;
}
}
return null;
}
public Row getByLongKey(String keyField, Long keyValue) {
if (!isKeyField(keyField)) {
return null;
}
for (Row row : rows) {
if (keyValue.equals(row.getLong(keyField))) {
return row;
}
}
return null;
}
public boolean updateField(String keyField, Long keyValue,
String targetField, String newValue) {
Row row = getByLongKey(keyField, keyValue);
if (row == null) {
return false;
}
row.setString(targetField, newValue);
return true;
}
public boolean updateField(String keyField, Long keyValue,
String targetField, Long newValue) {
Row row = getByLongKey(keyField, keyValue);
if (row == null) {
return false;
}
row.setLong(targetField, newValue);
return true;
}
public boolean updateField(String keyField, String keyValue,
String targetField, String newValue) {
Row row = getByStringKey(keyField, keyValue);
if (row == null) {
return false;
}
row.setString(targetField, newValue);
return true;
}
public boolean updateField(String keyField, String keyValue,
String targetField, Long newValue) {
Row row = getByStringKey(keyField, keyValue);
if (row == null) {
return false;
}
row.setLong(targetField, newValue);
return true;
}
public boolean updateRow(String keyField, Long keyValue, Row newRow) {
for (int i = 0; i < rows.size(); i++) {
if (keyValue.equals(rows.get(i).getLong(keyField))) {
rows.set(i, newRow);
return true;
}
}
return false;
}
public int deleteByLongKey(String keyField, Long keyValue) {
if (!isKeyField(keyField)) {
return 0;
}
int before = rows.size();
rows.removeIf(r -> keyValue.equals(r.getLong(keyField)));
return before - rows.size();
}
public int deleteByStringKey(String keyField, String keyValue) {
if (!isKeyField(keyField)) {
return 0;
}
int before = rows.size();
rows.removeIf(r -> keyValue.equals(r.getString(keyField)));
return before - rows.size();
}
public void printAll() {
System.out.println("=== Table: " + tableName + " ===");
if (rows.isEmpty()) {
System.out.println(" (empty)");
} else {
for (Row row : rows) {
System.out.println(" " + row);
}
}
}
public List<String> getKeyValuesAsStrings(String keyField) {
List<String> values = new ArrayList<>();
for (Row row : rows) {
Object value = row.get(keyField);
if (value != null) {
values.add(value.toString());
}
}
return values;
}
public int size() { return rows.size(); }
public String getFieldType(String fieldName) {
int index = fieldNames.indexOf(fieldName);
if (index == -1) {
return null;
}
return fieldTypes.get(index);
}
}