From c4ce1aebe7ba33d5427ce92903a4bab71fa46d4f Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 16 Jul 2009 12:06:51 +0000 Subject: [PATCH] * improved various hashCode() methods * removed unnecessary synchronized locks * refactoring --- .../format/EpisodeFormatBindingBean.java | 2 +- .../sourceforge/filebot/similarity/Match.java | 7 ++-- .../similarity/SeasonEpisodeMatcher.java | 3 +- .../subtitle/SubStationAlphaReader.java | 34 ++++++++++++++----- .../rename/AutoFetchEpisodeListMatcher.java | 16 +++++---- .../filebot/ui/panel/rename/History.java | 5 +-- .../sourceforge/filebot/web/AnidbClient.java | 2 +- .../sourceforge/filebot/web/HyperLink.java | 22 ++++++++++-- .../filebot/web/MovieDescriptor.java | 7 ++-- .../filebot/web/SubsceneSubtitleClient.java | 2 +- .../filebot/web/TVDotComClient.java | 8 ++--- .../tuned/ByteBufferInputStream.java | 12 +++---- 12 files changed, 83 insertions(+), 37 deletions(-) diff --git a/source/net/sourceforge/filebot/format/EpisodeFormatBindingBean.java b/source/net/sourceforge/filebot/format/EpisodeFormatBindingBean.java index 5ee4c704..6ef10fe4 100644 --- a/source/net/sourceforge/filebot/format/EpisodeFormatBindingBean.java +++ b/source/net/sourceforge/filebot/format/EpisodeFormatBindingBean.java @@ -225,7 +225,7 @@ public class EpisodeFormatBindingBean { } - private synchronized MediaInfo getMediaInfo() { + private MediaInfo getMediaInfo() { if (mediaInfo == null) { // make sure media file is defined checkMediaFile(); diff --git a/source/net/sourceforge/filebot/similarity/Match.java b/source/net/sourceforge/filebot/similarity/Match.java index 4707d3e4..3c984955 100644 --- a/source/net/sourceforge/filebot/similarity/Match.java +++ b/source/net/sourceforge/filebot/similarity/Match.java @@ -2,12 +2,15 @@ package net.sourceforge.filebot.similarity; +import java.util.Arrays; + + public class Match { private final Value value; private final Candidate candidate; - + public Match(Value value, Candidate candidate) { this.value = value; this.candidate = candidate; @@ -49,7 +52,7 @@ public class Match { @Override public int hashCode() { - return (value == null ? 0 : value.hashCode()) ^ (candidate == null ? 0 : candidate.hashCode()); + return Arrays.hashCode(new Object[] { value, candidate }); } diff --git a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java index 9f75df79..1baa55e3 100644 --- a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java +++ b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java @@ -3,6 +3,7 @@ package net.sourceforge.filebot.similarity; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -104,7 +105,7 @@ public class SeasonEpisodeMatcher { @Override public int hashCode() { - return season ^ episode; + return Arrays.hashCode(new Object[] { season, episode }); } diff --git a/source/net/sourceforge/filebot/subtitle/SubStationAlphaReader.java b/source/net/sourceforge/filebot/subtitle/SubStationAlphaReader.java index 7a884e6a..e9588518 100644 --- a/source/net/sourceforge/filebot/subtitle/SubStationAlphaReader.java +++ b/source/net/sourceforge/filebot/subtitle/SubStationAlphaReader.java @@ -6,7 +6,7 @@ import static net.sourceforge.tuned.StringUtilities.*; import java.text.DateFormat; import java.util.Arrays; -import java.util.HashMap; +import java.util.EnumMap; import java.util.InputMismatchException; import java.util.Map; import java.util.Scanner; @@ -18,7 +18,7 @@ public class SubStationAlphaReader extends SubtitleReader { private final DateFormat timeFormat = new SubtitleTimeFormat(); private final Pattern newline = Pattern.compile(Pattern.quote("\\n"), Pattern.CASE_INSENSITIVE); - private Map format; + private Map format; public SubStationAlphaReader(Scanner scanner) { @@ -37,10 +37,14 @@ public class SubStationAlphaReader extends SubtitleReader { String[] columns = event[1].split(","); // map column name to column index - format = new HashMap(columns.length); + format = new EnumMap(EventProperty.class); for (int i = 0; i < columns.length; i++) { - format.put(columns[i].trim(), i); + try { + format.put(EventProperty.valueOf(columns[i].trim()), i); + } catch (IllegalArgumentException e) { + // ignore + } } } @@ -71,11 +75,11 @@ public class SubStationAlphaReader extends SubtitleReader { throw new InputMismatchException("Illegal dialogue event: " + Arrays.toString(event)); // extract information - String[] row = event[1].split(",", format.size()); + String[] values = event[1].split(",", format.size()); - long start = timeFormat.parse(row[format.get("Start")]).getTime(); - long end = timeFormat.parse(row[format.get("End")]).getTime(); - String text = row[format.get("Text")].trim(); + long start = timeFormat.parse(values[format.get(EventProperty.Start)]).getTime(); + long end = timeFormat.parse(values[format.get(EventProperty.End)]).getTime(); + String text = values[format.get(EventProperty.Text)].trim(); // translate "\\n" to new lines String[] lines = newline.split(text); @@ -83,4 +87,18 @@ public class SubStationAlphaReader extends SubtitleReader { return new SubtitleElement(start, end, join(lines, "\n")); } + + private enum EventProperty { + Layer, + Start, + End, + Style, + Name, + MarginL, + MarginR, + MarginV, + Effect, + Text + } + } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java b/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java index 6e4b5a05..e9f563cf 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java @@ -71,10 +71,11 @@ class AutoFetchEpisodeListMatcher extends SwingWorker> protected SearchResult selectSearchResult(final String query, final List searchResults) throws Exception { if (searchResults.size() == 1) { - return searchResults.iterator().next(); + return searchResults.get(0); } - final LinkedList probableMatches = new LinkedList(); + // auto-select most probable search result + final List probableMatches = new LinkedList(); // use name similarity metric SimilarityMetric metric = new NameSimilarityMetric(); @@ -86,8 +87,9 @@ class AutoFetchEpisodeListMatcher extends SwingWorker> } } + // auto-select first and only probable search result if (probableMatches.size() == 1) { - return probableMatches.getFirst(); + return probableMatches.get(0); } // show selection dialog on EDT @@ -95,11 +97,11 @@ class AutoFetchEpisodeListMatcher extends SwingWorker> @Override public SearchResult call() throws Exception { - // display only probable matches if any - List selection = probableMatches.isEmpty() ? searchResults : probableMatches; + // display only probable matches if possible + List options = probableMatches.isEmpty() ? searchResults : probableMatches; // multiple results have been found, user must select one - SelectDialog selectDialog = new SelectDialog(null, selection); + SelectDialog selectDialog = new SelectDialog(null, options); selectDialog.getHeaderLabel().setText(String.format("Shows matching '%s':", query)); selectDialog.getCancelAction().putValue(Action.NAME, "Ignore"); @@ -149,7 +151,7 @@ class AutoFetchEpisodeListMatcher extends SwingWorker> // fetch episode lists concurrently List episodes = new ArrayList(); - ExecutorService executor = Executors.newFixedThreadPool(tasks.size()); + ExecutorService executor = Executors.newCachedThreadPool(); for (Future> future : executor.invokeAll(tasks)) { episodes.addAll(future.get()); diff --git a/source/net/sourceforge/filebot/ui/panel/rename/History.java b/source/net/sourceforge/filebot/ui/panel/rename/History.java index e002b5c4..467b12b7 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/History.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/History.java @@ -7,6 +7,7 @@ import static java.util.Collections.*; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.List; @@ -75,7 +76,7 @@ class History { @Override public int hashCode() { - return elements.hashCode() ^ date.hashCode(); + return Arrays.hashCode(new Object[] { elements, date }); } } @@ -125,7 +126,7 @@ class History { @Override public int hashCode() { - return to.hashCode() ^ from.hashCode() ^ dir.hashCode(); + return Arrays.hashCode(new Object[] { to, from, dir }); } } diff --git a/source/net/sourceforge/filebot/web/AnidbClient.java b/source/net/sourceforge/filebot/web/AnidbClient.java index 86563474..420d8791 100644 --- a/source/net/sourceforge/filebot/web/AnidbClient.java +++ b/source/net/sourceforge/filebot/web/AnidbClient.java @@ -195,7 +195,7 @@ public class AnidbClient implements EpisodeListProvider { @Override public URI getEpisodeListLink(SearchResult searchResult) { - return ((HyperLink) searchResult).toURI(); + return ((HyperLink) searchResult).getURI(); } diff --git a/source/net/sourceforge/filebot/web/HyperLink.java b/source/net/sourceforge/filebot/web/HyperLink.java index 4327dd6b..ac69b146 100644 --- a/source/net/sourceforge/filebot/web/HyperLink.java +++ b/source/net/sourceforge/filebot/web/HyperLink.java @@ -5,13 +5,14 @@ package net.sourceforge.filebot.web; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.util.Arrays; public class HyperLink extends SearchResult { private final URL url; - + public HyperLink(String name, URL url) { super(name); this.url = url; @@ -23,7 +24,7 @@ public class HyperLink extends SearchResult { } - public URI toURI() { + public URI getURI() { try { return url.toURI(); } catch (URISyntaxException e) { @@ -31,4 +32,21 @@ public class HyperLink extends SearchResult { } } + + @Override + public boolean equals(Object object) { + if (object instanceof HyperLink) { + HyperLink other = (HyperLink) object; + return name.equals(name) && url.toString().equals(other.url.toString()); + } + + return false; + } + + + @Override + public int hashCode() { + return Arrays.hashCode(new Object[] { name, url.toString() }); + } + } diff --git a/source/net/sourceforge/filebot/web/MovieDescriptor.java b/source/net/sourceforge/filebot/web/MovieDescriptor.java index d6effbb6..a8e7cdc6 100644 --- a/source/net/sourceforge/filebot/web/MovieDescriptor.java +++ b/source/net/sourceforge/filebot/web/MovieDescriptor.java @@ -2,6 +2,9 @@ package net.sourceforge.filebot.web; +import java.util.Arrays; + + public class MovieDescriptor extends SearchResult { private final int year; @@ -35,7 +38,7 @@ public class MovieDescriptor extends SearchResult { public boolean equals(Object object) { if (object instanceof MovieDescriptor) { MovieDescriptor other = (MovieDescriptor) object; - return imdbId == other.imdbId && name.equals(other.name) && year == other.year; + return imdbId == other.imdbId && year == other.year && name.equals(other.name); } return false; @@ -44,7 +47,7 @@ public class MovieDescriptor extends SearchResult { @Override public int hashCode() { - return name.hashCode() ^ year ^ imdbId; + return Arrays.hashCode(new Object[] { name, year, imdbId }); } diff --git a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java index 8365b3af..23fa0d7d 100644 --- a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java +++ b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java @@ -204,7 +204,7 @@ public class SubsceneSubtitleClient implements SubtitleProvider { @Override public URI getSubtitleListLink(SearchResult searchResult, String languageName) { - return ((HyperLink) searchResult).toURI(); + return ((HyperLink) searchResult).getURI(); } } diff --git a/source/net/sourceforge/filebot/web/TVDotComClient.java b/source/net/sourceforge/filebot/web/TVDotComClient.java index 348b49e6..aa6d0899 100644 --- a/source/net/sourceforge/filebot/web/TVDotComClient.java +++ b/source/net/sourceforge/filebot/web/TVDotComClient.java @@ -6,6 +6,7 @@ import static net.sourceforge.filebot.web.WebRequest.*; import static net.sourceforge.tuned.XPathUtilities.*; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.net.URLEncoder; @@ -70,10 +71,9 @@ public class TVDotComClient implements EpisodeListProvider { String href = getAttribute("href", node); try { - URL episodeListingUrl = new URL(href.replaceAll("summary\\.html\\?.*", "episode.html")); - - searchResults.add(new HyperLink(title, episodeListingUrl)); - } catch (Exception e) { + URL episodeGuideLocation = new URL(href.replaceAll("summary\\.html\\?.*", "episode.html")); + searchResults.add(new HyperLink(title, episodeGuideLocation)); + } catch (MalformedURLException e) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Invalid href: " + href, e); } } diff --git a/source/net/sourceforge/tuned/ByteBufferInputStream.java b/source/net/sourceforge/tuned/ByteBufferInputStream.java index 3eeeac19..683e1d7d 100644 --- a/source/net/sourceforge/tuned/ByteBufferInputStream.java +++ b/source/net/sourceforge/tuned/ByteBufferInputStream.java @@ -11,14 +11,14 @@ public class ByteBufferInputStream extends InputStream { private final ByteBuffer buffer; - + public ByteBufferInputStream(ByteBuffer buffer) { this.buffer = buffer; } @Override - public synchronized int read() throws IOException { + public int read() throws IOException { if (buffer.remaining() <= 0) return -1; @@ -27,7 +27,7 @@ public class ByteBufferInputStream extends InputStream { @Override - public synchronized int read(byte[] b, int off, int len) throws IOException { + public int read(byte[] b, int off, int len) throws IOException { if (buffer.remaining() <= 0) return -1; @@ -40,7 +40,7 @@ public class ByteBufferInputStream extends InputStream { @Override - public synchronized int available() throws IOException { + public int available() throws IOException { return buffer.remaining(); } @@ -52,13 +52,13 @@ public class ByteBufferInputStream extends InputStream { @Override - public synchronized void mark(int readlimit) { + public void mark(int readlimit) { buffer.mark(); } @Override - public synchronized void reset() throws IOException { + public void reset() throws IOException { buffer.reset(); }