View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2016-2020 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.core;
8   
9   import java.nio.file.Path;
10  import java.nio.file.Paths;
11  import java.util.function.Predicate;
12  
13  
14  /**
15   * Used for checking if a file should be used in a baking.
16   */
17  /* default */ final class DirEntryChecker {
18  
19  
20      private final DirBakeOptions _options;
21      private final boolean _isIncludeHere;
22  
23  
24      /**
25       *
26       */
27      /* default */ DirEntryChecker(final DirBakeOptions options) {
28  
29          _options = options;
30          _isIncludeHere = options.filesToIncludeHere.isDefined();
31      }
32  
33  
34      /**
35       *
36       */
37      public Predicate<Path> asPathPredicate() {
38  
39          return this::testPath;
40      }
41  
42  
43      /**
44       *
45       */
46      public Predicate<String> asStringPredicate() {
47  
48          return this::testString;
49      }
50  
51  
52      /**
53       *
54       */
55      private boolean testPath(final Path path) {
56  
57          return _isIncludeHere
58                  ? testForIncludeHere(path)
59                  : !testForExclude(path);
60      }
61  
62  
63      /**
64       *
65       */
66      private boolean testString(final String name) {
67  
68          final Path path = Paths.get(name);
69  
70          return testPath(path);
71      }
72  
73  
74      /**
75       *
76       */
77      private boolean testForIncludeHere(final Path path) {
78  
79          return _options.filesToIncludeHere
80                  .map(matcher -> matcher.test(path))
81                  .getOrElse(false);
82      }
83  
84  
85      /**
86       *
87       */
88      private boolean testForExclude(final Path path) {
89  
90          return _options.filesToExclude
91                  .map(matcher -> matcher.test(path))
92                  .getOrElse(false)
93                  || _options.filesToExcludeHere
94                  .map(matcher -> matcher.test(path))
95                  .getOrElse(false);
96      }
97  
98  
99  }