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.Collections;
10  import java.util.HashMap;
11  import java.util.Map;
12  import java.util.Optional;
13  
14  
15  /**
16   * A builder of <code>PageVars</code> instances.
17   */
18  public final class PageVarsBuilder {
19  
20  
21      private final Map<String, Object> _map;
22  
23  
24      /**
25       *
26       */
27      private PageVarsBuilder(final Map<String, Object> map) {
28  
29          _map = new HashMap<>(map);
30      }
31  
32  
33      /**
34       * Creates a new empty builder.
35       *
36       * @return A newly created <code>PageVarsBuilder</code> instance.
37       */
38      public static PageVarsBuilder create() {
39  
40          return new PageVarsBuilder(Collections.emptyMap());
41      }
42  
43  
44      /**
45       * Creates a new builder initialized with the data from the given
46       * map.
47       *
48       * @param data Used for initializing the builder state.
49       *
50       * @return A newly created <code>Builder</code> instance.
51       */
52      public static PageVarsBuilder create(final Map<String, Object> data) {
53  
54          return new PageVarsBuilder(data);
55      }
56  
57  
58      /**
59       * Creates a new builder initialized with the data from the given
60       * <code>PageVars</code>.
61       *
62       * @param pageVars Used for initializing the builder state.
63       *
64       * @return A newly created <code>Builder</code> instance.
65       */
66      public static PageVarsBuilder create(final PageVars pageVars) {
67  
68          return new PageVarsBuilder(pageVars.asMap());
69      }
70  
71  
72      /**
73       * Adds or updates a page variable variable.
74       */
75      public PageVarsBuilder addVar(
76              final String varName,
77              final Object varValue) {
78  
79          _map.put(varName, varValue);
80          return this;
81      }
82  
83  
84      /**
85       * Adds to this builder all the variables contained in the
86       * given <code>PageVars</code>.
87       */
88      public PageVarsBuilder addPageVars(final PageVars pageVars) {
89  
90          _map.putAll(pageVars.asMap());
91          return this;
92      }
93  
94  
95      /**
96       * Creates a new <code>PageVars</code> instance that containing
97       * the data currently in this builder.
98       *
99       * @return A new <code>PageVars</code> instance.
100      */
101     public PageVars build() {
102 
103         return new PageVarsImpl(_map);
104     }
105 
106 
107     /**
108      *
109      */
110     private static final class PageVarsImpl
111             implements PageVars {
112 
113 
114         private final Map<String, Object> _map;
115 
116 
117         /**
118          *
119          */
120         /* default */ PageVarsImpl() {
121 
122             _map = Collections.emptyMap();
123         }
124 
125 
126         /**
127          *
128          */
129         /* default */ PageVarsImpl(final Map<String, Object> data) {
130 
131             _map = Collections.unmodifiableMap(data);
132         }
133 
134 
135         /**
136          * {@inheritDoc}
137          */
138         @Override
139         public Optional<Object> get(final String key) {
140 
141             final Object value = _map.get(key);
142             return Optional.ofNullable(value);
143         }
144 
145 
146         /**
147          * {@inheritDoc}
148          */
149         @Override
150         public Map<String, Object> asMap() {
151 
152             return _map;
153         }
154 
155     }
156 
157 
158 }