Skip to content

Commit

Permalink
Implement In-Memory Preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
kasemir committed Jun 12, 2024
1 parent 3fafd88 commit a633b75
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*******************************************************************************
* Copyright (c) 2024 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.phoebus.framework.preferences;

import java.util.concurrent.ConcurrentHashMap;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/** Java Preferences that are held in memory, not persisted
* @author Kay Kasemir
*/
class InMemoryPreferences extends AbstractPreferences
{
/** Preferences for both "user" and "system" */
private static final InMemoryPreferences prefs = new InMemoryPreferences(null, "");

/** Settings for this node in the preferences hierarchy */
private ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();

/** @return User preferences */
public static Preferences getUserRoot()
{
return prefs;
}

/** @return System preferences */
public static Preferences getSystemRoot()
{
return prefs;
}

InMemoryPreferences(final InMemoryPreferences parent, final String name)
{
super(parent, name);
}

/** @inheritDoc */
@Override
protected void putSpi(String key, String value)
{
cache.put(key, value);
}

/** @inheritDoc */
@Override
protected String getSpi(String key)
{
return cache.get(key);
}

/** @inheritDoc */
@Override
protected void removeSpi(String key)
{
cache.remove(key);
}

/** @inheritDoc */
@Override
protected void removeNodeSpi() throws BackingStoreException
{
// Nothing to remove in the file system
cache.clear();
}

/** @inheritDoc */
@Override
protected String[] keysSpi() throws BackingStoreException
{
return cache.keySet().toArray(new String[cache.size()]);
}

/** @inheritDoc */
@Override
protected String[] childrenNamesSpi() throws BackingStoreException
{
// This method need not return the names of any nodes already cached
// by the AbstractPreferences
return new String[0];
}

/** @inheritDoc */
@Override
protected AbstractPreferences childSpi(String name)
{
// AbstractPreferences guaranteed that the named node has not been returned
// by a previous invocation of this method or {@link #getChild(String)},
// so only called once and can then create the one and only new child
return new InMemoryPreferences(this, name);
}

/** @inheritDoc */
@Override
protected void syncSpi() throws BackingStoreException
{
// Nothing to sync
}

/** @inheritDoc */
@Override
protected void flushSpi() throws BackingStoreException
{
// Nothing to sync
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public static Preferences userNodeForClass(final Class<?> clazz)
@Override
public Preferences userRoot()
{
return FileSystemPreferences.getUserRoot();
return InMemoryPreferences.getUserRoot();
}

@Override
public Preferences systemRoot()
{
return FileSystemPreferences.getSystemRoot();
return InMemoryPreferences.getSystemRoot();
}

}

0 comments on commit a633b75

Please sign in to comment.