1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-17 23:05:03 -05:00
filebot/source/net/sourceforge/tuned/PreferencesMap.java

348 lines
6.6 KiB
Java
Raw Normal View History

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;
2011-12-28 08:51:36 -05:00
public PreferencesMap(Preferences prefs, Adapter<T> adapter) {
this.prefs = prefs;
this.adapter = adapter;
}
2011-12-28 08:51:36 -05:00
@Override
public T get(Object key) {
return adapter.get(prefs, key.toString());
}
2011-12-28 08:51:36 -05:00
@Override
public T put(String key, T value) {
adapter.put(prefs, key, value);
// don't know previous entry
return null;
}
2011-12-28 08:51:36 -05:00
@Override
public T remove(Object key) {
adapter.remove(prefs, key.toString());
// don't know removed entry
return null;
}
2011-12-28 08:51:36 -05:00
public String[] keys() {
try {
return adapter.keys(prefs);
} catch (BackingStoreException e) {
throw new RuntimeException(e);
}
}
2011-12-28 08:51:36 -05:00
@Override
public void clear() {
for (String key : keys()) {
adapter.remove(prefs, key);
}
}
2011-12-28 08:51:36 -05:00
@Override
public boolean containsKey(Object key) {
if (key instanceof String) {
return Arrays.asList(keys()).contains(key);
}
return false;
}
2011-12-28 08:51:36 -05:00
@Override
public boolean containsValue(Object value) {
for (String key : keys()) {
if (value.equals(get(key)))
return true;
}
return false;
}
2011-12-28 08:51:36 -05:00
@Override
public Set<Entry<String, T>> entrySet() {
Set<Map.Entry<String, T>> entries = new LinkedHashSet<Map.Entry<String, T>>();
for (String key : keys()) {
entries.add(new PreferencesEntry<T>(prefs, key, adapter));
}
return entries;
}
2011-12-28 08:51:36 -05:00
@Override
public boolean isEmpty() {
return size() == 0;
}
2011-12-28 08:51:36 -05:00
@Override
public Set<String> keySet() {
return new LinkedHashSet<String>(Arrays.asList(keys()));
}
2011-12-28 08:51:36 -05:00
@Override
public void putAll(Map<? extends String, ? extends T> map) {
for (Map.Entry<? extends String, ? extends T> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
2011-12-28 08:51:36 -05:00
@Override
public int size() {
return keys().length;
}
2011-12-28 08:51:36 -05:00
@Override
public Collection<T> values() {
List<T> values = new ArrayList<T>();
for (String key : keys()) {
values.add(get(key));
}
return values;
}
2011-12-28 08:51:36 -05:00
public static PreferencesMap<String> map(Preferences prefs) {
return map(prefs, new StringAdapter());
}
2011-12-28 08:51:36 -05:00
public static <T> PreferencesMap<T> map(Preferences prefs, Adapter<T> adapter) {
return new PreferencesMap<T>(prefs, adapter);
}
2011-12-28 08:51:36 -05:00
public static interface Adapter<T> {
public String[] keys(Preferences prefs) throws BackingStoreException;
2011-12-28 08:51:36 -05:00
public T get(Preferences prefs, String key);
2011-12-28 08:51:36 -05:00
public void put(Preferences prefs, String key, T value);
2011-12-28 08:51:36 -05:00
public void remove(Preferences prefs, String key);
}
2011-12-28 08:51:36 -05:00
public static abstract class AbstractAdapter<T> implements Adapter<T> {
@Override
public abstract T get(Preferences prefs, String key);
2011-12-28 08:51:36 -05:00
@Override
public abstract void put(Preferences prefs, String key, T value);
2011-12-28 08:51:36 -05:00
@Override
public String[] keys(Preferences prefs) throws BackingStoreException {
return prefs.keys();
}
2011-12-28 08:51:36 -05:00
@Override
public void remove(Preferences prefs, String key) {
prefs.remove(key);
}
}
2011-12-28 08:51:36 -05:00
public static class StringAdapter extends AbstractAdapter<String> {
@Override
public String get(Preferences prefs, String key) {
return prefs.get(key, null);
}
2011-12-28 08:51:36 -05:00
@Override
public void put(Preferences prefs, String key, String value) {
prefs.put(key, value);
}
}
2011-12-28 08:51:36 -05:00
public static class SimpleAdapter<T> extends AbstractAdapter<T> {
private final Constructor<T> constructor;
2011-12-28 08:51:36 -05:00
public SimpleAdapter(Class<T> type) {
try {
constructor = type.getConstructor(String.class);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
2011-12-28 08:51:36 -05:00
@Override
public T get(Preferences prefs, String key) {
String value = prefs.get(key, null);
if (value != null) {
try {
return constructor.newInstance(value);
} catch (InvocationTargetException e) {
// try to throw the cause directly, e.g. NumberFormatException
throw ExceptionUtilities.asRuntimeException(e.getCause());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return null;
}
2011-12-28 08:51:36 -05:00
@Override
public void put(Preferences prefs, String key, T value) {
prefs.put(key, value.toString());
}
2011-12-28 08:51:36 -05:00
public static <T> SimpleAdapter<T> forClass(Class<T> type) {
return new SimpleAdapter<T>(type);
}
}
2011-12-28 08:51:36 -05:00
public static class SerializableAdapter<T extends Serializable> extends AbstractAdapter<T> {
@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);
}
}
2011-12-28 08:51:36 -05:00
@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);
}
}
}
2011-12-28 08:51:36 -05:00
public static class PreferencesEntry<T> implements Entry<String, T> {
private final String key;
private final Preferences prefs;
private final Adapter<T> adapter;
2011-12-28 08:51:36 -05:00
private T defaultValue = null;
public PreferencesEntry(Preferences prefs, String key, Adapter<T> adapter) {
this.key = key;
this.prefs = prefs;
this.adapter = adapter;
}
2011-12-28 08:51:36 -05:00
@Override
public String getKey() {
return key;
}
2011-12-28 08:51:36 -05:00
@Override
public T getValue() {
2011-12-28 08:51:36 -05:00
T value = adapter.get(prefs, key);
return value != null ? value : defaultValue;
}
2011-12-28 08:51:36 -05:00
@Override
public T setValue(T value) {
adapter.put(prefs, key, value);
return null;
}
2011-12-28 08:51:36 -05:00
public PreferencesEntry<T> defaultValue(T defaultValue) {
this.defaultValue = defaultValue;
return this;
}
2011-12-28 08:51:36 -05:00
public void remove() {
adapter.remove(prefs, key);
}
}
}