From 4c95e5b9ca416b9c341fb41aa1cc0c6913dd8ba8 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 6 Jul 2008 03:32:24 +0000 Subject: [PATCH] * even more cleanup --- .../ui/panel/rename/matcher/Match.java | 29 ++++++ .../ui/panel/rename/matcher/Matcher.java | 99 +++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 source/net/sourceforge/filebot/ui/panel/rename/matcher/Match.java create mode 100644 source/net/sourceforge/filebot/ui/panel/rename/matcher/Matcher.java diff --git a/source/net/sourceforge/filebot/ui/panel/rename/matcher/Match.java b/source/net/sourceforge/filebot/ui/panel/rename/matcher/Match.java new file mode 100644 index 00000000..0e4bbe44 --- /dev/null +++ b/source/net/sourceforge/filebot/ui/panel/rename/matcher/Match.java @@ -0,0 +1,29 @@ + +package net.sourceforge.filebot.ui.panel.rename.matcher; + + +import net.sourceforge.filebot.ui.panel.rename.entry.ListEntry; + + +public class Match { + + private final ListEntry a; + private final ListEntry b; + + + public Match(ListEntry a, ListEntry b) { + this.a = a; + this.b = b; + } + + + public ListEntry getA() { + return a; + } + + + public ListEntry getB() { + return b; + } + +} diff --git a/source/net/sourceforge/filebot/ui/panel/rename/matcher/Matcher.java b/source/net/sourceforge/filebot/ui/panel/rename/matcher/Matcher.java new file mode 100644 index 00000000..8e3be765 --- /dev/null +++ b/source/net/sourceforge/filebot/ui/panel/rename/matcher/Matcher.java @@ -0,0 +1,99 @@ + +package net.sourceforge.filebot.ui.panel.rename.matcher; + + +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import net.sourceforge.filebot.ui.panel.rename.entry.ListEntry; +import net.sourceforge.filebot.ui.panel.rename.metric.SimilarityMetric; + + +public class Matcher implements Iterator { + + private final LinkedList primaryList; + private final LinkedList secondaryList; + private final SimilarityMetric similarityMetric; + + + public Matcher(List primaryList, List secondaryList, SimilarityMetric similarityMetric) { + this.primaryList = new LinkedList(primaryList); + this.secondaryList = new LinkedList(secondaryList); + this.similarityMetric = similarityMetric; + } + + + @Override + public boolean hasNext() { + return remainingMatches() > 0; + } + + + @Override + public Match next() { + ListEntry primaryEntry = primaryList.removeFirst(); + + float maxSimilarity = -1; + ListEntry mostSimilarSecondaryEntry = null; + + for (ListEntry secondaryEntry : secondaryList) { + float similarity = similarityMetric.getSimilarity(primaryEntry, secondaryEntry); + + if (similarity > maxSimilarity) { + maxSimilarity = similarity; + mostSimilarSecondaryEntry = secondaryEntry; + } + } + + if (mostSimilarSecondaryEntry != null) { + secondaryList.remove(mostSimilarSecondaryEntry); + } + + return new Match(primaryEntry, mostSimilarSecondaryEntry); + } + + + public ListEntry getFirstPrimaryEntry() { + if (primaryList.isEmpty()) + return null; + + return primaryList.getFirst(); + } + + + public ListEntry getFirstSecondaryEntry() { + if (secondaryList.isEmpty()) + return null; + + return secondaryList.getFirst(); + } + + + public int remainingMatches() { + return Math.min(primaryList.size(), secondaryList.size()); + } + + + public List getPrimaryList() { + return Collections.unmodifiableList(primaryList); + } + + + public List getSecondaryList() { + return Collections.unmodifiableList(secondaryList); + } + + + /** + * The remove operation is not supported by this implementation of Iterator. + * + * @throws UnsupportedOperationException if this method is invoked. + * @see java.util.Iterator + */ + @Override + public void remove() { + throw new UnsupportedOperationException(); + } +}