|
| 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.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +/** |
| 33 | + * A builder to construct {@code HumanNameParserParser}. |
| 34 | + * @since 0.2 |
| 35 | + * @see HumanNameParserParser |
| 36 | + */ |
| 37 | +public final class HumanNameParserBuilder { |
| 38 | + |
| 39 | + // constants with default values |
| 40 | + public static final List<String> DEFAULT_SALUTATIONS = Collections.unmodifiableList( |
| 41 | + Arrays.asList( |
| 42 | + "mr", |
| 43 | + "master", |
| 44 | + "mister", |
| 45 | + "mrs", |
| 46 | + "miss", |
| 47 | + "ms", |
| 48 | + "dr", |
| 49 | + "prof", |
| 50 | + "rev", |
| 51 | + "fr", |
| 52 | + "judge", |
| 53 | + "honorable", |
| 54 | + "hon")); |
| 55 | + public static final List<String> DEFAULT_POSTNOMINALS = Collections.unmodifiableList( |
| 56 | + Arrays.asList( |
| 57 | + "phd", |
| 58 | + "ph.d.", |
| 59 | + "ph.d", |
| 60 | + "esq", |
| 61 | + "esquire", |
| 62 | + "apr", |
| 63 | + "rph", |
| 64 | + "pe", |
| 65 | + "md", |
| 66 | + "ma", |
| 67 | + "dmd", |
| 68 | + "cme", |
| 69 | + "dds", |
| 70 | + "cpa", |
| 71 | + "dvm")); |
| 72 | + public static final List<String> DEFAULT_PREFIXES = Collections.unmodifiableList( |
| 73 | + Arrays.asList( |
| 74 | + "bar", |
| 75 | + "ben", |
| 76 | + "bin", |
| 77 | + "da", |
| 78 | + "dal", |
| 79 | + "de la", |
| 80 | + "de", |
| 81 | + "del", |
| 82 | + "der", |
| 83 | + "di", |
| 84 | + "ibn", |
| 85 | + "la", |
| 86 | + "le", |
| 87 | + "san", |
| 88 | + "st", |
| 89 | + "ste", |
| 90 | + "van", |
| 91 | + "van der", |
| 92 | + "van den", |
| 93 | + "vel", |
| 94 | + "von")); |
| 95 | + public static final List<String> DEFAULT_SUFFIXES = Collections.unmodifiableList( |
| 96 | + Arrays.asList( |
| 97 | + "jr", |
| 98 | + "sr", |
| 99 | + "2", |
| 100 | + "ii", |
| 101 | + "iii", |
| 102 | + "iv", |
| 103 | + "v", |
| 104 | + "senior", |
| 105 | + "junior")); |
| 106 | + |
| 107 | + // build values |
| 108 | + private Name name; |
| 109 | + private List<String> salutations; |
| 110 | + private List<String> postnominals; |
| 111 | + private List<String> prefixes; |
| 112 | + private List<String> suffixes; |
| 113 | + |
| 114 | + /** |
| 115 | + * Create the parser builder for a name. |
| 116 | + * @param name the name |
| 117 | + */ |
| 118 | + public HumanNameParserBuilder(String name) { |
| 119 | + super(); |
| 120 | + Objects.requireNonNull(name); |
| 121 | + this.name = new Name(name); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Create the parser builder for a name. |
| 126 | + * @param name the name |
| 127 | + */ |
| 128 | + public HumanNameParserBuilder(Name name) { |
| 129 | + super(); |
| 130 | + Objects.requireNonNull(name); |
| 131 | + this.name = name; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Build the parser. |
| 136 | + * @return a {@code HumanNameParserParser} |
| 137 | + */ |
| 138 | + public HumanNameParserParser build() { |
| 139 | + if (this.salutations == null) { |
| 140 | + this.salutations = DEFAULT_SALUTATIONS; |
| 141 | + } |
| 142 | + if (this.postnominals == null) { |
| 143 | + this.postnominals = DEFAULT_POSTNOMINALS; |
| 144 | + } |
| 145 | + if (this.prefixes == null) { |
| 146 | + this.prefixes = DEFAULT_PREFIXES; |
| 147 | + } |
| 148 | + if (this.suffixes == null) { |
| 149 | + this.suffixes = DEFAULT_SUFFIXES; |
| 150 | + } |
| 151 | + final HumanNameParserParser parser = new HumanNameParserParser( |
| 152 | + name, |
| 153 | + salutations, |
| 154 | + postnominals, |
| 155 | + prefixes, |
| 156 | + suffixes |
| 157 | + ); |
| 158 | + parser.parse(); |
| 159 | + return parser; |
| 160 | + } |
| 161 | + |
| 162 | + // salutations |
| 163 | + |
| 164 | + public HumanNameParserBuilder withSalutations(List<String> salutations) { |
| 165 | + Objects.requireNonNull(salutations); |
| 166 | + this.salutations = salutations; |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
| 170 | + public HumanNameParserBuilder withExtraSalutations(List<String> salutations) { |
| 171 | + Objects.requireNonNull(salutations); |
| 172 | + this.salutations = new ArrayList<>(salutations); |
| 173 | + this.salutations.addAll(DEFAULT_SALUTATIONS); |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + // postnominals |
| 178 | + |
| 179 | + public HumanNameParserBuilder withPostnominals(List<String> postnominals) { |
| 180 | + Objects.requireNonNull(postnominals); |
| 181 | + this.postnominals = postnominals; |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + public HumanNameParserBuilder withExtraPostnominals(List<String> postnominals) { |
| 186 | + Objects.requireNonNull(postnominals); |
| 187 | + this.postnominals = new ArrayList<>(postnominals); |
| 188 | + this.postnominals.addAll(DEFAULT_POSTNOMINALS); |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + // prefixes |
| 193 | + |
| 194 | + public HumanNameParserBuilder withPrefixes(List<String> prefixes) { |
| 195 | + Objects.requireNonNull(prefixes); |
| 196 | + this.prefixes = prefixes; |
| 197 | + return this; |
| 198 | + } |
| 199 | + |
| 200 | + public HumanNameParserBuilder withExtraPrefixes(List<String> prefixes) { |
| 201 | + Objects.requireNonNull(prefixes); |
| 202 | + this.prefixes = new ArrayList<>(prefixes); |
| 203 | + this.prefixes.addAll(DEFAULT_PREFIXES); |
| 204 | + return this; |
| 205 | + } |
| 206 | + |
| 207 | + // suffixes |
| 208 | + |
| 209 | + public HumanNameParserBuilder withSuffixes(List<String> suffixes) { |
| 210 | + Objects.requireNonNull(suffixes); |
| 211 | + this.suffixes = suffixes; |
| 212 | + return this; |
| 213 | + } |
| 214 | + |
| 215 | + public HumanNameParserBuilder withExtraSuffixes(List<String> suffixes) { |
| 216 | + Objects.requireNonNull(suffixes); |
| 217 | + this.suffixes = new ArrayList<>(suffixes); |
| 218 | + this.suffixes.addAll(DEFAULT_SUFFIXES); |
| 219 | + return this; |
| 220 | + } |
| 221 | +} |
0 commit comments