### Question:
CachedSimilarityMeasure implements SimilarityMeasure<T> { @Override public double calculateSimilarity(T obj1, T obj2) { return calculateSimilarity(UnorderedPair.of(obj1, obj2)); }  @Override double calculateSimilarity(T obj1, T obj2); @Override double calculateSimilarity(UnorderedPair<T> pair); @Override void hash(Hasher hasher);  }### Answer:
@Test public void test() { doReturn(Double.valueOf(0.5)).when(sim).calculateSimilarity(UnorderedPair.of(Integer.valueOf(1), Integer.valueOf(2))); doReturn(Double.valueOf(1.0 / 3)).when(sim).calculateSimilarity(UnorderedPair.of(Integer.valueOf(1), Integer.valueOf(3))); SimilarityMeasure<Integer> cachedSim = CachedSimilarityMeasure.<Integer>builder() .similarityMeasure(sim) .build(); assertThat(cachedSim.calculateSimilarity(Integer.valueOf(1), Integer.valueOf(2))).isEqualTo(0.5); assertThat(cachedSim.calculateSimilarity(Integer.valueOf(2), Integer.valueOf(1))).isEqualTo(0.5); assertThat(cachedSim.calculateSimilarity(Integer.valueOf(1), Integer.valueOf(3))).isEqualTo(1.0 / 3); }
@Test public void testCache() { SimilarityMeasure<Integer> cachedSim = CachedSimilarityMeasure.<Integer>builder() .similarityMeasure(sim) .maximumSize(1) .build(); cachedSim.calculateSimilarity(Integer.valueOf(1), Integer.valueOf(2)); verify(sim).calculateSimilarity(UnorderedPair.of(Integer.valueOf(1), Integer.valueOf(2))); cachedSim.calculateSimilarity(Integer.valueOf(1), Integer.valueOf(2)); verify(sim).calculateSimilarity(UnorderedPair.of(Integer.valueOf(1), Integer.valueOf(2))); }
@Test public void testNoCache() { SimilarityMeasure<Integer> cachedSim = CachedSimilarityMeasure.<Integer>builder() .similarityMeasure(sim) .maximumSize(0) .build(); cachedSim.calculateSimilarity(Integer.valueOf(1), Integer.valueOf(2)); verify(sim).calculateSimilarity(UnorderedPair.of(Integer.valueOf(1), Integer.valueOf(2))); cachedSim.calculateSimilarity(Integer.valueOf(1), Integer.valueOf(2)); verify(sim, times(2)).calculateSimilarity(UnorderedPair.of(Integer.valueOf(1), Integer.valueOf(2))); }