View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2016-2020 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.util;
8   
9   import java.util.Collections;
10  import java.util.List;
11  import java.util.Map;
12  import java.util.Objects;
13  import java.util.Optional;
14  import java.util.Set;
15  
16  import com.varmateo.yawg.api.YawgException;
17  
18  
19  /**
20   * Wrapper for an unmodifiable map with utility methods for retrieving
21   * values.
22   */
23  public final class SimpleMap {
24  
25  
26      private final Map<String, Object> _map;
27  
28  
29      /**
30       * Initializes this simple map with the given map.
31       *
32       * <p>As a matter of optimization we assume the given map will
33       * never be modified. It is up to the caller to ensure no changes
34       * are performed in the givan map</p>.
35       *
36       * @param map The map to be wrapped by this simple map.
37       */
38      public SimpleMap(final Map<String, Object> map) {
39  
40          _map = Objects.requireNonNull(map);
41      }
42  
43  
44      /**
45       * Fetches a view on the contents of this simple map as an
46       * unmodifiable map.
47       *
48       * @return A unmodifiable view of the contents of this simple map.
49       */
50      public Map<String, Object> asMap() {
51  
52          return Collections.unmodifiableMap(_map);
53      }
54  
55  
56      /**
57       *
58       */
59      public <T> Optional<T> get(
60              final String key,
61              final Class<T> klass) {
62  
63          final T value = getWithType(key, klass);
64  
65          return Optional.ofNullable(value);
66      }
67  
68  
69      /**
70       *
71       */
72      public Optional<String> getString(final String key) {
73  
74          return get(key, String.class);
75      }
76  
77  
78      /**
79       *
80       */
81      public Optional<SimpleMap> getMap(final String key) {
82  
83          @SuppressWarnings("unchecked")
84          final Map<String, Object> value = (Map<String, Object>) getWithType(key, Map.class);
85  
86          return Optional.ofNullable(value)
87                  .map(SimpleMap::new);
88      }
89  
90  
91      /**
92       *
93       */
94      public <T> Optional<SimpleList<T>> getList(
95              final String key,
96              final Class<T> itemsClass) {
97  
98          @SuppressWarnings("unchecked")
99          final List<Object> value = (List<Object>) getWithType(key, List.class);
100 
101         return Optional.ofNullable(value)
102                 .map(v -> new SimpleList<T>(v, itemsClass));
103     }
104 
105 
106     /**
107      *
108      */
109     private <T> T getWithType(
110             final String key,
111             final Class<T> klass) {
112 
113         final Object value = _map.get(key);
114 
115         if ( (value != null) && !klass.isInstance(value) ) {
116             throw SimpleMapException.invalidValue(key, klass, value.getClass());
117         }
118 
119         @SuppressWarnings("unchecked")
120         final T result = (T) value;
121 
122         return result;
123     }
124 
125 
126     /**
127      *
128      */
129     public Set<String> keySet() {
130 
131         return _map.keySet();
132     }
133 
134 
135 }