Skip to main content

MemoryStore

The MemoryStore class is simply an in-memory storage location for configuration files.

There is no hard persistence and is used primarily for testing purposes. However, the implementation provides a good idea on how stores work (especially the write method):

public class MemoryStore extends ConfigStore {

// Some methods ommitted.

private final ConcurrentHashMap cache = new ConcurrentHashMap();

@Override /* ConfigStore */
public synchronized String read(String name) {
return emptyIfNull(cache.get(name));
}

@Override /* ConfigStore */
public synchronized String write(String name, String expectedContents, String newContents) {

// This is a no-op.
if (isEquals(expectedContents, newContents))
return null;

String currentContents = read(name);

if (expectedContents != null && ! isEquals(currentContents, expectedContents))
return currentContents;

update(name, newContents);

// Success!
return null;
}

@Override /* ConfigStore */
public synchronized MemoryStore update(String name, String newContents) {
cache.put(name, newContents);
super.update(name, newContents); // Trigger any listeners.
return this;
}
}