public override IPreferenceArray GetPreferencesForItem(long itemID) { if (tempPrefs.Count == 0) { return(getDelegate().GetPreferencesForItem(itemID)); } IPreferenceArray delegatePrefs = null; try { delegatePrefs = getDelegate().GetPreferencesForItem(itemID); } catch (NoSuchItemException nsie) { // OK. Probably an item that only the anonymous user has //if (log.isDebugEnabled()) { // log.debug("Item {} unknown", itemID); //} } List <IPreference> anonymousPreferences = new List <IPreference>(); foreach (var prefsMap in tempPrefs) { IPreferenceArray singleUserTempPrefs = prefsMap.Value; for (int i = 0; i < singleUserTempPrefs.Length(); i++) { if (singleUserTempPrefs.GetItemID(i) == itemID) { anonymousPreferences.Add(singleUserTempPrefs.Get(i)); } } } int delegateLength = delegatePrefs == null ? 0 : delegatePrefs.Length(); int anonymousPrefsLength = anonymousPreferences.Count; int prefsCounter = 0; // Merge the delegate and anonymous preferences into a single array IPreferenceArray newPreferenceArray = new GenericItemPreferenceArray(delegateLength + anonymousPrefsLength); for (int i = 0; i < delegateLength; i++) { newPreferenceArray.Set(prefsCounter++, delegatePrefs.Get(i)); } foreach (IPreference anonymousPreference in anonymousPreferences) { newPreferenceArray.Set(prefsCounter++, anonymousPreference); } if (newPreferenceArray.Length() == 0) { // No, didn't find it among the anonymous user prefs throw new NoSuchItemException(itemID); } return(newPreferenceArray); }
public void testUserID() { IPreferenceArray prefs = new GenericItemPreferenceArray(3); Assert.AreEqual(3, prefs.Length()); prefs.SetItemID(0, 1L); Assert.AreEqual(1L, prefs.GetItemID(0)); Assert.AreEqual(1L, prefs.GetItemID(1)); Assert.AreEqual(1L, prefs.GetItemID(2)); }
public void testItemID() { IPreferenceArray prefs = new GenericItemPreferenceArray(3); Assert.AreEqual(3, prefs.Length()); prefs.SetUserID(0, 1L); prefs.SetUserID(1, 2L); prefs.SetUserID(2, 3L); Assert.AreEqual(1L, prefs.GetUserID(0)); Assert.AreEqual(2L, prefs.GetUserID(1)); Assert.AreEqual(3L, prefs.GetUserID(2)); }
public void testSetValue() { IPreferenceArray prefs = new GenericItemPreferenceArray(3); Assert.AreEqual(3, prefs.Length()); prefs.SetValue(0, 1.0f); prefs.SetValue(1, 2.0f); prefs.SetValue(2, 3.0f); Assert.AreEqual(1.0f, prefs.GetValue(0), EPSILON); Assert.AreEqual(2.0f, prefs.GetValue(1), EPSILON); Assert.AreEqual(3.0f, prefs.GetValue(2), EPSILON); }
public override IPreferenceArray GetPreferencesForItem(long itemID) { if (tempPrefs.Count==0) { return getDelegate().GetPreferencesForItem(itemID); } IPreferenceArray delegatePrefs = null; try { delegatePrefs = getDelegate().GetPreferencesForItem(itemID); } catch (NoSuchItemException nsie) { // OK. Probably an item that only the anonymous user has //if (log.isDebugEnabled()) { // log.debug("Item {} unknown", itemID); //} } List<IPreference> anonymousPreferences = new List<IPreference>(); foreach (var prefsMap in tempPrefs) { IPreferenceArray singleUserTempPrefs = prefsMap.Value; for (int i = 0; i < singleUserTempPrefs.Length(); i++) { if (singleUserTempPrefs.GetItemID(i) == itemID) { anonymousPreferences.Add(singleUserTempPrefs.Get(i)); } } } int delegateLength = delegatePrefs == null ? 0 : delegatePrefs.Length(); int anonymousPrefsLength = anonymousPreferences.Count; int prefsCounter = 0; // Merge the delegate and anonymous preferences into a single array IPreferenceArray newPreferenceArray = new GenericItemPreferenceArray(delegateLength + anonymousPrefsLength); for (int i = 0; i < delegateLength; i++) { newPreferenceArray.Set(prefsCounter++, delegatePrefs.Get(i)); } foreach (IPreference anonymousPreference in anonymousPreferences) { newPreferenceArray.Set(prefsCounter++, anonymousPreference); } if (newPreferenceArray.Length() == 0) { // No, didn't find it among the anonymous user prefs throw new NoSuchItemException(itemID); } return newPreferenceArray; }