DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / commons / src / main / java / org / onap / sdc / dcae / catalog / commons / MapBuilder.java
1 package org.onap.sdc.dcae.catalog.commons;
2
3 import java.util.Map;
4 import java.util.HashMap;
5 import java.util.function.Function;
6
7 import org.onap.sdc.dcae.catalog.commons.MapBuilder;
8
9 import java.util.function.BiFunction;
10
11 public class MapBuilder<K,V> {
12
13         private Map<K,V> map;
14
15         public MapBuilder() {
16                 this.map = new HashMap<K,V>();
17         }
18
19         public boolean isEmpty() {
20                 return this.map.isEmpty();
21         }
22
23         public MapBuilder<K,V> put(K theKey, V theValue) {
24                 this.map.put(theKey, theValue);
25                 return this;
26         }
27
28         public MapBuilder<K,V> putOpt(K theKey, V theValue) {
29                 if (theValue != null) { 
30                         this.map.put(theKey, theValue);
31                 }
32                 return this;
33         }
34
35         public MapBuilder<K,V> put(final Map.Entry<? extends K, ? extends V> theEntry) {
36                 this.map.put(theEntry.getKey(), theEntry.getValue());
37                 return this;
38         }
39         
40         public MapBuilder<K,V> putOpt(final Map.Entry<? extends K, ? extends V> theEntry) {
41                 if (theEntry != null) {
42                         this.map.put(theEntry.getKey(), theEntry.getValue());
43                 }
44                 return this;
45         }
46     
47         public MapBuilder<K,V> putAll(final Iterable<? extends Map.Entry<? extends K, ? extends V>> theEntries) {
48                 for (final Map.Entry<? extends K, ? extends V> e : theEntries) {
49                         this.map.put(e.getKey(), e.getValue());
50                 }
51                 return this;
52         }
53         
54         /* If theEntries contains multiple entries with the same key then the key gets a suffix in order to make it unique
55      .. */
56 //      public MapBuilder forceAll(final Iterable<? extends Map.Entry<? extends K, ? extends V>> theEntries,
57                 public MapBuilder<K,V> forceAll(final Iterable<? extends Map.Entry<K, V>> theEntries,
58                                                                                                                  Function<Map.Entry<K, V> , K> rekeyFunction) {
59                 for (final Map.Entry<? extends K, ? extends V> e : theEntries) {
60                         K key = e.getKey();
61                         if (this.map.containsKey(key))
62                                 key = rekeyFunction.apply((Map.Entry<K,V>)e);
63                         this.map.put(key, e.getValue());
64                 }
65                 return this;
66         }
67
68         public MapBuilder<K,V> putAll(final Map<? extends K, ? extends V> theMap) {
69                 this.map.putAll(theMap);
70                 return this;
71         }  
72
73         public Map<K,V> build() {
74                 return this.map;
75         }
76
77         public Map<K,V> buildOpt() {
78                 return this.map.isEmpty() ? null : this.map;
79         }
80 }