OSDN Git Service

FileWatch用ユーティリティ
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / filewatch / FileWatchUtil.java
1 package yukihane.inqubus.filewatch;
2
3 import java.nio.file.Path;
4 import java.util.Collection;
5 import java.util.SortedSet;
6 import java.util.TreeSet;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9 import org.apache.commons.io.FilenameUtils;
10
11 /**
12  *
13  * @author user
14  */
15 public final class FileWatchUtil {
16     // TODO スレッドID指定の場合の考慮も必要か
17
18     private static final Pattern VIDEO_ID_PATTERN = Pattern.compile("\\w\\w\\d+");
19
20     private FileWatchUtil() {
21     }
22
23     public static SortedSet<String> getVideoIds(Collection<Path> paths) {
24         final SortedSet<String> set = new TreeSet<>();
25         for (Path p : paths) {
26             final String name = FilenameUtils.getBaseName(p.toString());
27             final Matcher m = VIDEO_ID_PATTERN.matcher(name);
28             while (m.find()) {
29                 set.add(m.group());
30             }
31         }
32         return set;
33     }
34
35     public static SortedSet<String> contain(Collection<Path> paths, String id) {
36         final SortedSet<String> set = new TreeSet<>();
37         for (Path p : paths) {
38             final String name = FilenameUtils.getBaseName(p.toString());
39             if (name.contains(id)) {
40                 set.add(p.toAbsolutePath().toString());
41             }
42         }
43         return set;
44     }
45 }