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 java.util.Objects;
28  
29  /**
30   * @author Kevin Morin (Code Lutin)
31   * @since 4.5
32   */
33  public class SamplingCodePrefix {
34  
35      public static final String SEPARATOR = "#";
36      public static final String NO_SURVEY_CODE_LABEL = "~";
37      
38      protected final String prefix;
39      protected final String species;
40  
41      public SamplingCodePrefix(String prefix, String species) {
42          this.prefix = prefix;
43          if (SEPARATOR.equals(species)) {
44              species = NO_SURVEY_CODE_LABEL;
45          }
46          this.species = species;
47      }
48  
49      public SamplingCodePrefix(String samplingCode) {
50          String[] codeParts = samplingCode.split(SEPARATOR);
51          prefix = codeParts[0];
52          species = codeParts[1];
53      }
54  
55      public String getPrefix() {
56          return prefix;
57      }
58  
59      public String getSpecies() {
60          return species;
61      }
62  
63      @Override
64      public String toString() {
65          return prefix + SEPARATOR + species + SEPARATOR;
66      }
67  
68      public String toSamplingCode(int samplingCodeId) {
69          return toString() + samplingCodeId;
70      }
71  
72      public String toSpeciesOnlySamplingCode(int samplingCodeId) {
73          return species + SEPARATOR + samplingCodeId;
74      }
75  
76      /**
77       * Extrait l'id du code (le nombre à la fin)
78       * @param samplingCode
79       * @return
80       */
81      public static int extractSamplingCodeIdFromSamplingCode(String samplingCode) {
82          Objects.requireNonNull(samplingCode);
83          String[] codeParts = samplingCode.split(SEPARATOR);
84          return Integer.parseInt(codeParts[codeParts.length - 1]);
85      }
86  
87  }