View Javadoc
1   package net.sumaris.core.util;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core shared
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
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 org.hibernate.boot.model.naming.Identifier;
26  
27  import java.util.regex.Pattern;
28  
29  /**
30   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>*
31   */
32  public class StringUtils extends org.apache.commons.lang3.StringUtils {
33  
34      public static String underscoreToChangeCase(String value) {
35          if (org.apache.commons.lang3.StringUtils.isBlank(value)) return value;
36          value = value.toLowerCase();
37  
38          // Replace underscore by case change
39          int i = value.indexOf('_');
40          do {
41              if (i > 0 && i+1<value.length()) {
42                  value = value.substring(0, i)
43                          + value.substring(i+1, i+2).toUpperCase()
44                          + ((i+1<value.length()) ? value.substring(i + 2) : "");
45              }
46              // Start with a underscore
47              else if (i == 0 && value.length() > 1) {
48                  value = value.substring(1);
49              }
50              // Finish with a underscore
51              else if (i+1 == value.length()) {
52                  return value.substring(0, i);
53              }
54              i = value.indexOf('_', i+1);
55          } while (i != -1);
56          return value;
57      }
58  
59      public static String changeCaseToUnderscore(String value) {
60          if (org.apache.commons.lang3.StringUtils.isBlank(value)) return value;
61  
62          // Replace case change by an underscore
63          String regex = "([a-z])([A-Z])";
64          String replacement = "$1_$2";
65          return value.replaceAll(regex, replacement).toLowerCase();
66      }
67  }