View Javadoc
1   package fr.ifremer.tutti.persistence.entities;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * A map (key are {@link Caracteristic}, values are values of caracteristics).
35   *
36   * @author Tony Chemit - chemit@codelutin.com
37   * @since 0.3
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                     // check value is a qualitative value
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 }