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 family names.<br/>
43   * <br/>
44   * Created: 09.06.2006 22:03:56
45   *
46   * @author Volker Bergmann
47   * @since 0.1
48   */
49  public class FamilyNameGenerator 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 Family name generator.
59     */
60    public FamilyNameGenerator() {
61      this(Locale.getDefault().getCountry());
62    }
63  
64    // Constructors ----------------------------------------------------------------------------------------------------
65  
66    /**
67     * Instantiates a new Family name generator.
68     *
69     * @param datasetName the dataset name
70     */
71    public FamilyNameGenerator(String datasetName) {
72      this(datasetName,
73          "/com/rapiddweller/dataset/region",
74          "/com/rapiddweller/domain/person/familyName_{0}.csv");
75    }
76  
77    /**
78     * Instantiates a new Family name generator.
79     *
80     * @param datasetName     the dataset name
81     * @param nesting         the nesting
82     * @param fileNamePattern the file name pattern
83     */
84    public FamilyNameGenerator(String datasetName, String nesting,
85                               String fileNamePattern) {
86      super(String.class, fileNamePattern, datasetName, nesting, true,
87          Encodings.UTF_8);
88      logger.debug("Instantiated FamilyNameGenerator for dataset '{}'",
89          datasetName);
90    }
91  
92    /**
93     * Shared instance generator.
94     *
95     * @param datasetName the dataset name
96     * @return the generator
97     */
98    public static Generator<String> sharedInstance(String datasetName) {
99      Generator<String> instance = defaultInstances.get(datasetName);
100     if (instance == null) {
101       instance =
102           new SharedGenerator<>(new FamilyNameGenerator(datasetName));
103       defaultInstances.put(datasetName, instance);
104     }
105     return instance;
106   }
107 
108   @Override
109   public double getWeight() {
110     Country country = Country.getInstance(datasetName);
111     return (country != null ? country.getPopulation() : super.getWeight());
112   }
113 
114   // NonNullGenerator interface implementation -----------------------------------------------------------------------
115 
116   @Override
117   public String generate() {
118     return GeneratorUtil.generateNonNull(this);
119   }
120 
121 }