View Javadoc
1   package fr.ifremer.tutti.service.sampling;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
28  import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch;
29  import fr.ifremer.tutti.service.cruise.CruiseCacheAble;
30  import org.apache.commons.lang3.mutable.MutableInt;
31  
32  import java.io.IOException;
33  import java.util.Collection;
34  import java.util.Map;
35  import java.util.Objects;
36  import java.util.TreeMap;
37  
38  /**
39   * Contient le cache des codes de prélèvement disponibles.
40   *
41   * Created on 16/04/16.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 4.5
45   */
46  public class SamplingCodeCache implements CruiseCacheAble {
47  
48      /**
49       * Le code prélèvement le plus grand pour chaque espèce.
50       */
51      private final Map<Integer, MutableInt> highestSamplingCodeBySpecies;
52  
53      public SamplingCodeCache() {
54          this.highestSamplingCodeBySpecies = new TreeMap<>();
55      }
56  
57      @Override
58      public void addFishingOperation(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
59  
60          Objects.requireNonNull(fishingOperation);
61          Objects.requireNonNull(individualObservations);
62          addIndividualObservations(fishingOperation, individualObservations);
63  
64      }
65  
66      @Override
67      public void removeFishingOperation(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
68  
69          Objects.requireNonNull(fishingOperation);
70          Objects.requireNonNull(individualObservations);
71          removeIndividualObservations(fishingOperation, individualObservations);
72  
73      }
74  
75      @Override
76      public void addIndividualObservations(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
77  
78          Objects.requireNonNull(fishingOperation);
79          Objects.requireNonNull(individualObservations);
80  
81          individualObservations
82                  .stream()
83                  .filter(batch -> batch.getSamplingCode() != null)
84                  .forEach(batch -> addSamplingCode(batch.getSpecies().getReferenceTaxonId(), batch.getSamplingCode()));
85  
86      }
87  
88      @Override
89      public void removeIndividualObservations(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
90  
91          Objects.requireNonNull(fishingOperation);
92          Objects.requireNonNull(individualObservations);
93  
94          individualObservations
95                  .stream()
96                  .filter(batch -> batch.getSamplingCode() != null)
97                  .forEach(batch -> removeSamplingCode(batch.getSpecies().getReferenceTaxonId(), batch.getSamplingCode()));
98  
99      }
100 
101     @Override
102     public void close() throws IOException {
103         highestSamplingCodeBySpecies.clear();
104     }
105 
106     public int getNextSamplingCodeId(int speciesId) {
107         MutableInt samplingCode = highestSamplingCodeBySpecies.get(speciesId);
108         return (samplingCode == null ? 0 : samplingCode.intValue()) + 1;
109     }
110 
111     public int addSamplingCode(int speciesId, String samplingCode) {
112         int code = SamplingCodePrefix.extractSamplingCodeIdFromSamplingCode(samplingCode);
113 
114         return highestSamplingCodeBySpecies.compute(speciesId,
115                                                     (key, highestSamplingCode) -> {
116                                                         if (highestSamplingCode == null) {
117                                                             highestSamplingCode = new MutableInt(code);
118                                                         } else {
119                                                             int nexValue = Math.max(highestSamplingCode.intValue(), code);
120                                                             highestSamplingCode.setValue(nexValue);
121                                                         }
122                                                         return highestSamplingCode;
123                                                     }).intValue();
124     }
125 
126     public void removeSamplingCode(int speciesId, String samplingCode) {
127 
128         int code = SamplingCodePrefix.extractSamplingCodeIdFromSamplingCode(samplingCode);
129 
130         // decrement the highest sampling code if it is this code
131         MutableInt samplingCodeFound = highestSamplingCodeBySpecies.get(speciesId);
132         if (samplingCodeFound != null && code == samplingCodeFound.intValue()) {
133             samplingCodeFound.decrement();
134         }
135 
136     }
137 
138 }