Skip to content

Commit 1725345

Browse files
committed
Create a builder allowing different salutations, postnominals, suffixes, and prefixes
1 parent 4726133 commit 1725345

File tree

3 files changed

+269
-78
lines changed

3 files changed

+269
-78
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2010-2020 Jason Priem, Bruno P. Kinoshita
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.tupilabs.human_name_parser;
25+
26+
import java.util.Arrays;
27+
import java.util.Collections;
28+
import java.util.List;
29+
import java.util.Objects;
30+
31+
/**
32+
* @since 0.2
33+
*/
34+
public final class HumanNameParserBuilder {
35+
36+
// constants with default values
37+
public static final List<String> DEFAULT_SALUTATIONS = Collections.unmodifiableList(
38+
Arrays.asList(
39+
"mr",
40+
"master",
41+
"mister",
42+
"mrs",
43+
"miss",
44+
"ms",
45+
"dr",
46+
"prof",
47+
"rev",
48+
"fr",
49+
"judge",
50+
"honorable",
51+
"hon"));
52+
public static final List<String> DEFAULT_POSTNOMINALS = Collections.unmodifiableList(
53+
Arrays.asList(
54+
"phd",
55+
"ph.d.",
56+
"ph.d",
57+
"esq",
58+
"esquire",
59+
"apr",
60+
"rph",
61+
"pe",
62+
"md",
63+
"ma",
64+
"dmd",
65+
"cme",
66+
"dds",
67+
"cpa",
68+
"dvm"));
69+
public static final List<String> DEFAULT_PREFIXES = Collections.unmodifiableList(
70+
Arrays.asList(
71+
"bar",
72+
"ben",
73+
"bin",
74+
"da",
75+
"dal",
76+
"de la",
77+
"de",
78+
"del",
79+
"der",
80+
"di",
81+
"ibn",
82+
"la",
83+
"le",
84+
"san",
85+
"st",
86+
"ste",
87+
"van",
88+
"van der",
89+
"van den",
90+
"vel",
91+
"von"));
92+
public static final List<String> DEFAULT_SUFFIXES = Collections.unmodifiableList(
93+
Arrays.asList(
94+
"jr",
95+
"sr",
96+
"2",
97+
"ii",
98+
"iii",
99+
"iv",
100+
"v",
101+
"senior",
102+
"junior"));
103+
104+
// build values
105+
private Name name;
106+
private List<String> salutations;
107+
private List<String> postnominals;
108+
private List<String> prefixes;
109+
private List<String> suffixes;
110+
111+
/**
112+
* Create the parser builder for a name.
113+
* @param name the name
114+
*/
115+
public HumanNameParserBuilder(String name) {
116+
super();
117+
Objects.requireNonNull(name);
118+
this.name = new Name(name);
119+
}
120+
121+
/**
122+
* Create the parser builder for a name.
123+
* @param name the name
124+
*/
125+
public HumanNameParserBuilder(Name name) {
126+
super();
127+
Objects.requireNonNull(name);
128+
this.name = name;
129+
}
130+
131+
/**
132+
* Build the parser.
133+
* @return a {@code HumanNameParserParser}
134+
*/
135+
public HumanNameParserParser build() {
136+
if (this.salutations == null) {
137+
this.salutations = DEFAULT_SALUTATIONS;
138+
}
139+
if (this.postnominals == null) {
140+
this.postnominals = DEFAULT_POSTNOMINALS;
141+
}
142+
if (this.prefixes == null) {
143+
this.prefixes = DEFAULT_PREFIXES;
144+
}
145+
if (this.suffixes == null) {
146+
this.suffixes = DEFAULT_SUFFIXES;
147+
}
148+
return new HumanNameParserParser(
149+
name,
150+
salutations,
151+
postnominals,
152+
prefixes,
153+
suffixes
154+
);
155+
}
156+
157+
// salutations
158+
159+
public HumanNameParserBuilder withSalutations(List<String> salutations) {
160+
Objects.requireNonNull(salutations);
161+
this.salutations = salutations;
162+
return this;
163+
}
164+
165+
public HumanNameParserBuilder withExtraSalutations(List<String> salutations) {
166+
Objects.requireNonNull(salutations);
167+
this.salutations = DEFAULT_SALUTATIONS;
168+
this.salutations.addAll(salutations);
169+
return this;
170+
}
171+
172+
// postnominals
173+
174+
public HumanNameParserBuilder withPostnominals(List<String> postnominals) {
175+
Objects.requireNonNull(postnominals);
176+
this.postnominals = postnominals;
177+
return this;
178+
}
179+
180+
public HumanNameParserBuilder withExtraPostnominals(List<String> postnominals) {
181+
Objects.requireNonNull(postnominals);
182+
this.postnominals = DEFAULT_POSTNOMINALS;
183+
this.postnominals.addAll(postnominals);
184+
return this;
185+
}
186+
187+
// prefixes
188+
189+
public HumanNameParserBuilder withPrefixes(List<String> prefixes) {
190+
Objects.requireNonNull(prefixes);
191+
this.prefixes = prefixes;
192+
return this;
193+
}
194+
195+
public HumanNameParserBuilder withExtraPrefixes(List<String> prefixes) {
196+
Objects.requireNonNull(prefixes);
197+
this.prefixes = DEFAULT_PREFIXES;
198+
this.prefixes.addAll(prefixes);
199+
return this;
200+
}
201+
202+
// suffixes
203+
204+
public HumanNameParserBuilder withSuffixes(List<String> suffixes) {
205+
Objects.requireNonNull(suffixes);
206+
this.suffixes = suffixes;
207+
return this;
208+
}
209+
210+
public HumanNameParserBuilder withExtraSuffixes(List<String> suffixes) {
211+
Objects.requireNonNull(suffixes);
212+
this.suffixes = DEFAULT_SUFFIXES;
213+
this.suffixes.addAll(suffixes);
214+
return this;
215+
}
216+
}

0 commit comments

Comments
 (0)