View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.ds;
17  
18  import java.util.Collection;
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.function.BiConsumer;
22  import java.util.function.BiFunction;
23  import java.util.function.Function;
24  
25  import org.apache.commons.lang3.StringUtils;
26  
27  import com.google.common.base.CaseFormat;
28  
29  public class ParamMap<K, V> implements Map<K, V> {
30  
31      private final Map<K, V> parent;
32  
33      public ParamMap(final Map<K, V> parent) {
34          this.parent = parent;
35      }
36  
37      protected Object toCamelCase(final Object key) {
38          if (key == null) {
39              return key;
40          }
41          String keyStr = key.toString();
42          if (keyStr.indexOf('_') < 0) {
43              keyStr = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, StringUtils.uncapitalize(keyStr));
44          } else {
45              keyStr = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, keyStr);
46          }
47          return keyStr;
48      }
49  
50      public int size() {
51          return parent.size();
52      }
53  
54      public boolean isEmpty() {
55          return parent.isEmpty();
56      }
57  
58      public boolean containsKey(Object key) {
59          if (parent.containsKey(key)) {
60              return true;
61          }
62          return parent.containsKey(toCamelCase(key));
63      }
64  
65      public boolean containsValue(Object value) {
66          return parent.containsValue(value);
67      }
68  
69      public V get(Object key) {
70          final V value = parent.get(key);
71          if (value != null) {
72              return value;
73          }
74          return parent.get(toCamelCase(key));
75      }
76  
77      public V put(K key, V value) {
78          return parent.put(key, value);
79      }
80  
81      public V remove(Object key) {
82          final V value = parent.remove(key);
83          if (value != null) {
84              return value;
85          }
86          return parent.remove(key);
87      }
88  
89      public void putAll(Map<? extends K, ? extends V> m) {
90          parent.putAll(m);
91      }
92  
93      public void clear() {
94          parent.clear();
95      }
96  
97      public Set<K> keySet() {
98          // return original keys
99          return parent.keySet();
100     }
101 
102     public Collection<V> values() {
103         return parent.values();
104     }
105 
106     public Set<Entry<K, V>> entrySet() {
107         // return original keys
108         return parent.entrySet();
109     }
110 
111     public boolean equals(Object o) {
112         return parent.equals(o);
113     }
114 
115     public int hashCode() {
116         return parent.hashCode();
117     }
118 
119     public V getOrDefault(Object key, V defaultValue) {
120         final V value = parent.get(key);
121         if (value != null) {
122             return value;
123         }
124         return parent.getOrDefault(toCamelCase(key), defaultValue);
125     }
126 
127     public void forEach(BiConsumer<? super K, ? super V> action) {
128         parent.forEach(action);
129     }
130 
131     public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
132         parent.replaceAll(function);
133     }
134 
135     public V putIfAbsent(K key, V value) {
136         return parent.putIfAbsent(key, value);
137     }
138 
139     public boolean remove(Object key, Object value) {
140         if (parent.remove(key, value)) {
141             return true;
142         }
143         return parent.remove(toCamelCase(key), value);
144     }
145 
146     public boolean replace(K key, V oldValue, V newValue) {
147         if (parent.replace(key, oldValue, newValue)) {
148             return true;
149         }
150         return parent.replace(key, oldValue, newValue);
151     }
152 
153     public V replace(K key, V value) {
154         // use original key
155         return parent.replace(key, value);
156     }
157 
158     public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
159         // use original key
160         return parent.computeIfAbsent(key, mappingFunction);
161     }
162 
163     public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
164         // use original key
165         return parent.computeIfPresent(key, remappingFunction);
166     }
167 
168     public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
169         // use original key
170         return parent.compute(key, remappingFunction);
171     }
172 
173     public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
174         // use original key
175         return parent.merge(key, value, remappingFunction);
176     }
177 }