DefaultDeviationCalculationStrategy implements DeviationCalculationStrategy { public DeviationCalculationResult calculate(int userId, List<RatingVO<Integer, Integer>> ratings, Date oldRatingsBeforeThisDate) { if ((ratings == null) || ratings.isEmpty()) throw new IllegalArgumentException("Cannot calculate for empty/null rating list."); int approximateSize = (ratings.size() * (ratings.size() - 1)) / 2; final TLongObjectMap<Deviation> deviations = new TLongObjectHashMap<Deviation>(approximateSize); for (int i = 0; i < ratings.size(); i++) { RatingVO<Integer, Integer> rating1 = ratings.get(i); boolean rating1IsOld = rating1.getLastActionTime().before(oldRatingsBeforeThisDate); for (int j = i + 1; j < ratings.size(); j++) { RatingVO<Integer, Integer> rating2 = ratings.get(j); if (rating1IsOld && rating2.getLastActionTime().before(oldRatingsBeforeThisDate)) continue; int item1 = rating1.getItem().getItem(); int item2 = rating2.getItem().getItem(); int item1TypeId = rating1.getItem().getType(); int item2TypeId = rating2.getItem().getType(); boolean swapped = false; if (item1 > item2) { int itemTmp = item1; item1 = item2; item2 = itemTmp; swapped = true; } long hashOfItems = Deviation.hashCodeOfItems(item1, item1TypeId, item2, item2TypeId); Deviation deviation = deviations.get(hashOfItems); if (deviation == null) { deviation = new Deviation(rating1.getItem().getTenant(), item1, item1TypeId, item2, item2TypeId, 0.0, 0L); deviations.put(hashOfItems, deviation); } deviation.setDenominator(deviation.getDenominator() + 1); double diff; diff = swapped ? rating1.getRatingValue() + rating2.getRatingValue() : rating1.getRatingValue() - rating2.getRatingValue(); deviation.setNumerator(deviation.getNumerator() + diff); } } return new DeviationCalculationResult(new ArrayList<Deviation>(deviations.valueCollection()), deviations.size(), 0); }  DeviationCalculationResult calculate(int userId, List<RatingVO<Integer, Integer>> ratings,
                                                Date oldRatingsBeforeThisDate);  }
@Test public void calculate_withLemireData() { Date old = new Date(0); DeviationCalculationResult result = strategy.calculate(1, lemireRatingsByUser.get(1), old); List<Deviation> deviations = result.getDeviations(); assertThat(result.getCreated(), is(3)); assertThat(result.getModified(), is(0)); assertThat(deviations.size(), is(3)); assertThat(deviations, containsSymmetricDifference(1, 2, 5.0, 1)); assertThat(deviations, containsSymmetricDifference(1, 4, 9.0, 1)); assertThat(deviations, containsSymmetricDifference(2, 4, 4.0, 1)); result = strategy.calculate(2, lemireRatingsByUser.get(2), old); deviations = result.getDeviations(); assertThat(result.getCreated(), is(3)); assertThat(result.getModified(), is(0)); assertThat(deviations.size(), is(3)); assertThat(deviations, containsSymmetricDifference(1, 3, 5.0, 1)); assertThat(deviations, containsSymmetricDifference(1, 4, 8.0, 1)); assertThat(deviations, containsSymmetricDifference(3, 4, 3.0, 1)); result = strategy.calculate(3, lemireRatingsByUser.get(3), old); deviations = result.getDeviations(); assertThat(result.getCreated(), is(6)); assertThat(result.getModified(), is(0)); assertThat(deviations.size(), is(6)); assertThat(deviations, containsSymmetricDifference(1, 2, 5.0, 1)); assertThat(deviations, containsSymmetricDifference(1, 3, 4.0, 1)); assertThat(deviations, containsSymmetricDifference(1, 4, 8.0, 1)); assertThat(deviations, containsSymmetricDifference(2, 3, -1.0, 1)); assertThat(deviations, containsSymmetricDifference(2, 4, 3.0, 1)); assertThat(deviations, containsSymmetricDifference(3, 4, 4.0, 1)); result = strategy.calculate(4, lemireRatingsByUser.get(4), old); deviations = result.getDeviations(); assertThat(result.getCreated(), is(3)); assertThat(result.getModified(), is(0)); assertThat(deviations.size(), is(3)); assertThat(deviations, containsSymmetricDifference(1, 4, -9.0, 1)); assertThat(deviations, containsSymmetricDifference(1, 5, -3.0, 1)); assertThat(deviations, containsSymmetricDifference(4, 5, 6.0, 1)); }