PlusAnonymousConcurrentUserDataModel extends PlusAnonymousUserDataModel { @Override public PreferenceArray getPreferencesForItem(long itemID) throws TasteException { if (tempPrefs.isEmpty()) { return getDelegate().getPreferencesForItem(itemID); } PreferenceArray delegatePrefs = null; try { delegatePrefs = getDelegate().getPreferencesForItem(itemID); } catch (NoSuchItemException nsie) { if (log.isDebugEnabled()) { log.debug("Item {} unknown", itemID); } } List<Preference> anonymousPreferences = Lists.newArrayList(); for (Map.Entry<Long, PreferenceArray> prefsMap : tempPrefs.entrySet()) { PreferenceArray singleUserTempPrefs = prefsMap.getValue(); 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.size(); int prefsCounter = 0; PreferenceArray newPreferenceArray = new GenericItemPreferenceArray(delegateLength + anonymousPrefsLength); for (int i = 0; i < delegateLength; i++) { newPreferenceArray.set(prefsCounter++, delegatePrefs.get(i)); } for (Preference anonymousPreference : anonymousPreferences) { newPreferenceArray.set(prefsCounter++, anonymousPreference); } if (newPreferenceArray.length() == 0) { throw new NoSuchItemException(itemID); } return newPreferenceArray; } PlusAnonymousConcurrentUserDataModel(DataModel delegate, int maxConcurrentUsers); Long takeAvailableUser(); boolean releaseUser(Long userID); void setTempPrefs(PreferenceArray prefs, long anonymousUserID); void clearTempPrefs(long anonymousUserID); @Override LongPrimitiveIterator getUserIDs(); @Override PreferenceArray getPreferencesFromUser(long userID); @Override FastIDSet getItemIDsFromUser(long userID); @Override PreferenceArray getPreferencesForItem(long itemID); @Override Float getPreferenceValue(long userID, long itemID); @Override Long getPreferenceTime(long userID, long itemID); @Override int getNumUsers(); @Override int getNumUsersWithPreferenceFor(long itemID); @Override int getNumUsersWithPreferenceFor(long itemID1, long itemID2); @Override void setPreference(long userID, long itemID, float value); @Override void removePreference(long userID, long itemID);  }
@Test public void testGetPreferencesForItem() throws TasteException { PreferenceArray prefs = new GenericUserPreferenceArray(2); long sampleUserID = 4; prefs.setUserID(0, sampleUserID); long sampleItemID = 11; prefs.setItemID(0, sampleItemID); prefs.setUserID(1, sampleUserID); long sampleItemID2 = 22; prefs.setItemID(1, sampleItemID2); FastByIDMap<PreferenceArray> delegatePreferences = new FastByIDMap<PreferenceArray>(); delegatePreferences.put(sampleUserID, prefs); PlusAnonymousConcurrentUserDataModel instance = getTestableWithDelegateData(10, delegatePreferences); Long anonymousUserID = instance.takeAvailableUser(); PreferenceArray tempPrefs = new GenericUserPreferenceArray(2); tempPrefs.setUserID(0, anonymousUserID); tempPrefs.setItemID(0, sampleItemID); tempPrefs.setUserID(1, anonymousUserID); long sampleItemID3 = 33; tempPrefs.setItemID(1, sampleItemID3); instance.setTempPrefs(tempPrefs, anonymousUserID); assertEquals(sampleUserID, instance.getPreferencesForItem(sampleItemID).get(0).getUserID()); assertEquals(2, instance.getPreferencesForItem(sampleItemID).length()); assertEquals(1, instance.getPreferencesForItem(sampleItemID2).length()); assertEquals(1, instance.getPreferencesForItem(sampleItemID3).length()); assertEquals(2, instance.getNumUsersWithPreferenceFor(sampleItemID)); assertEquals(1, instance.getNumUsersWithPreferenceFor(sampleItemID, sampleItemID2)); assertEquals(1, instance.getNumUsersWithPreferenceFor(sampleItemID, sampleItemID3)); }
@Test public void testGetPreferencesForItem() throws TasteException { PreferenceArray prefs = new GenericUserPreferenceArray(2); long sampleUserID = 4; prefs.setUserID(0, sampleUserID); long sampleItemID = 11; prefs.setItemID(0, sampleItemID); prefs.setUserID(1, sampleUserID); long sampleItemID2 = 22; prefs.setItemID(1, sampleItemID2); FastByIDMap<PreferenceArray> delegatePreferences = new FastByIDMap<>(); delegatePreferences.put(sampleUserID, prefs); PlusAnonymousConcurrentUserDataModel instance = getTestableWithDelegateData(10, delegatePreferences); Long anonymousUserID = instance.takeAvailableUser(); PreferenceArray tempPrefs = new GenericUserPreferenceArray(2); tempPrefs.setUserID(0, anonymousUserID); tempPrefs.setItemID(0, sampleItemID); tempPrefs.setUserID(1, anonymousUserID); long sampleItemID3 = 33; tempPrefs.setItemID(1, sampleItemID3); instance.setTempPrefs(tempPrefs, anonymousUserID); assertEquals(sampleUserID, instance.getPreferencesForItem(sampleItemID).get(0).getUserID()); assertEquals(2, instance.getPreferencesForItem(sampleItemID).length()); assertEquals(1, instance.getPreferencesForItem(sampleItemID2).length()); assertEquals(1, instance.getPreferencesForItem(sampleItemID3).length()); assertEquals(2, instance.getNumUsersWithPreferenceFor(sampleItemID)); assertEquals(1, instance.getNumUsersWithPreferenceFor(sampleItemID, sampleItemID2)); assertEquals(1, instance.getNumUsersWithPreferenceFor(sampleItemID, sampleItemID3)); }