Added Pattern/Matcher stream

This commit is contained in:
Reinhard Pointner 2016-04-05 18:06:06 +00:00
parent 0cda058ae7
commit 247bc668b1
2 changed files with 29 additions and 5 deletions

View File

@ -274,6 +274,8 @@ public final class FileUtilities {
} }
public static String getName(File file) { public static String getName(File file) {
if (file == null)
return null;
if (file.getName().isEmpty() || UNC_PREFIX.equals(file.getParent())) if (file.getName().isEmpty() || UNC_PREFIX.equals(file.getParent()))
return getFolderName(file); return getFolderName(file);

View File

@ -1,5 +1,6 @@
package net.filebot.util; package net.filebot.util;
import static java.util.stream.StreamSupport.*;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.*;
@ -9,7 +10,11 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Spliterators.AbstractSpliterator;
import java.util.function.Consumer;
import java.util.regex.MatchResult;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream; import java.util.stream.Stream;
public final class StringUtilities { public final class StringUtilities {
@ -47,6 +52,10 @@ public final class StringUtilities {
return null; return null;
} }
public static Stream<String> streamMatches(CharSequence s, Pattern pattern) {
return stream(new MatcherSpliterator(pattern.matcher(s)), false).map(MatchResult::group);
}
public static String asString(Object object) { public static String asString(Object object) {
return object == null ? null : object.toString(); return object == null ? null : object.toString();
} }
@ -85,11 +94,24 @@ public final class StringUtilities {
return values.map(StringUtilities::asNonEmptyString).filter(Objects::nonNull).collect(joining(delimiter, prefix, suffix)); return values.map(StringUtilities::asNonEmptyString).filter(Objects::nonNull).collect(joining(delimiter, prefix, suffix));
} }
/** public static class MatcherSpliterator extends AbstractSpliterator<MatchResult> {
* Dummy constructor to prevent instantiation.
*/ private final Matcher m;
private StringUtilities() {
throw new UnsupportedOperationException(); public MatcherSpliterator(Matcher m) {
super(Long.MAX_VALUE, ORDERED | NONNULL | IMMUTABLE);
this.m = m;
}
@Override
public boolean tryAdvance(Consumer<? super MatchResult> f) {
if (!m.find()) {
return false;
}
f.accept(m.toMatchResult());
return true;
}
} }
} }