@Override public String put(String key, String value) throws IllegalArgumentException { if (key == null || key.isEmpty() || key.contains("\n") || key.trim().isEmpty()) { throw new IllegalArgumentException(table.getTableName() + ": put: key is null"); } if (value == null || value.isEmpty() || value.trim().isEmpty() || value.contains("\n") || value.contains(" ")) { throw new IllegalArgumentException(table.getTableName() + ": put: value is null"); } String result; try { result = table.put(key, value); } catch (InvalidWorkingException e) { throw new IllegalArgumentException(e.getMessage()); } if (values.containsKey(key) && value.equals(values.get(key))) { changes.remove(key); } else { changes.put(key, value); } return result; }
@Override public String get(String key) throws IllegalArgumentException { if (key == null || key.isEmpty() || key.contains("\n") || key.trim().isEmpty()) { throw new IllegalArgumentException(table.getTableName() + ": get: key is null"); } return table.get(key); }
@Override public void open() throws IOException { table.open(); for (String key : table.keySet()) { values.put(key, table.get(key)); } }
public void rollChanges() { String oldValue; String newValue; for (String key : changes.keySet()) { oldValue = values.get(key); newValue = changes.get(key); if (oldValue != null && !oldValue.equals(newValue)) { try { table.put(key, oldValue); } catch (InvalidWorkingException e) { System.err.println( "Can not roll back key = \'" + key + "\' and value = \'" + newValue + "\'"); } } else if (oldValue == null && newValue != null) { try { table.remove(key); } catch (InvalidWorkingException e) { System.err.println( "Can not roll back deleting key = \'" + key + "\' and value = \'" + oldValue + "\'"); } } } }
@Override public int size() { return table.size(); }
@Override public void close() throws IOException { table.close(); changes.clear(); }
@Override public String getName() { return table.getTableName(); }