1 package org.onap.sdc.dcae.catalog.commons;
4 import java.util.HashMap;
5 import java.util.function.Function;
7 import org.onap.sdc.dcae.catalog.commons.MapBuilder;
9 import java.util.function.BiFunction;
11 public class MapBuilder<K,V> {
16 this.map = new HashMap<K,V>();
19 public boolean isEmpty() {
20 return this.map.isEmpty();
23 public MapBuilder<K,V> put(K theKey, V theValue) {
24 this.map.put(theKey, theValue);
28 public MapBuilder<K,V> putOpt(K theKey, V theValue) {
29 if (theValue != null) {
30 this.map.put(theKey, theValue);
35 public MapBuilder<K,V> put(final Map.Entry<? extends K, ? extends V> theEntry) {
36 this.map.put(theEntry.getKey(), theEntry.getValue());
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());
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());
54 /* If theEntries contains multiple entries with the same key then the key gets a suffix in order to make it unique
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) {
61 if (this.map.containsKey(key))
62 key = rekeyFunction.apply((Map.Entry<K,V>)e);
63 this.map.put(key, e.getValue());
68 public MapBuilder<K,V> putAll(final Map<? extends K, ? extends V> theMap) {
69 this.map.putAll(theMap);
73 public Map<K,V> build() {
77 public Map<K,V> buildOpt() {
78 return this.map.isEmpty() ? null : this.map;