Refactor SimilarityComparator simple usage

This commit is contained in:
Reinhard Pointner 2016-02-09 01:17:48 +00:00
parent 2ca7b6b806
commit e2a3149d19
3 changed files with 6 additions and 6 deletions

View File

@ -938,7 +938,7 @@ public class MediaBindingBean {
// still no good match found -> just take the most probable video from the same folder // still no good match found -> just take the most probable video from the same folder
if (videos.size() > 0) { if (videos.size() > 0) {
sort(videos, new SimilarityComparator<File, String>(FileUtilities.getName(getMediaFile()), FileUtilities::getName)); sort(videos, SimilarityComparator.compareTo(getMediaFile(), FileUtilities::getName));
return videos.get(0); return videos.get(0);
} }
} }

View File

@ -9,6 +9,10 @@ import java.util.function.Function;
public class SimilarityComparator<T, P> implements Comparator<T> { public class SimilarityComparator<T, P> implements Comparator<T> {
public static <T, S extends CharSequence> SimilarityComparator<T, S> compareTo(T value, Function<T, S> mapper) {
return new SimilarityComparator<T, S>(new NameSimilarityMetric(), singleton(mapper.apply(value)), mapper.andThen(Collections::singleton));
}
protected SimilarityMetric metric; protected SimilarityMetric metric;
protected Collection<P> paragon; protected Collection<P> paragon;
@ -20,10 +24,6 @@ public class SimilarityComparator<T, P> implements Comparator<T> {
this.mapper = mapper; this.mapper = mapper;
} }
public SimilarityComparator(P paragon, Function<T, P> mapper) {
this(new NameSimilarityMetric(), singleton(paragon), mapper.andThen(Collections::singleton));
}
@Override @Override
public int compare(T o1, T o2) { public int compare(T o1, T o2) {
return Double.compare(getSimilarity(o2), getSimilarity(o1)); return Double.compare(getSimilarity(o2), getSimilarity(o1));

View File

@ -27,7 +27,7 @@ public class SimilarityComparatorTest {
@Test @Test
public void defaultUsage() { public void defaultUsage() {
SimilarityComparator<String, String> c = new SimilarityComparator<String, String>("Hello", String::toString); SimilarityComparator<String, String> c = SimilarityComparator.compareTo("Hello", String::toString);
List<String> phrases = generateWords(); List<String> phrases = generateWords();
phrases.sort(c); phrases.sort(c);