2008-06-09 18:36:05 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.AbstractList;
|
|
|
|
import java.util.ArrayList;
|
2009-07-18 22:06:32 +00:00
|
|
|
import java.util.Collection;
|
2008-06-09 18:36:05 +00:00
|
|
|
import java.util.List;
|
2009-07-18 22:06:32 +00:00
|
|
|
import java.util.RandomAccess;
|
2008-06-09 18:36:05 +00:00
|
|
|
import java.util.prefs.Preferences;
|
|
|
|
|
|
|
|
import net.sourceforge.tuned.PreferencesMap.Adapter;
|
|
|
|
|
|
|
|
|
2009-07-18 22:06:32 +00:00
|
|
|
public class PreferencesList<T> extends AbstractList<T> implements RandomAccess {
|
2008-06-09 18:36:05 +00:00
|
|
|
|
|
|
|
private final PreferencesMap<T> prefs;
|
|
|
|
|
2009-07-18 22:06:32 +00:00
|
|
|
|
2008-06-09 18:36:05 +00:00
|
|
|
public PreferencesList(PreferencesMap<T> preferencesMap) {
|
|
|
|
this.prefs = preferencesMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T get(int index) {
|
|
|
|
return prefs.get(key(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String key(int index) {
|
|
|
|
return Integer.toString(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size() {
|
|
|
|
return prefs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean add(T e) {
|
2008-06-29 17:38:57 +00:00
|
|
|
setImpl(size(), e);
|
2008-06-09 18:36:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2008-06-29 17:38:57 +00:00
|
|
|
public void add(int index, T element) {
|
2008-07-30 22:37:01 +00:00
|
|
|
int size = size();
|
|
|
|
|
|
|
|
if (index > size)
|
|
|
|
throw new IndexOutOfBoundsException(String.format("Index: %d, Size: %d", index, size));
|
|
|
|
|
|
|
|
copy(index, index + 1, size - index);
|
2008-06-09 18:36:05 +00:00
|
|
|
|
2008-06-29 17:38:57 +00:00
|
|
|
setImpl(index, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private T setImpl(int index, T element) {
|
|
|
|
return prefs.put(key(index), element);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return always null
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public T remove(int index) {
|
2008-06-09 18:36:05 +00:00
|
|
|
int lastIndex = size() - 1;
|
|
|
|
|
2008-06-29 17:38:57 +00:00
|
|
|
copy(index + 1, index, lastIndex - index);
|
2008-06-09 18:36:05 +00:00
|
|
|
prefs.remove(key(lastIndex));
|
|
|
|
|
2008-06-29 17:38:57 +00:00
|
|
|
return null;
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T set(int index, T element) {
|
|
|
|
if (index < 0 || index >= size())
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
|
2008-06-29 17:38:57 +00:00
|
|
|
return setImpl(index, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void copy(int startIndex, int newStartIndex, int count) {
|
|
|
|
if (count == 0 || startIndex == newStartIndex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<T> copy = new ArrayList<T>(subList(startIndex, startIndex + count));
|
|
|
|
|
|
|
|
for (int i = newStartIndex, n = 0; n < count; i++, n++) {
|
|
|
|
setImpl(i, copy.get(n));
|
|
|
|
}
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-18 22:06:32 +00:00
|
|
|
public void trimToSize(int limit) {
|
|
|
|
for (int i = size() - 1; i >= limit; i--) {
|
|
|
|
remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void set(Collection<T> data) {
|
|
|
|
// remove all elements beyond data.size
|
|
|
|
trimToSize(data.size());
|
|
|
|
|
|
|
|
// override elements
|
|
|
|
int i = 0;
|
|
|
|
for (T element : data) {
|
|
|
|
setImpl(i++, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 18:36:05 +00:00
|
|
|
@Override
|
|
|
|
public void clear() {
|
|
|
|
prefs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-08 18:41:11 +00:00
|
|
|
public static PreferencesList<String> map(Preferences prefs) {
|
|
|
|
return new PreferencesList<String>(PreferencesMap.map(prefs));
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> PreferencesList<T> map(Preferences prefs, Adapter<T> adapter) {
|
|
|
|
return new PreferencesList<T>(PreferencesMap.map(prefs, adapter));
|
|
|
|
}
|
2008-07-30 22:37:01 +00:00
|
|
|
|
2008-06-09 18:36:05 +00:00
|
|
|
}
|