|
1 | 1 | package org.dfinity.ic.burp.storage; |
2 | 2 |
|
3 | 3 | import burp.api.montoya.persistence.Preferences; |
| 4 | +import org.dfinity.ic.burp.model.PreferenceType; |
4 | 5 |
|
5 | 6 | import java.util.HashMap; |
| 7 | +import java.util.HashSet; |
6 | 8 | import java.util.Map; |
7 | 9 | import java.util.Objects; |
8 | 10 | import java.util.Optional; |
|
12 | 14 | import java.util.stream.Collectors; |
13 | 15 |
|
14 | 16 | public class HierarchicPreferences implements Preferences { |
15 | | - private static final String KEY_SEPARATOR = "#"; |
16 | | - private static final String TYPE_VALUE_SEPARATOR = "$"; |
17 | | - private static final String RESERVED_CHARS = KEY_SEPARATOR + TYPE_VALUE_SEPARATOR; |
18 | | - private static final String BOOLEAN_TYPE = "Boolean"; |
19 | | - private static final String BYTE_TYPE = "Byte"; |
20 | | - private static final String INTEGER_TYPE = "Integer"; |
21 | | - private static final String LONG_TYPE = "Long"; |
22 | | - private static final String SHORT_TYPE = "Short"; |
23 | | - private static final String STRING_TYPE = "String"; |
24 | | - private static final String CHILD_TYPE = "Child"; |
| 17 | + public static final String KEY_SEPARATOR = "#"; |
| 18 | + public static final String TYPE_VALUE_SEPARATOR = "$"; |
| 19 | + public static final String RESERVED_CHARS = KEY_SEPARATOR + TYPE_VALUE_SEPARATOR; |
| 20 | + public static final String BOOLEAN_TYPE = "Boolean"; |
| 21 | + public static final String BYTE_TYPE = "Byte"; |
| 22 | + public static final String INTEGER_TYPE = "Integer"; |
| 23 | + public static final String LONG_TYPE = "Long"; |
| 24 | + public static final String SHORT_TYPE = "Short"; |
| 25 | + public static final String STRING_TYPE = "String"; |
| 26 | + public static final String CHILD_TYPE = "Child"; |
25 | 27 | private final Map<String, Integer> integers = new HashMap<>(); |
26 | 28 | private final Map<String, Boolean> booleans = new HashMap<>(); |
27 | 29 | private final Map<String, Byte> bytes = new HashMap<>(); |
@@ -235,12 +237,14 @@ private void storeInternal(Preferences preferences, String prefix) { |
235 | 237 | } |
236 | 238 | } |
237 | 239 |
|
238 | | - public void store(Preferences preferences, String key) { |
| 240 | + public Map<PreferenceType, Set<String>> store(Preferences preferences, String key) { |
239 | 241 | assertValid(key); |
240 | 242 | if (isEmpty()) |
241 | 243 | throw new RuntimeException("trying to store empty object"); |
242 | 244 |
|
243 | | - storeInternal(preferences, key); |
| 245 | + WrappedKeyTrackingPreferences wrappedPref = new WrappedKeyTrackingPreferences(preferences); |
| 246 | + storeInternal(wrappedPref, key); |
| 247 | + return wrappedPref.getKeysByType(); |
244 | 248 | } |
245 | 249 |
|
246 | 250 | private void loadInternal(Preferences preferences, String prefix) { |
@@ -282,4 +286,152 @@ public boolean equals(Object other) { |
282 | 286 | public int hashCode() { |
283 | 287 | return Objects.hash(booleans, bytes, integers, longs, shorts, strings, children); |
284 | 288 | } |
| 289 | + |
| 290 | + private static class WrappedKeyTrackingPreferences implements Preferences { |
| 291 | + private final Preferences preferences; |
| 292 | + private final Map<PreferenceType, Set<String>> keysByType = new HashMap<>(); |
| 293 | + |
| 294 | + public WrappedKeyTrackingPreferences(Preferences preferences) { |
| 295 | + this.preferences = preferences; |
| 296 | + for (var type : PreferenceType.values()) { |
| 297 | + keysByType.put(type, new HashSet<>()); |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + public Map<PreferenceType, Set<String>> getKeysByType() { |
| 302 | + return keysByType; |
| 303 | + } |
| 304 | + |
| 305 | + @Override |
| 306 | + public String getString(String s) { |
| 307 | + return preferences.getString(s); |
| 308 | + } |
| 309 | + |
| 310 | + @Override |
| 311 | + public void setString(String s, String s1) { |
| 312 | + keysByType.get(PreferenceType.STRING).add(s); |
| 313 | + preferences.setString(s, s1); |
| 314 | + } |
| 315 | + |
| 316 | + @Override |
| 317 | + public void deleteString(String s) { |
| 318 | + keysByType.get(PreferenceType.STRING).remove(s); |
| 319 | + preferences.deleteString(s); |
| 320 | + } |
| 321 | + |
| 322 | + @Override |
| 323 | + public Set<String> stringKeys() { |
| 324 | + return preferences.stringKeys(); |
| 325 | + } |
| 326 | + |
| 327 | + @Override |
| 328 | + public Boolean getBoolean(String s) { |
| 329 | + return preferences.getBoolean(s); |
| 330 | + } |
| 331 | + |
| 332 | + @Override |
| 333 | + public void setBoolean(String s, boolean b) { |
| 334 | + keysByType.get(PreferenceType.BOOLEAN).add(s); |
| 335 | + preferences.setBoolean(s, b); |
| 336 | + } |
| 337 | + |
| 338 | + @Override |
| 339 | + public void deleteBoolean(String s) { |
| 340 | + keysByType.get(PreferenceType.BOOLEAN).remove(s); |
| 341 | + preferences.deleteBoolean(s); |
| 342 | + } |
| 343 | + |
| 344 | + @Override |
| 345 | + public Set<String> booleanKeys() { |
| 346 | + return preferences.booleanKeys(); |
| 347 | + } |
| 348 | + |
| 349 | + @Override |
| 350 | + public Byte getByte(String s) { |
| 351 | + return preferences.getByte(s); |
| 352 | + } |
| 353 | + |
| 354 | + @Override |
| 355 | + public void setByte(String s, byte b) { |
| 356 | + keysByType.get(PreferenceType.BYTE).add(s); |
| 357 | + preferences.setByte(s, b); |
| 358 | + } |
| 359 | + |
| 360 | + @Override |
| 361 | + public void deleteByte(String s) { |
| 362 | + keysByType.get(PreferenceType.BYTE).remove(s); |
| 363 | + preferences.deleteByte(s); |
| 364 | + } |
| 365 | + |
| 366 | + @Override |
| 367 | + public Set<String> byteKeys() { |
| 368 | + return preferences.byteKeys(); |
| 369 | + } |
| 370 | + |
| 371 | + @Override |
| 372 | + public Short getShort(String s) { |
| 373 | + return preferences.getShort(s); |
| 374 | + } |
| 375 | + |
| 376 | + @Override |
| 377 | + public void setShort(String s, short i) { |
| 378 | + keysByType.get(PreferenceType.SHORT).add(s); |
| 379 | + preferences.setShort(s, i); |
| 380 | + } |
| 381 | + |
| 382 | + @Override |
| 383 | + public void deleteShort(String s) { |
| 384 | + keysByType.get(PreferenceType.SHORT).remove(s); |
| 385 | + preferences.deleteShort(s); |
| 386 | + } |
| 387 | + |
| 388 | + @Override |
| 389 | + public Set<String> shortKeys() { |
| 390 | + return preferences.shortKeys(); |
| 391 | + } |
| 392 | + |
| 393 | + @Override |
| 394 | + public Integer getInteger(String s) { |
| 395 | + return preferences.getInteger(s); |
| 396 | + } |
| 397 | + |
| 398 | + @Override |
| 399 | + public void setInteger(String s, int i) { |
| 400 | + keysByType.get(PreferenceType.INTEGER).add(s); |
| 401 | + preferences.setInteger(s, i); |
| 402 | + } |
| 403 | + |
| 404 | + @Override |
| 405 | + public void deleteInteger(String s) { |
| 406 | + keysByType.get(PreferenceType.INTEGER).remove(s); |
| 407 | + preferences.deleteInteger(s); |
| 408 | + } |
| 409 | + |
| 410 | + @Override |
| 411 | + public Set<String> integerKeys() { |
| 412 | + return preferences.integerKeys(); |
| 413 | + } |
| 414 | + |
| 415 | + @Override |
| 416 | + public Long getLong(String s) { |
| 417 | + return preferences.getLong(s); |
| 418 | + } |
| 419 | + |
| 420 | + @Override |
| 421 | + public void setLong(String s, long l) { |
| 422 | + keysByType.get(PreferenceType.LONG).add(s); |
| 423 | + preferences.setLong(s, l); |
| 424 | + } |
| 425 | + |
| 426 | + @Override |
| 427 | + public void deleteLong(String s) { |
| 428 | + keysByType.get(PreferenceType.LONG).remove(s); |
| 429 | + preferences.deleteLong(s); |
| 430 | + } |
| 431 | + |
| 432 | + @Override |
| 433 | + public Set<String> longKeys() { |
| 434 | + return preferences.longKeys(); |
| 435 | + } |
| 436 | + } |
285 | 437 | } |
0 commit comments