OSDN Git Service

動画IDのパターンを一箇所にまとめる
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / filewatch / FileWatchUtil.java
1 package yukihane.inqubus.filewatch;
2
3 import static yukihane.inqubus.InqubusConstants.*;
4
5 import java.nio.file.Path;
6 import java.util.Collection;
7 import java.util.SortedSet;
8 import java.util.TreeSet;
9 import java.util.regex.Matcher;
10 import org.apache.commons.io.FilenameUtils;
11
12 /**
13  *
14  * @author user
15  */
16 public final class FileWatchUtil {
17
18     private FileWatchUtil() {
19     }
20
21     /**
22      * 指定された文字列で始まる動画IDを検索します.
23      * @param paths 検索対象のファイルパス.
24      * @param startText この文字で始まる動画ID一覧を出力します.
25      * @return 条件に当てはまる動画ID一覧.
26      */
27     public static SortedSet<String> getVideoIdsStartWith(Collection<Path> paths, String startText) {
28         final SortedSet<String> set = new TreeSet<>();
29         for (Path p : paths) {
30             final String name = FilenameUtils.getBaseName(p.toString());
31             final Matcher m = PATTERN_VIDEO_ID.matcher(name);
32             while (m.find()) {
33                 final String alt = m.group(1);
34                 if (alt.startsWith(startText)) {
35                     set.add(alt);
36                 }
37             }
38         }
39         return set;
40     }
41
42     /**
43      * 指定した文字列を含むファイル名一覧を取得します.
44      * @param paths 検索対象のファイルパス.
45      * @param text ファイル名にこの文字列を含むパスを抽出します.
46      * @return 条件に合致するファイルのフルパス.
47      */
48     public static SortedSet<String> getFileNamesContain(Collection<Path> paths, String text) {
49         final SortedSet<String> set = new TreeSet<>();
50         for (Path p : paths) {
51             final String name = FilenameUtils.getBaseName(p.toString());
52             final Matcher m = PATTERN_VIDEO_ID.matcher(name);
53             while (m.find()) {
54                 final String alt = m.group(1);
55                 if (alt.equals(text)) {
56                     set.add(p.toAbsolutePath().toString());
57                     break;
58                 }
59             }
60         }
61         return set;
62     }
63 }