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.address;
28  
29  import com.rapiddweller.benerator.NonNullGenerator;
30  import com.rapiddweller.benerator.WeightedGenerator;
31  import com.rapiddweller.benerator.dataset.AbstractDatasetGenerator;
32  import com.rapiddweller.benerator.dataset.AtomicDatasetGenerator;
33  import com.rapiddweller.benerator.dataset.Dataset;
34  import com.rapiddweller.benerator.dataset.WeightedDatasetGenerator;
35  import com.rapiddweller.benerator.sample.ConstantGenerator;
36  import com.rapiddweller.benerator.util.GeneratorUtil;
37  import com.rapiddweller.benerator.wrapper.WeighingGeneratorWrapper;
38  
39  /**
40   * Generates a random country.<br/>
41   * <br/>
42   * Created: 11.06.2006 08:15:51
43   *
44   * @author Volker Bergmann
45   */
46  public class CountryGenerator extends AbstractDatasetGenerator<Country>
47      implements NonNullGenerator<Country> {
48  
49    private static final String REGION = "/com/rapiddweller/dataset/region";
50  
51    // Constructors ----------------------------------------------------------------------------------------------------
52  
53    /**
54     * Instantiates a new Country generator.
55     */
56    public CountryGenerator() {
57      this("world");
58    }
59  
60    /**
61     * Instantiates a new Country generator.
62     *
63     * @param datasetName the dataset name
64     */
65    public CountryGenerator(String datasetName) {
66      super(Country.class, REGION, datasetName, true);
67    }
68  
69    @Override
70    protected boolean isAtomic(Dataset dataset) {
71      Country country = Country.getInstance(dataset.getName(), false);
72      return (country != null);
73    }
74  
75    @Override
76    protected WeightedGenerator<Country> createGeneratorForAtomicDataset(
77        Dataset dataset) {
78      WeightedDatasetGenerator<Country> result;
79      Country country = Country.getInstance(dataset.getName(), false);
80      result = createGeneratorForCountry(country);
81      supportedDatasets.add(dataset.getName());
82      return result;
83    }
84  
85    /**
86     * Create generator for country weighted dataset generator.
87     *
88     * @param country the country
89     * @return the weighted dataset generator
90     */
91    protected WeightedDatasetGenerator<Country> createGeneratorForCountry(
92        Country country) {
93      ConstantGenerator<Country> coreGenerator =
94          new ConstantGenerator<>(country);
95      WeightedGenerator<Country> generator =
96          new WeighingGeneratorWrapper<>(coreGenerator,
97              country.getPopulation());
98      totalWeight += generator.getWeight();
99      return new AtomicDatasetGenerator<>(generator, nesting,
100         country.getIsoCode());
101   }
102 
103   @Override
104   public Country generate() {
105     return GeneratorUtil.generateNonNull(this);
106   }
107 
108   // java.lang.Object overrides --------------------------------------------------------------------------------------
109 
110   @Override
111   public String toString() {
112     return getClass().getSimpleName() + "[" + getDataset() + "]";
113   }
114 
115 }