Minor optimizations

This commit is contained in:
Reinhard Pointner 2016-10-08 04:49:08 +08:00
parent 70a9fc7d0c
commit a4686dd3c6
5 changed files with 21 additions and 11 deletions

View File

@ -1,6 +1,8 @@
package net.filebot.similarity;
import static java.util.stream.Collectors.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.StringUtilities.*;
import java.io.File;
import java.time.LocalDate;
@ -17,7 +19,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.filebot.web.SimpleDate;
import one.util.streamex.IntStreamEx;
import one.util.streamex.StreamEx;
public class DateMatcher {
@ -153,7 +154,7 @@ public class DateMatcher {
protected SimpleDate process(MatchResult match) {
try {
String dateString = IntStreamEx.rangeClosed(1, match.groupCount()).mapToObj(match::group).joining(DELIMITER);
String dateString = streamCapturingGroups(match).collect(joining(DELIMITER));
LocalDate date = LocalDate.parse(dateString, format);
if (sanity == null || sanity.test(date)) {

View File

@ -14,7 +14,6 @@ import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.MatchResult;
@ -72,7 +71,7 @@ public class SeasonEpisodeMatcher {
// match patterns like 01, 102, 1003, 10102 (enclosed in separators)
Num101_TOKEN = new SeasonEpisodePattern(sanity, "(?<!\\p{Alnum})([0-2]?\\d?)(\\d{2})(\\d{2})?(?!\\p{Alnum})", m -> {
return numbers(m.group(1), IntStream.rangeClosed(2, m.groupCount()).mapToObj(m::group).filter(Objects::nonNull).toArray(String[]::new));
return numbers(m.group(1), streamCapturingGroups(m).skip(1).toArray(String[]::new));
});
// match patterns like "1 of 2" as Episode 1

View File

@ -51,16 +51,14 @@ class FilesListTransferablePolicy extends BackgroundFileTransferablePolicy<File>
@Override
protected void load(List<File> files, TransferAction action) {
// use fast file to minimize system calls like length(), isDirectory(), isFile(), ...
files.replaceAll(FastFile::new);
// collect files recursively and eliminate duplicates
Set<File> sink = new LinkedHashSet<File>(64, 4);
// load files recursively by default
load(files, action != TransferAction.LINK, sink);
publish(sink.toArray(new File[0]));
// use fast file to minimize system calls like length(), isDirectory(), isFile(), ...
publish(sink.stream().map(FastFile::new).toArray(File[]::new));
}
private void load(List<File> files, boolean recursive, Collection<File> sink) {

View File

@ -124,7 +124,7 @@ public class FileTransferable implements Transferable {
}
}
return files.stream().distinct().sorted(HUMAN_NAME_ORDER).collect(toList());
return sortUnique(files);
}
}
}
@ -136,7 +136,7 @@ public class FileTransferable implements Transferable {
if (transferable instanceof List) {
List<File> files = (List<File>) transferable;
return files.stream().distinct().sorted(HUMAN_NAME_ORDER).collect(toList());
return sortUnique(files);
}
// on some platforms transferable data will not be available until the drop has been accepted
@ -146,4 +146,9 @@ public class FileTransferable implements Transferable {
// cannot get files from transferable
throw new UnsupportedFlavorException(DataFlavor.javaFileListFlavor);
}
private static List<File> sortUnique(List<File> files) {
return files.stream().distinct().sorted(HUMAN_NAME_ORDER).collect(toList());
}
}

View File

@ -17,6 +17,7 @@ import java.util.function.Function;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public final class StringUtilities {
@ -62,6 +63,11 @@ public final class StringUtilities {
return stream(new MatcherSpliterator(pattern.matcher(s)), false).map(mapper);
}
public static Stream<String> streamCapturingGroups(MatchResult match) {
// Group 0 is the entire match and not the first capturing group
return IntStream.rangeClosed(1, match.groupCount()).mapToObj(match::group).filter(Objects::nonNull);
}
public static boolean find(String s, Pattern pattern) {
if (s == null || s.length() == 0) {
return false;
@ -127,7 +133,8 @@ public final class StringUtilities {
if (!m.find()) {
return false;
}
f.accept(m.toMatchResult());
f.accept(m);
return true;
}