1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
41
42
43
44
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
52
53
54
55
56 public CountryGenerator() {
57 this("world");
58 }
59
60
61
62
63
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
87
88
89
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
109
110 @Override
111 public String toString() {
112 return getClass().getSimpleName() + "[" + getDataset() + "]";
113 }
114
115 }