View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2017-2020 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.spi;
8   
9   import java.nio.file.Path;
10  import java.util.Objects;
11  import java.util.Optional;
12  import java.util.function.Function;
13  
14  
15  /**
16   * A builder of <code>PageContext</code> instances.
17   */
18  public final class PageContextBuilder {
19  
20  
21      private String _dirUrl;
22      private Function<Path, Optional<Template>> _templateFetcher;
23      private String _rootRelativeUrl;
24      private PageVarsBuilder _pageVarsBuilder;
25      private String _bakeId;
26  
27  
28      /**
29       *
30       */
31      private PageContextBuilder() {
32  
33          _dirUrl = null;
34          _templateFetcher = (path -> Optional.empty());
35          _rootRelativeUrl = null;
36          _pageVarsBuilder = PageVarsBuilder.create();
37          _bakeId = null;
38      }
39  
40  
41      /**
42       *
43       */
44      private PageContextBuilder(final PageContext data) {
45  
46          _dirUrl = data.dirUrl();
47          _templateFetcher = data::templateFor;
48          _rootRelativeUrl = data.rootRelativeUrl();
49          _pageVarsBuilder = PageVarsBuilder.create(data.pageVars());
50          _bakeId = data.bakeId();
51      }
52  
53  
54      /**
55       * Creates a new empty <code>PageContextBuilder</code> instance.
56       *
57       * @return A new empty builder object.
58       */
59      public static PageContextBuilder create() {
60  
61          return new PageContextBuilder();
62      }
63  
64  
65      /**
66       * Creates a new <code>PageContextBuilder</code> instance
67       * initialized with the data from the given
68       * <code>PageContext</code>.
69       *
70       * @param initialData Provides the data used for initializing the
71       * returned builder.
72       *
73       * @return A new builder object initialized with the given data.
74       */
75      public static PageContextBuilder create(final PageContext initialData) {
76  
77          return new PageContextBuilder(initialData);
78      }
79  
80  
81      /**
82       * @return A reference to this builder.
83       */
84      public PageContextBuilder dirUrl(final String dirUrl) {
85  
86          _dirUrl = dirUrl;
87          return this;
88      }
89  
90  
91      /**
92       * @param templateFetcher Function that can be used to retrieve
93       * the template to be applied to a given path.
94       *
95       * @return A reference to this builder.
96       */
97      public PageContextBuilder templateFetcher(
98              final Function<Path, Optional<Template>> templateFetcher) {
99  
100         _templateFetcher = templateFetcher;
101         return this;
102     }
103 
104 
105     /**
106      * @return A reference to this builder.
107      */
108     public PageContextBuilder rootRelativeUrl(final String rootRelativeUrl) {
109 
110         _rootRelativeUrl = rootRelativeUrl;
111         return this;
112     }
113 
114 
115     /**
116      * @return A reference to this builder.
117      */
118     public PageContextBuilder pageVars(final PageVars pageVars) {
119 
120         _pageVarsBuilder = PageVarsBuilder.create(pageVars);
121         return this;
122     }
123 
124 
125     /**
126      * Merges the given set of page variables into the set of page
127      * variables currently hold by this builder. Page variables
128      * currently in this builder with the same name will be superseded
129      * by the new provided page variables.
130      *
131      * @param pageVars The set of page variables to add to the set of
132      * page variables hold by this builder.
133      *
134      * @return A reference to this builder.
135      */
136     public PageContextBuilder addPageVars(final PageVars pageVars) {
137 
138         _pageVarsBuilder.addPageVars(pageVars);
139         return this;
140     }
141 
142 
143     /**
144      * Adds one page variable to this builder. An existing page
145      * variable in this builder with the same name will be superseded
146      * by the given page variable.
147      *
148      * @param varName The name of the page variable to be added.
149      *
150      * @param varValue The value of the page variable to be added.
151      *
152      * @return A reference to this builder.
153      */
154     public PageContextBuilder addVar(
155             final String varName,
156             final Object varValue) {
157 
158         _pageVarsBuilder.addVar(varName, varValue);
159         return this;
160     }
161 
162 
163     /**
164      *
165      */
166     public PageContextBuilder bakeId(final String bakeId) {
167 
168         _bakeId = bakeId;
169         return this;
170     }
171 
172 
173     /**
174      * Creates a new <code>PageContext</code> instance initialized
175      * with the current values in this builder.
176      *
177      * @return A newly created <code>PageContext</code> instance.
178      */
179     public PageContext build() {
180 
181         return new PageContextImpl(this);
182     }
183 
184 
185     /**
186      *
187      */
188     private static final class PageContextImpl
189             implements PageContext {
190 
191 
192         private final String _dirUrl;
193         private final Function<Path, Optional<Template>> _templateFetcher;
194         private final PageVars _pageVars;
195         private final String _rootRelativeUrl;
196         private final String _bakeId;
197 
198 
199         /**
200          *
201          */
202         /* default */ PageContextImpl(final PageContextBuilder builder) {
203 
204             _dirUrl = Objects.requireNonNull(builder._dirUrl);
205             _templateFetcher = builder._templateFetcher;
206             _rootRelativeUrl = Objects.requireNonNull(builder._rootRelativeUrl);
207             _pageVars = builder._pageVarsBuilder.build();
208             _bakeId = Objects.requireNonNull(builder._bakeId);
209         }
210 
211 
212         /**
213          * {@inheritDoc}
214          */
215         @Override
216         public String dirUrl() {
217             return _dirUrl;
218         }
219 
220 
221         /**
222          * {@inheritDoc}
223          */
224         @Override
225         public Optional<Template> templateFor(final Path path) {
226             return _templateFetcher.apply(path);
227         }
228 
229 
230         /**
231          * {@inheritDoc}
232          */
233         @Override
234         public PageVars pageVars() {
235             return _pageVars;
236         }
237 
238 
239         /**
240          * {@inheritDoc}
241          */
242         @Override
243         public String rootRelativeUrl() {
244             return _rootRelativeUrl;
245         }
246 
247 
248         /**
249          * {@inheritDoc}
250          */
251         @Override
252         public String bakeId() {
253             return _bakeId;
254         }
255 
256     }
257 
258 }