BrightSide Workbench Full Report + Source Code
ComplexName.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.jpa.embeddables;
20 
21 import java.io.Serializable;
22 import java.util.List;
23 import javax.persistence.Embeddable;
24 import org.apache.commons.text.WordUtils;
25 import org.turro.names.NamesDB;
26 import org.turro.string.Strings;
27 
32 @Embeddable
33 public class ComplexName implements Serializable {
34 
35  private String informal, formal, full;
36 
37  public String getInformal() {
38  return informal;
39  }
40 
41  public void setInformal(String informal) {
42  this.informal = informal;
43  }
44 
45  public String getFormal() {
46  return formal;
47  }
48 
49  public void setFormal(String formal) {
50  this.formal = formal;
51  }
52 
53  public String getFull() {
54  return full;
55  }
56 
57  public void setFull(String full) {
58  this.full = full;
59  }
60 
61  /* Retrievals */
62 
63  public String getFriendly() {
64  if(!Strings.isBlank(informal)) return getInformal();
65  return getSerious();
66  }
67 
68  public String getSerious() {
69  if(!Strings.isBlank(formal)) return getFormal();
70  return getFull();
71  }
72 
73  /* Utils */
74 
75  public void guessFrom(String name, boolean entity) {
76  if(entity || isJuridical(name)) {
77  setFull(Strings.normalize(name));
78  setInformal(null);
79  setFormal(null);
80  } else {
81  String first = NamesDB.firstName(Strings.normalize(name));
82  if(!Strings.isBlank(first)) {
83  setFull(capitalize(name));
84  setInformal(first);
85  if(!Strings.isBlank(getInformal())) {
86  String remaining = Strings.killPrefix(getFull(), getInformal()).trim();
87  String surname = firstSurname(remaining);
88  if(!Strings.isBlank(surname)) {
89  setFormal(getInformal() + " " + surname);
90  }
91  }
92  }
93  }
94  normalize();
95  }
96 
97  private final static List<String> connectives = List.of("del", "de", "van", "von", "i");
98  private final static List<String> juridicals = List.of(",", ".");
99 
100  private void normalize() {
101  full = Strings.normalize(full);
102  informal = Strings.normalize(informal);
103  formal = Strings.normalize(formal);
104  }
105 
106  private String capitalize(String name) {
107  String capitalized = Strings.normalize(WordUtils.capitalizeFully(name, ' ', '.', '-'));
108  for(String connective : connectives) {
109  capitalized = capitalized.replaceAll(
110  " " + WordUtils.capitalizeFully(connective) + " ",
111  " " + connective + " ");
112  }
113  return capitalized;
114  }
115 
116  private String firstSurname(String text) {
117  for(String connective : connectives) {
118  if(text.toLowerCase().startsWith(connective + " ")) {
119  return Strings.untilFirst(text, " ", connective.length() + 1);
120  }
121  }
122  return Strings.untilFirst(text, " ");
123  }
124 
125  private boolean isJuridical(String name) {
126  for(String juridical : juridicals) {
127  if(name.toLowerCase().contains(juridical)) return true;
128  }
129  return false;
130  }
131 
132 }
void guessFrom(String name, boolean entity)