@Override
public PreferenceArray getPreferencesForItem(long itemID) throws TasteException {
if (tempPrefs == null) {
return delegate.getPreferencesForItem(itemID);
}
PreferenceArray delegatePrefs = null;
try {
delegatePrefs = delegate.getPreferencesForItem(itemID);
} catch (NoSuchItemException nsie) {
// OK. Probably an item that only the anonymous user has
}
for (int i = 0; i < tempPrefs.length(); i++) {
if (tempPrefs.getItemID(i) == itemID) {
int length = delegatePrefs == null ? 0 : delegatePrefs.length();
PreferenceArray newPreferenceArray = new GenericItemPreferenceArray(length + 1);
for (int j = 0; j < length; j++) {
newPreferenceArray.setUserID(j, delegatePrefs.getUserID(j));
newPreferenceArray.setItemID(j, delegatePrefs.getItemID(j));
newPreferenceArray.setValue(j, delegatePrefs.getValue(j));
}
newPreferenceArray.setUserID(length, tempPrefs.getUserID(i));
newPreferenceArray.setItemID(length, tempPrefs.getItemID(i));
newPreferenceArray.setValue(length, tempPrefs.getValue(i));
newPreferenceArray.sortByUser();
return newPreferenceArray;
}
}
if (delegatePrefs == null) {
// No, didn't find it among the anonymous user prefs