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.util.ArrayList;
10  import java.util.Collections;
11  import java.util.List;
12  import java.util.Objects;
13  
14  import com.varmateo.yawg.spi.TemplateContext.Author;
15  
16  
17  /**
18   * Builder of <code>TemplateContext</code> instances.
19   */
20  public final class TemplateContextBuilder {
21  
22  
23      private List<Author> _authors;
24      private String _body;
25      private String _pageUrl;
26      private String _rootRelativeUrl;
27      private PageVars _pageVars;
28      private String _title;
29      private String _bakeId;
30  
31  
32      /**
33       *
34       */
35      private TemplateContextBuilder() {
36  
37          _authors = new ArrayList<>();
38          _body = null;
39          _pageUrl = null;
40          _rootRelativeUrl = null;
41          _pageVars = PageVars.empty();
42          _title = null;
43          _bakeId = null;
44      }
45  
46  
47      /**
48       * Creates a new empty builder.
49       *
50       * @return A newly created <code>TemplateContextBuilder</code>
51       * instance.
52       */
53      public static TemplateContextBuilder create() {
54  
55          return new TemplateContextBuilder();
56      }
57  
58  
59      /**
60       *
61       */
62      public TemplateContextBuilder addAuthor(
63              final String name,
64              final String email) {
65  
66          final Author author = Author.create(name, email);
67          _authors.add(author);
68          return this;
69      }
70  
71  
72      /**
73       *
74       */
75      public TemplateContextBuilder body(final String body) {
76  
77          _body = Objects.requireNonNull(body);
78          return this;
79      }
80  
81  
82      /**
83       *
84       */
85      public TemplateContextBuilder pageUrl(final String pageUrl) {
86  
87          _pageUrl = pageUrl;
88          return this;
89      }
90  
91  
92      /**
93       *
94       */
95      public TemplateContextBuilder rootRelativeUrl(
96              final String rootRelativeUrl) {
97  
98          _rootRelativeUrl = rootRelativeUrl;
99          return this;
100     }
101 
102 
103     /**
104      *
105      */
106     public TemplateContextBuilder pageVars(final PageVars pageVars) {
107 
108         _pageVars = pageVars;
109         return this;
110     }
111 
112 
113     /**
114      *
115      */
116     public TemplateContextBuilder title(final String title) {
117 
118         _title = Objects.requireNonNull(title);
119         return this;
120     }
121 
122 
123     /**
124      *
125      */
126     public TemplateContextBuilder bakeId(final String bakeId) {
127 
128         _bakeId = bakeId;
129         return this;
130     }
131 
132 
133     /**
134      *
135      */
136     public TemplateContext build() {
137 
138         return new TemplateContextImpl(this);
139     }
140 
141 
142     /**
143      *
144      */
145     private static final class TemplateContextImpl
146             implements TemplateContext {
147 
148 
149         private final List<Author> _authors;
150         private final String _body;
151         private final String _pageUrl;
152         private final PageVars _pageVars;
153         private final String _rootRelativeUrl;
154         private final String _title;
155         private final String _bakeId;
156 
157 
158         /**
159          *
160          */
161         /* default */ TemplateContextImpl(final TemplateContextBuilder builder) {
162 
163             _authors = Collections.unmodifiableList(new ArrayList<>(builder._authors));
164             _body = Objects.requireNonNull(builder._body);
165             _pageUrl = Objects.requireNonNull(builder._pageUrl);
166             _pageVars = Objects.requireNonNull(builder._pageVars);
167             _rootRelativeUrl = Objects.requireNonNull(builder._rootRelativeUrl);
168             _title = Objects.requireNonNull(builder._title);
169             _bakeId = Objects.requireNonNull(builder._bakeId);
170         }
171 
172 
173         /**
174          * {@inheritDoc}
175          */
176         @Override
177         public Iterable<Author> authors() {
178             return _authors;
179         }
180 
181 
182         /**
183          * {@inheritDoc}
184          */
185         @Override
186         public String body() {
187             return _body;
188         }
189 
190 
191         /**
192          * {@inheritDoc}
193          */
194         @Override
195         public String pageUrl() {
196             return _pageUrl;
197         }
198 
199 
200         /**
201          * {@inheritDoc}
202          */
203         @Override
204         public PageVars pageVars() {
205             return _pageVars;
206         }
207 
208 
209         /**
210          * {@inheritDoc}
211          */
212         @Override
213         public String rootRelativeUrl() {
214             return _rootRelativeUrl;
215         }
216 
217 
218         /**
219          * {@inheritDoc}
220          */
221         @Override
222         public String title() {
223             return _title;
224         }
225 
226 
227         /**
228          * {@inheritDoc}
229          */
230         @Override
231         public String bakeId() {
232             return _bakeId;
233         }
234 
235     }
236 
237 }