Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / utils / Multival.java
1 package org.onap.vid.utils;
2
3 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
4
5 import java.util.Collection;
6 import java.util.function.Function;
7
8 import static java.util.stream.Collectors.toSet;
9
10 @JsonPropertyOrder({"keyType", "valuesType"})
11 public class Multival<K, V> {
12     private final String keyType;
13     private final String valuesType;
14     private final K key;
15     private final Collection<V> values;
16
17     private Multival(String keyType, K key, String valuesType, Collection<V> values) {
18         this.keyType = keyType;
19         this.key = key;
20         this.valuesType = valuesType;
21         this.values = values;
22     }
23
24     public static <K, V> Multival<K, V> of(String keyType, K key, String valuesType, Collection<V> values) {
25         return new Multival<>(keyType, key, valuesType, values);
26     }
27
28     public String getKeyType() {
29         return keyType;
30     }
31
32     public String getValuesType() {
33         return valuesType;
34     }
35
36     public K getKey() {
37         return key;
38     }
39
40     public Collection<V> getValues() {
41         return values;
42     }
43
44     public <W> Multival<K, W> mapEachVal(Function<V, W> mapper) {
45         return Multival.of(
46                 this.getKeyType(),
47                 this.getKey(),
48                 this.getValuesType(),
49                 this.getValues().stream()
50                         .map(mapper)
51                         .collect(toSet())
52         );
53     }
54 }