1 package fr.ifremer.tutti.persistence.entities;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
26 import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
27 import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType;
28
29 import java.io.Serializable;
30 import java.util.Collection;
31 import java.util.LinkedHashMap;
32
33
34
35
36
37
38
39 public class CaracteristicMap extends LinkedHashMap<Caracteristic, Serializable> {
40
41 private static final long serialVersionUID = 1L;
42
43 public static CaracteristicMap copy(CaracteristicMap map) {
44 CaracteristicMap result = new CaracteristicMap();
45 if (map != null) {
46 result.putAll(map);
47 }
48 return result;
49 }
50
51 public static CaracteristicMap fromCollection(Collection<Caracteristic> caracterstics) {
52 CaracteristicMap result = new CaracteristicMap();
53 if (caracterstics != null) {
54 caracterstics.forEach(caracteristic -> result.put(caracteristic, null));
55 }
56 return result;
57 }
58
59 public boolean hasNonNullValues(Collection<Caracteristic> caracteristicsToIgnore) {
60 return keySet().stream()
61 .filter(caracteristic -> !caracteristicsToIgnore.contains(caracteristic) && get(caracteristic) != null)
62 .count() > 0;
63 }
64
65 public CaracteristicQualitativeValue getQualitativeValue(Caracteristic caracteristic) {
66 Serializable value = get(caracteristic);
67 if (value != null && !(value instanceof CaracteristicQualitativeValue)) {
68 throw new IllegalArgumentException("caracteristic value for " + caracteristic + " is not qualitative: " + value);
69 }
70 return (CaracteristicQualitativeValue) value;
71 }
72
73 public String getStringValue(Caracteristic caracteristic) {
74 Serializable value = get(caracteristic);
75 if (value != null && !(value instanceof String)) {
76 throw new IllegalArgumentException("caracteristic value for " + caracteristic + " is not text: " + value);
77 }
78 return (String) value;
79 }
80
81 public Float getFloatValue(Caracteristic caracteristic) {
82 Serializable value = get(caracteristic);
83 if (value != null && !(value instanceof Float)) {
84 throw new IllegalArgumentException("caracteristic value for " + caracteristic + " is not float: " + value);
85 }
86 return (Float) value;
87 }
88
89 public CaracteristicQualitativeValue removeQualitativeValue(Caracteristic caracteristic) {
90 Serializable remove = remove(caracteristic);
91 if (remove != null && !(remove instanceof CaracteristicQualitativeValue)) {
92 throw new IllegalArgumentException("caracteristic value for " + caracteristic + " is not qualitative: " + remove);
93 }
94 return (CaracteristicQualitativeValue) remove;
95 }
96
97 public String removeStringValue(Caracteristic caracteristic) {
98 Serializable remove = remove(caracteristic);
99 if (remove != null && !(remove instanceof String)) {
100 throw new IllegalArgumentException("caracteristic value for " + caracteristic + " is not text: " + remove);
101 }
102 return (String) remove;
103 }
104
105 public Float removeFloatValue(Caracteristic caracteristic) {
106 Serializable remove = remove(caracteristic);
107 if (remove != null && !(remove instanceof Float)) {
108 throw new IllegalArgumentException("caracteristic value for " + caracteristic + " is not float: " + remove);
109 }
110 return (Float) remove;
111 }
112
113 @Override
114 public Serializable put(Caracteristic key, Serializable value) {
115
116 if (value != null) {
117 CaracteristicType caracteristicType = key.getCaracteristicType();
118 switch (caracteristicType) {
119
120 case NUMBER:
121 if (!(value instanceof Number)) {
122 throw new IllegalArgumentException("caracteristic value for " + key + " is not a number: " + value);
123 }
124 break;
125 case QUALITATIVE:
126
127
128 if (!(value instanceof CaracteristicQualitativeValue)) {
129 throw new IllegalArgumentException("caracteristic value for " + key + " is not qualitative: " + value);
130 }
131 break;
132 case TEXT:
133 if (!(value instanceof String)) {
134 throw new IllegalArgumentException("caracteristic value for " + key + " is not text: " + value);
135 }
136 break;
137 }
138 }
139
140 return super.put(key, value);
141 }
142 }