* improved various hashCode() methods

* removed unnecessary synchronized locks
* refactoring
This commit is contained in:
Reinhard Pointner 2009-07-16 12:06:51 +00:00
parent c81cd50fc9
commit c4ce1aebe7
12 changed files with 83 additions and 37 deletions

View File

@ -225,7 +225,7 @@ public class EpisodeFormatBindingBean {
}
private synchronized MediaInfo getMediaInfo() {
private MediaInfo getMediaInfo() {
if (mediaInfo == null) {
// make sure media file is defined
checkMediaFile();

View File

@ -2,12 +2,15 @@
package net.sourceforge.filebot.similarity;
import java.util.Arrays;
public class Match<Value, Candidate> {
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<Value, Candidate> {
@Override
public int hashCode() {
return (value == null ? 0 : value.hashCode()) ^ (candidate == null ? 0 : candidate.hashCode());
return Arrays.hashCode(new Object[] { value, candidate });
}

View File

@ -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 });
}

View File

@ -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<String, Integer> format;
private Map<EventProperty, Integer> 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<String, Integer>(columns.length);
format = new EnumMap<EventProperty, Integer>(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
}
}

View File

@ -71,10 +71,11 @@ class AutoFetchEpisodeListMatcher extends SwingWorker<List<Match<File, Episode>>
protected SearchResult selectSearchResult(final String query, final List<SearchResult> searchResults) throws Exception {
if (searchResults.size() == 1) {
return searchResults.iterator().next();
return searchResults.get(0);
}
final LinkedList<SearchResult> probableMatches = new LinkedList<SearchResult>();
// auto-select most probable search result
final List<SearchResult> probableMatches = new LinkedList<SearchResult>();
// use name similarity metric
SimilarityMetric metric = new NameSimilarityMetric();
@ -86,8 +87,9 @@ class AutoFetchEpisodeListMatcher extends SwingWorker<List<Match<File, Episode>>
}
}
// 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<List<Match<File, Episode>>
@Override
public SearchResult call() throws Exception {
// display only probable matches if any
List<SearchResult> selection = probableMatches.isEmpty() ? searchResults : probableMatches;
// display only probable matches if possible
List<SearchResult> options = probableMatches.isEmpty() ? searchResults : probableMatches;
// multiple results have been found, user must select one
SelectDialog<SearchResult> selectDialog = new SelectDialog<SearchResult>(null, selection);
SelectDialog<SearchResult> selectDialog = new SelectDialog<SearchResult>(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<List<Match<File, Episode>>
// fetch episode lists concurrently
List<Episode> episodes = new ArrayList<Episode>();
ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
ExecutorService executor = Executors.newCachedThreadPool();
for (Future<Collection<Episode>> future : executor.invokeAll(tasks)) {
episodes.addAll(future.get());

View File

@ -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 });
}
}

View File

@ -195,7 +195,7 @@ public class AnidbClient implements EpisodeListProvider {
@Override
public URI getEpisodeListLink(SearchResult searchResult) {
return ((HyperLink) searchResult).toURI();
return ((HyperLink) searchResult).getURI();
}

View File

@ -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() });
}
}

View File

@ -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 });
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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();
}