Skip to content

Commit

Permalink
Remove the "ancient PC" optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Sep 25, 2023
1 parent 2b8695d commit 6affbe8
Showing 1 changed file with 16 additions and 38 deletions.
54 changes: 16 additions & 38 deletions src/main/java/listfix/model/playlists/PlaylistEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public abstract class PlaylistEntry implements Cloneable
{
Expand Down Expand Up @@ -103,49 +104,26 @@ public void setFixed(boolean fixed)

public List<PotentialPlaylistEntryMatch> findClosestMatches(Collection<String> mediaFiles, IProgressObserver<String> observer, IPlaylistOptions playListOptions)
{
List<PotentialPlaylistEntryMatch> matches = new ArrayList<>();
ProgressAdapter<String> progress = new ProgressAdapter<>(observer);
progress.setTotal(mediaFiles.size());

// Remove apostrophes and addAt spaces between lowercase and capital letters so we can tokenize by camel case.
String entryName = CAMEL_CASE_PATTERN.matcher(APOS_PATTERN.matcher(getTrackFileName()).replaceAll("")).replaceAll("$2 $3").toLowerCase();
File mediaFile;
int score;
for (String mediaFilePath : mediaFiles)
{
if (observer == null || !observer.getCancelled())
{
progress.stepCompleted();

mediaFile = new File(mediaFilePath);

// Remove apostrophes and addAt spaces between lowercase and capital letters so we can tokenize by camel case.
score = new FileNameTokenizer(playListOptions).score(entryName, CAMEL_CASE_PATTERN.matcher(APOS_PATTERN.matcher(mediaFile.getName()).replaceAll("")).replaceAll("$2 $3").toLowerCase());
if (score > 0)
{
// Only keep the top X highest-rated matches (default is 20), anything more than that has a good chance of using too much memory
// on systems w/ huge media libraries, too little RAM, or when fixing excessively large playlists (the things you have to worry
// about when people run your software on ancient PCs in Africa =])
if (matches.size() < playListOptions.getMaxClosestResults())
{
matches.add(new PotentialPlaylistEntryMatch(mediaFile.toPath(), score));
}
else
{
if (matches.get(playListOptions.getMaxClosestResults() - 1).getScore() < score)
{
matches.set(playListOptions.getMaxClosestResults() - 1, new PotentialPlaylistEntryMatch(mediaFile.toPath(), score));
}
}
matches.sort(new MatchedPlaylistEntryComparator());
}
}
else
{
return null;
}
}
return matches;

return mediaFiles.stream().map(mediaFilePath -> {

if (observer == null || !observer.getCancelled()) return null;

progress.stepCompleted();

final File mediaFile = new File(mediaFilePath);
final int score = new FileNameTokenizer(playListOptions).score(entryName, CAMEL_CASE_PATTERN.matcher(APOS_PATTERN.matcher(mediaFile.getName()).replaceAll("")).replaceAll("$2 $3").toLowerCase());

return new PotentialPlaylistEntryMatch(mediaFile.toPath(), score);
}).filter(Objects::nonNull)
.sorted(new MatchedPlaylistEntryComparator())
.limit(playListOptions.getMaxClosestResults())
.collect(Collectors.toList());
}

@Override
Expand Down

0 comments on commit 6affbe8

Please sign in to comment.