package net.sourceforge.tuned; import java.util.AbstractList; import java.util.ArrayList; import java.util.List; import java.util.prefs.Preferences; import net.sourceforge.tuned.PreferencesMap.Adapter; public class PreferencesList extends AbstractList { private final PreferencesMap prefs; public PreferencesList(PreferencesMap 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) { prefs.put(key(size()), e); return true; } @Override public T remove(int index) { int lastIndex = size() - 1; List shiftList = new ArrayList(subList(index, lastIndex + 1)); T value = shiftList.remove(0); prefs.remove(key(lastIndex)); for (T element : shiftList) { set(index, element); index++; } return value; } @Override public T set(int index, T element) { if (index < 0 || index >= size()) throw new IndexOutOfBoundsException(); return prefs.put(key(index), element); } @Override public void clear() { prefs.clear(); } public void set(List data) { clear(); addAll(data); } public static PreferencesList map(Preferences prefs, Class type) { return new PreferencesList(PreferencesMap.map(prefs, type)); } public static PreferencesList map(Preferences prefs, Adapter adapter) { return new PreferencesList(PreferencesMap.map(prefs, adapter)); } }