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  
11  import io.vavr.Tuple2;
12  import io.vavr.collection.HashMap;
13  import io.vavr.collection.Map;
14  import io.vavr.control.Option;
15  
16  import com.varmateo.yawg.util.GlobMatcher;
17  
18  
19  /**
20   *
21   */
22  /* default */ final class TemplateNameMatcher {
23  
24  
25      private final Map<String, GlobMatcher> _matchersByName;
26  
27  
28      /**
29       *
30       */
31      /* default */ TemplateNameMatcher(final Builder builder) {
32  
33          _matchersByName = builder._matchersByName;
34      }
35  
36  
37      /**
38       * Initializes an empty template name matcher.
39       */
40      public static TemplateNameMatcher empty() {
41  
42          return builder().build();
43      }
44  
45  
46      /**
47       *
48       */
49      public static Builder builder() {
50  
51          return new Builder();
52      }
53  
54  
55      /**
56       *
57       */
58      public Option<String> templateNameFor(final Path path) {
59  
60          return _matchersByName
61                  .filter(entry -> entry._2().test(path))
62                  .headOption()
63                  .map(Tuple2::_1);
64      }
65  
66  
67      /**
68       *
69       */
70      public static final class Builder {
71  
72  
73          private Map<String, GlobMatcher> _matchersByName;
74  
75  
76          /**
77           *
78           */
79          /* default */ Builder() {
80  
81              _matchersByName = HashMap.empty();
82          }
83  
84  
85          /**
86           *
87           */
88          public Builder addTemplateName(
89                  final String templateName,
90                  final GlobMatcher globMatcher) {
91  
92              _matchersByName = _matchersByName.put(templateName, globMatcher);
93  
94              return this;
95          }
96  
97  
98          /**
99           *
100          */
101         public TemplateNameMatcher build() {
102 
103             return new TemplateNameMatcher(this);
104         }
105 
106 
107     }
108 
109 
110 }