GenericItemSimilarity implements ItemSimilarity { @Override public double itemSimilarity(long itemID1, long itemID2) { if (itemID1 == itemID2) { return 1.0; } long firstID; long secondID; if (itemID1 < itemID2) { firstID = itemID1; secondID = itemID2; } else { firstID = itemID2; secondID = itemID1; } FastByIDMap<Double> nextMap = similarityMaps.get(firstID); if (nextMap == null) { return Double.NaN; } Double similarity = nextMap.get(secondID); return similarity == null ? Double.NaN : similarity; } GenericItemSimilarity(Iterable<ItemItemSimilarity> similarities);  GenericItemSimilarity(Iterable<ItemItemSimilarity> similarities, int maxToKeep);  GenericItemSimilarity(ItemSimilarity otherSimilarity, DataModel dataModel);  GenericItemSimilarity(ItemSimilarity otherSimilarity,
                               DataModel dataModel,
                               int maxToKeep); @Override double itemSimilarity(long itemID1, long itemID2); @Override double[] itemSimilarities(long itemID1, long[] itemID2s); @Override long[] allSimilarItemIDs(long itemID); @Override void refresh(Collection<Refreshable> alreadyRefreshed);  }
@Test public void testSimple() { List<GenericItemSimilarity.ItemItemSimilarity> similarities = Lists.newArrayList(); similarities.add(new GenericItemSimilarity.ItemItemSimilarity(1, 2, 0.5)); similarities.add(new GenericItemSimilarity.ItemItemSimilarity(2, 1, 0.6)); similarities.add(new GenericItemSimilarity.ItemItemSimilarity(1, 1, 0.5)); similarities.add(new GenericItemSimilarity.ItemItemSimilarity(1, 3, 0.3)); GenericItemSimilarity itemCorrelation = new GenericItemSimilarity(similarities); assertEquals(1.0, itemCorrelation.itemSimilarity(1, 1), EPSILON); assertEquals(0.6, itemCorrelation.itemSimilarity(1, 2), EPSILON); assertEquals(0.6, itemCorrelation.itemSimilarity(2, 1), EPSILON); assertEquals(0.3, itemCorrelation.itemSimilarity(1, 3), EPSILON); assertTrue(Double.isNaN(itemCorrelation.itemSimilarity(3, 4))); }