@Override public synchronized Long2DoubleMap getUserRatingVector(long userId) { // FIXME Don't make this so locky if (cache == null) { cache = new Long2ObjectOpenHashMap<>(); } Long2DoubleMap data = cache.get(userId); if (data != null) { return data; } else { Collection<RatingMatrixEntry> prefs = this.getUserRatings(userId); Long2DoubleMap map = new Long2DoubleOpenHashMap(); for (RatingMatrixEntry e: prefs) { map.put(e.getItemId(), e.getValue()); } data = new Long2DoubleSortedArrayMap(map); cache.put(userId, data); return data; } }
@Test public void testGetUserRatings() { Collection<RatingMatrixEntry> ratings = snap.getUserRatings(1); Equivalence<Preference> eq = Ratings.preferenceEquivalence(); assertThat(ratings, hasSize(4)); ratings = snap.getUserRatings(2); assertThat(ratings, hasSize(0)); ratings = snap.getUserRatings(3); assertEquals(4, ratings.size()); assertThat(Iterables.transform(ratings, eq::wrap), ratings = snap.getUserRatings(4); assertEquals(5, ratings.size()); assertThat(Iterables.transform(ratings, eq::wrap), ratings = snap.getUserRatings(5); assertEquals(2, ratings.size()); assertThat(Iterables.transform(ratings, eq::wrap), ratings = snap.getUserRatings(6); assertEquals(2, ratings.size()); assertThat(Iterables.transform(ratings, eq::wrap), ratings = snap.getUserRatings(7); assertEquals(3, ratings.size()); assertThat(Iterables.transform(ratings, eq::wrap),
@Override public synchronized Long2DoubleMap getUserRatingVector(long userId) { // FIXME Don't make this so locky if (cache == null) { cache = new Long2ObjectOpenHashMap<>(); } Long2DoubleMap data = cache.get(userId); if (data != null) { return data; } else { Collection<RatingMatrixEntry> prefs = this.getUserRatings(userId); Long2DoubleMap map = new Long2DoubleOpenHashMap(); for (RatingMatrixEntry e: prefs) { map.put(e.getItemId(), e.getValue()); } data = new Long2DoubleSortedArrayMap(map); cache.put(userId, data); return data; } }