2008-06-09 18:36:05 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.prefs.BackingStoreException;
|
|
|
|
import java.util.prefs.Preferences;
|
|
|
|
|
|
|
|
|
|
|
|
public class PreferencesMap<T> implements Map<String, T> {
|
|
|
|
|
|
|
|
private final Preferences prefs;
|
|
|
|
private final Adapter<T> adapter;
|
|
|
|
|
|
|
|
|
|
|
|
public PreferencesMap(Preferences prefs, Adapter<T> adapter) {
|
|
|
|
this.prefs = prefs;
|
|
|
|
this.adapter = adapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T get(Object key) {
|
|
|
|
if (key instanceof String) {
|
|
|
|
return adapter.get(prefs, (String) key);
|
|
|
|
}
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
return null;
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T put(String key, T value) {
|
|
|
|
adapter.put(prefs, key, value);
|
|
|
|
|
2009-03-08 18:41:11 +00:00
|
|
|
return null;
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T remove(Object key) {
|
|
|
|
if (key instanceof String) {
|
2008-06-21 19:24:18 +00:00
|
|
|
adapter.remove(prefs, (String) key);
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String[] keys() {
|
|
|
|
try {
|
2008-06-21 19:24:18 +00:00
|
|
|
return adapter.keys(prefs);
|
2008-06-09 18:36:05 +00:00
|
|
|
} catch (BackingStoreException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear() {
|
2008-06-21 19:24:18 +00:00
|
|
|
for (String key : keys()) {
|
|
|
|
adapter.remove(prefs, key);
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean containsKey(Object key) {
|
|
|
|
if (key instanceof String) {
|
|
|
|
return Arrays.asList(keys()).contains(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean containsValue(Object value) {
|
|
|
|
for (String key : keys()) {
|
|
|
|
if (value.equals(get(key)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<Map.Entry<String, T>> entrySet() {
|
|
|
|
Set<Map.Entry<String, T>> entries = new LinkedHashSet<Map.Entry<String, T>>();
|
|
|
|
|
|
|
|
for (String key : keys()) {
|
2009-03-08 18:41:11 +00:00
|
|
|
entries.add(new PreferencesEntry<T>(prefs, key, adapter));
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<String> keySet() {
|
2008-06-21 19:24:18 +00:00
|
|
|
return new LinkedHashSet<String>(Arrays.asList(keys()));
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void putAll(Map<? extends String, ? extends T> map) {
|
2008-07-30 22:37:01 +00:00
|
|
|
for (Map.Entry<? extends String, ? extends T> entry : map.entrySet()) {
|
|
|
|
put(entry.getKey(), entry.getValue());
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size() {
|
|
|
|
return keys().length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Collection<T> values() {
|
|
|
|
List<T> values = new ArrayList<T>();
|
|
|
|
|
|
|
|
for (String key : keys()) {
|
|
|
|
values.add(get(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-08 18:41:11 +00:00
|
|
|
public static PreferencesMap<String> map(Preferences prefs) {
|
|
|
|
return map(prefs, new StringAdapter());
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> PreferencesMap<T> map(Preferences prefs, Adapter<T> adapter) {
|
|
|
|
return new PreferencesMap<T>(prefs, adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static interface Adapter<T> {
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public String[] keys(Preferences prefs) throws BackingStoreException;
|
|
|
|
|
|
|
|
|
2008-06-09 18:36:05 +00:00
|
|
|
public T get(Preferences prefs, String key);
|
|
|
|
|
|
|
|
|
|
|
|
public void put(Preferences prefs, String key, T value);
|
2008-06-21 19:24:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void remove(Preferences prefs, String key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static abstract class AbstractAdapter<T> implements Adapter<T> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public abstract T get(Preferences prefs, String key);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public abstract void put(Preferences prefs, String key, T value);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] keys(Preferences prefs) throws BackingStoreException {
|
|
|
|
return prefs.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove(Preferences prefs, String key) {
|
|
|
|
prefs.remove(key);
|
|
|
|
}
|
|
|
|
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public static class StringAdapter extends AbstractAdapter<String> {
|
2008-06-09 18:36:05 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String get(Preferences prefs, String key) {
|
|
|
|
return prefs.get(key, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void put(Preferences prefs, String key, String value) {
|
|
|
|
prefs.put(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public static class SimpleAdapter<T> extends AbstractAdapter<T> {
|
2008-06-09 18:36:05 +00:00
|
|
|
|
|
|
|
private final Constructor<T> constructor;
|
|
|
|
|
|
|
|
|
|
|
|
public SimpleAdapter(Class<T> type) {
|
|
|
|
try {
|
|
|
|
constructor = type.getConstructor(String.class);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T get(Preferences prefs, String key) {
|
|
|
|
String stringValue = prefs.get(key, null);
|
|
|
|
|
|
|
|
if (stringValue == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return constructor.newInstance(stringValue);
|
|
|
|
} catch (InvocationTargetException e) {
|
2008-06-21 19:24:18 +00:00
|
|
|
// try to throw the cause directly, e.g. NumberFormatException
|
2009-02-09 20:56:20 +00:00
|
|
|
throw ExceptionUtilities.asRuntimeException(e.getCause());
|
2008-06-09 18:36:05 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void put(Preferences prefs, String key, T value) {
|
|
|
|
prefs.put(key, value.toString());
|
|
|
|
}
|
|
|
|
|
2009-03-08 18:41:11 +00:00
|
|
|
|
|
|
|
public static <T> SimpleAdapter<T> forClass(Class<T> type) {
|
|
|
|
return new SimpleAdapter<T>(type);
|
|
|
|
}
|
|
|
|
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public static class SerializableAdapter<T extends Serializable> extends AbstractAdapter<T> {
|
2008-06-09 18:36:05 +00:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public T get(Preferences prefs, String key) {
|
|
|
|
byte[] bytes = prefs.getByteArray(key, null);
|
|
|
|
|
|
|
|
if (bytes == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
|
|
|
|
Object object = in.readObject();
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
return (T) object;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void put(Preferences prefs, String key, T value) {
|
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
try {
|
|
|
|
ObjectOutputStream out = new ObjectOutputStream(buffer);
|
|
|
|
out.writeObject(value);
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
prefs.putByteArray(key, buffer.toByteArray());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-08 18:41:11 +00:00
|
|
|
|
|
|
|
public static class PreferencesEntry<T> implements Entry<String, T> {
|
|
|
|
|
|
|
|
private final String key;
|
|
|
|
|
|
|
|
private final Preferences prefs;
|
|
|
|
|
|
|
|
private final Adapter<T> adapter;
|
|
|
|
|
|
|
|
|
|
|
|
public PreferencesEntry(Preferences prefs, String key, Adapter<T> adapter) {
|
|
|
|
this.key = key;
|
|
|
|
this.prefs = prefs;
|
|
|
|
this.adapter = adapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getKey() {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T getValue() {
|
|
|
|
return adapter.get(prefs, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T setValue(T value) {
|
|
|
|
adapter.put(prefs, key, value);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|