View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.domain.person;
28  
29  import com.rapiddweller.benerator.Generator;
30  import com.rapiddweller.benerator.NonNullGenerator;
31  import com.rapiddweller.benerator.csv.WeightedDatasetCSVGenerator;
32  import com.rapiddweller.benerator.util.GeneratorUtil;
33  import com.rapiddweller.benerator.util.SharedGenerator;
34  import com.rapiddweller.common.Encodings;
35  import com.rapiddweller.domain.address.Country;
36  
37  import java.util.HashMap;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  /**
42   * Generates a given name for a person.<br/>
43   * <br/>
44   * Created: 09.06.2006 21:13:09
45   *
46   * @author Volker Bergmann
47   * @since 0.1
48   */
49  public class GivenNameGenerator extends WeightedDatasetCSVGenerator<String>
50      implements NonNullGenerator<String> {
51  
52    // default instance management -------------------------------------------------------------------------------------
53  
54    private static final Map<String, Generator<String>> defaultInstances =
55        new HashMap<>();
56  
57    /**
58     * Instantiates a new Given name generator.
59     */
60    public GivenNameGenerator() {
61      this(Locale.getDefault().getCountry(), Gender.MALE);
62    }
63  
64    // constructors ----------------------------------------------------------------------------------------------------
65  
66    /**
67     * Instantiates a new Given name generator.
68     *
69     * @param datasetName the dataset name
70     * @param gender      the gender
71     */
72    public GivenNameGenerator(String datasetName, Gender gender) {
73      this(datasetName,
74          "/com/rapiddweller/dataset/region",
75          "/com/rapiddweller/domain/person/givenName",
76          gender);
77    }
78  
79    /**
80     * Instantiates a new Given name generator.
81     *
82     * @param datasetName the dataset name
83     * @param nesting     the nesting
84     * @param baseName    the base name
85     * @param gender      the gender
86     */
87    public GivenNameGenerator(String datasetName, String nesting,
88                              String baseName, Gender gender) {
89      super(String.class, genderBaseName(baseName, gender) + "_{0}.csv",
90          datasetName, nesting, true, Encodings.UTF_8);
91      logger.debug(
92          "Instantiated GivenNameGenerator for dataset '{}' and gender '{}'",
93          datasetName, gender);
94    }
95  
96    /**
97     * Shared instance generator.
98     *
99     * @param datasetName the dataset name
100    * @param gender      the gender
101    * @return the generator
102    */
103   public static Generator<String> sharedInstance(String datasetName,
104                                                  Gender gender) {
105     String key = datasetName + '-' + gender;
106     Generator<String> instance = defaultInstances.get(key);
107     if (instance == null) {
108       instance = new SharedGenerator<>(
109           new GivenNameGenerator(datasetName, gender));
110       defaultInstances.put(key, instance);
111     }
112     return instance;
113   }
114 
115   // public methods --------------------------------------------------------------------------------------------------
116 
117   private static String genderBaseName(String baseName, Gender gender) {
118     if (gender == Gender.FEMALE) {
119       return baseName + "_female";
120     } else if (gender == Gender.MALE) {
121       return baseName + "_male";
122     } else {
123       throw new IllegalArgumentException("Gender: " + gender);
124     }
125   }
126 
127   // NonNullGenerator interface implementation -----------------------------------------------------------------------
128 
129   @Override
130   public double getWeight() {
131     Country country = Country.getInstance(datasetName);
132     return (country != null ? country.getPopulation() : super.getWeight());
133   }
134 
135   // private helpers -------------------------------------------------------------------------------------------------
136 
137   @Override
138   public String generate() {
139     return GeneratorUtil.generateNonNull(this);
140   }
141 
142   @Override
143   public String toString() {
144     return getClass().getSimpleName() + "[" + datasetName + "]";
145   }
146 
147 }