View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2016-2019 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.spi;
8   
9   import java.util.Collections;
10  import java.util.Map;
11  import java.util.Optional;
12  
13  
14  /**
15   * Set of variables made available to the template when generating the
16   * final bake result.
17   *
18   * <p>The meaning of this variables is template specific.</p>
19   */
20  public interface PageVars {
21  
22  
23      /**
24       * Retrieves the value of one of the variables.
25       *
26       * @param key The name of the variable whose value is to be
27       * returned.
28       *
29       * @return An <code>Optional</code> containing the value of the
30       * given variable, or nan empty <code>Optional</code> if the
31       * variable does not exist.
32       */
33      Optional<Object> get(String key);
34  
35  
36      /**
37       * Fetches a view of the set of page variables as an unmodifiable
38       * map.
39       *
40       * @return An unmodifiable map containing all the vars. Each entry
41       * corresponds to one page variable.
42       */
43      Map<String, Object> asMap();
44  
45  
46      /**
47       * Creates an empty <code>PageVars</code>.
48       *
49       * @return An empty <code>PageVars</code>.
50       */
51      static PageVars empty() {
52  
53          return new PageVars() {
54              @Override
55              public Optional<Object> get(final String key) {
56                  return Optional.empty();
57              }
58  
59              @Override
60              public Map<String, Object> asMap() {
61                  return Collections.emptyMap();
62              }
63          };
64      }
65  
66  }